Hey i am a php developer and this is my first go with jsp. Now i retrieved a Json string from my class and converted it into GSON. I display a field in my result.jsp for eg:- ID and on clicking the id it should go to details.jsp and show more info about that ID
Currently my result.jsp is as follows:-
<html>
<body>
<div class="list-group">
<%
String json = (String)request.getAttribute("jsonstring");
Gson gson = new Gson();
ConCom diff = new ConCom();
diff = gson.fromJson(json, ConCom.class);
List<ComparisonResultDTOarr> ls = diff.getComparisonResultDTOarr();
for(int i = 0;i<ls.size();i++)
{
List<AuditItemLogsDTOArr> lsinner = ls.get(i).getAuditItemLogsDTOArr();
%><a href="#" class="list-group-item">
<%out.println(lsinner.get(0).getKeyAsString());%></a><%
}
%>
</div>
</body>
</html>
I read around SO and googled it and understood that that i could make a hidden form. Now I create a form with the following two fields and using the anchor tag i submit the form. But the values in the form need to be posted according to the ID clicked, how can i make that dynamic?
So if my form is as follows:-
<form action="details.jsp" method="post">
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="phone">
</form>
And my details.jsp will be like:-
<%= request.getParameter("firstname") %>
<%= request.getParameter("phone") %>
I want the firstname and phone to be set according to the ID clicked and the form to be submitted. I can obtain the String/Integer value from my diff object in this page. Would i need to use JQuery? Any help?
Also i know i should be using JSTL. And i will get to that soon. Thank you.
You could call a javascript function during the onclick event of your anchor tag:
<a href="#" onclick="submitHiddenForm("<%=lsinner.getFirstName()%>", "<%=lsinner.getPhone()%>");">...
Your JS function would like like:
function submitHiddenForm(firstName, phone) {
document.getElementById("firstname").value = firstName;
document.getElementById("phone").value = phone;
// attach a name attribute to your form tag
// submit the form
document.myForm.submit();
}
I hope this helps.
EDIT: changed diff to lsinner, since that's the var used in the loop.
On click of the ID call a javascript function passing the values inside the function you can dynamically set the values of the form by getting each element like document.getElementById("firstname").value=value passed similarly set the other fields and in the end document.myform.submit();
Note since we are fetching HTML elements by Id you can use
<form action="details.jsp" method="post" name="myform">
First name: <input type="hidden" name="firstname" id="firstname"><br>
Last name: <input type="hidden" name="phone" id="phone">
</form>
type="hidden" will hide the elements.
check these for better understanding.
How to submit a form using javascript?
If you have the diff object in details.jsp as well, it should be enough to pass only the ID as a parameter, in a normal link.
details
You'd probably do the same in PHP.
JQuery is client side JavaScript and not required to solve your problem.
Related
Im trying to get the values of some td elements where the data consist of data from MySQL table. It displays the data fine in my browser (e.g. if i change type from "hidden" to "submit"), but when I try to get the value i only get null.
Here are my jsp and it displays the correct results in the browser.
<td>
<form action="history.jsp" method="get">
<input type="hidden" name="res" value="<%=his.getRes()%>"/>
</form>
</td>
When i try to print the values however, I only get "null" at of evey :
<%
String res = request.getParameter("res");
System.out.print(res);
%>
I'm still very new, so it's proberly a straight forward answer. Thank you in advance for the help.
I suggest that you change the name of your variable :
String newname = request.getParameter("res");
System.out.println(newname)
One can submit (=send) only one <form>. So one must assume there is just one single td with one <form>. Forms also may not be nested in an outer form.
It need some way to submit the form.
So experiment first with:
<td>
<form action="history.jsp" method="get">
<input type="text" name="res" value="<%=his.getRes()%>"/>
<input type="submit" value="Send"/>
</form>
</td>
This will show whether his.getRes() yielded something. And allows a manual submit in the browser.
I am sending a request to the servlet it returning some data from db am constructing table and check box with the resulted data in, servlet itself using out.println and now i need to do select the data for further manipulation using check box and now i dono how to get a value of selected text boxes.
here is my servlet code,
ps=connection.prepareStatement("select t.tc_name,s.scenario_name,t.scenario_id from testcase t, scenario s where t.scenario_id=s.scenario_id;");
ResultSet rs=ps.executeQuery();
out.println("<table>");
/*out.println(executionValues.append("<tr><td>").append("Test Case Name :").append("</td><td>").append("Scenario Name :").append("</td></tr>"));*/
while(rs.next()){
out.println("<li class='panel' value='"+rs.getInt("scenario_id")+"'><b>Scenario Name:</b>"+rs.getString("scenario_name")+"</li><b>Test Case Name:</b>"+rs.getString("tc_name")+"<input type=\"checkbox\" name=\"checkbox\"></li>");
}
you should remove the ;
your sql query:
("select t.tc_name,s.scenario_name,t.scenario_id from testcase t, scenario s where t.scenario_id=s.scenario_id;");
you should change like:
("select t.tc_name,s.scenario_name,t.scenario_id from testcase t, scenario s where t.scenario_id=s.scenario_id");
You're printing a whole new <html> and <form> around every single checkbox. Your HTML ends up in browser like as:
<html>
<head></head>
<body>
<html><body><form><input type="checkbox"></form></body></html>
<form><input type="submit"></form>
</body>
</html>
This is syntactically invalid HTML. You need to rewrite your code so that all checkboxes and the submit button ends up in the same form:
<html>
<head></head>
<body>
<form>
<input type="checkbox">
<input type="submit">
</form>
</body>
</html>
Then you also don't need those ugly JavaScript workarounds. You just give the checkboxes the same name, but a different value. This way you can just grab the checked values by HttpServletRequest#getParameterValues().
String[] users = request.getParameterValues("user");
For example:
<form name="input" action="html_form_action" method="get">
<input type="checkbox" name="vehicle" value="Bike">I have a bike<br>
<input type="checkbox" name="vehicle" value="Car">I have a car
<br><br>
<input type="submit" value="Submit">
</form>
If you check both of checkboxes your server will receive this parameters like so:
http://sitename.com/your_page.jsp?vehicle=Bike&vehicle=Car
After that you can get values like this:
String checkboxValues = request.getParameter("vehicle");
checkboxValues gets all values separated by comma.
Refer this link:
http://theopentutorials.com/examples/java-ee/servlet/getting-checkbox-values-from-html-form-in-servlet/
Dear suganth It is not appropriate now to put html code in servlet. Instead you use jsp pages.
but if you have to do in this way then You may provide
if(conditionMatch){
// code for checked box
}
else{
// code for unchecked box
}
Hope this helps
I am assigning href to a button and passing a value to the controller
JSP page
<td><input type="button" name="remove" value="DELETE"/>
</td>
In controller, I am trying to get the value when the button is clicked using:
if(request.getParameter("remove") !=null)
{
int cart_id=(Integer)request.getSession(false).getAttribute("cartid");
b_id = (String) request.getParameter("book_id");
int bid=Integer.parseInt(b_id);
System.out.println(bid);
}
This prints a null value though I have passed book_id value in the URL.
I want to know how can I get the value passed in the URL via the button.
You can't combine [a] tag with an [input] tag like that.
Try using a form instead with hidden inputs:
<form action=viewController>
<input type=hidden name=remove value=delete>
<input type=hidden name=book_id value=<%=vo.getBookId()%>>
<input type=submit value='Delete'>
</form>
The resulting url will be : viewController?remove=delete&book_id=...
When submit button is pressed, the entire form will be sent. You can select where data will be sent by using attribute action:
<form action="demo_form.jsp">
<!--inputs here-->
<input type="submit">Send me now</input>
</form>
In that case form will be sent to demo_form.jsp. In HTML5 you can use formaction attribute if you want use different servlets for different buttons
Any way, you shouldn't use links for sending forms.
It is possible to use links without button:
Remove
Is there a way to get the name of the form element itself using JSP? I searched google and did not find any examples which show how to get the name of the form value specified in the JSP file.
For example, I have the below form,
<html>
<form name="register" action="servregister" method="POST"
onsubmit="return validate();">
</form>
</html>
I would like to get the string "register" to a string variable in JSP. Is there a way to do this?
No, a <form> is not submitted with the request. Instead create a hidden input element which holds that information.
<input type="hidden" name="name of form" value="value" />
or possibly the submit element
<input type="submit" name="distinguishing name" value="submit" />
Either of these in a form with be sent as a url-encoded parameter.
There is probably a better solution to what you are trying to do if you explain your goals.
Consider looking into different patterns for achieving your goals.
I need to pass the value from jsp file property in a loop. Here is the code
</tr>
<%
for (int i = 0; i < value.size(); i++) {
<html:form action="save" method="post" styleId="update" enctype="multipart/form-data">
<input type=hidden id="secfeaturetype" name="secfeaturetype" value="" />
<html:file property="testfile" styleId="testfile"/>
<input type="button" value="Update" onclick='javascript:check_updatefields()' />
</html:form>
</td>
</tr>
I am trying to do some validation in javascript and it is reading only the first file properly and not the rest. Any ideas ?
function check_updatefields() {
var file = jQuery('#testfile').val();
alert(testfile);
}
Am I missing something here ? Thanks.
"id", it means it's unique in your page, so if there's more than one, the browser will only identify the first element, so here you'd better use "class", correcting it like this: styleClass="testfile". js: jQuery('.testfile').
As I mentioned in a comment above, the id attribute is supposed to be unique, so creating elements in a loop with the same id is giving you invalid html and $("#testfile") will select only the first element with that id (except in some browsers that might select only the last).
A better solution for repeated elements is to give them a common class and distinguish between them according to which container they belong to, or by a known relationship to some other element if not the container. Or select by tagname if that is unique enough with the containing element, or by the name attribute. You seem to be using jQuery, and it makes any of these options easy.
I think you can tidy your code up to something like this:
<table id="yourTableIdHere">
<% for (int i = 0; i < value.size(); i++) { %>
<tr><td>
<html:form action="save" method="post" styleId="update" enctype="multipart/form-data">
<input type=hidden name="secfeaturetype" value="" />
<html:file property="testfile" styleClass="testfile"/>
<input type="button" value="Update" />
</html:form>
</td></tr>
<% }%>
</table>
I'm not really familiar with the Struts html tag library, but I believe styleClass="testfile" will render an attribute class="testfile" (if not please substitute whatever the appropriate thing is to render class=...). Notice I removed your inline onclick=... attribute. You can assign these handlers in one step with jQuery:
$(document).ready(function() {
// assign a click handler to all the Update buttons that are
// within forms within your table element:
$('#yourTableIdHere form input[type="button"][value="Update"]').click(function() {
var $myForm = $(this).parent(); // or $(this).closest('form'),
$myFileInput = $myForm.find('input.testfile'),
$myHiddenInput = $myForm.find('input[type="hidden"]');
alert($myFileInput.val()); // alert value of file input
$myHiddenInput.val("something"); // set value of hidden
});
});
Within the click handler, this is the clicked button, so from $(this) you can navigate to the parent element to get a reference to the form that particular button belongs to, and from that form you can use .find() to select the file and hidden inputs in the current form.