values are not getting display in jsp - java

I started learning jsp and I have written a form.jsp which contains some fields, and I have another page called formResult.jsp.
I am trying to display the value of form.jsp to formResult.jsp using El(Expression Language).
I dont know what is wrong with my code, the value are not getting populated from form.jsp to formResult.jsp except fName(First Name).
This is very annoying and abnormal. I don't know what to do.
Please help me.
form.jsp
<form action="formResult.jsp">
<fieldset>
<legend>
<font face="Courier New" size="+1"
color="red">
Please enter your information
</font>
</legend>
</fieldset>
<table>
<tr>
<td>First Name:</td>
<td><input type="text" name="fName"></td>
</tr>
<tr>
<td>Last Name:</td>
<td><input type="text" name="lName"></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="email">
</tr>
<tr>
<td>Address</td>
<td><textarea rows="3" cols="20" name="address"></textarea>
</tr>
<tr>
<td><input type="submit" value="SUBMIT"></td>
</tr>
</table>
</form>
With all the possible way I tried. the bellow three ways are not working.I am just getting the first value
formResult.jsp
<body>
First Name :${param.fName}<br>
Last Name :${param.lName}<br>
Email :${param.email}<br>
Address :${param.address}<br>
First Name :${param["fName"]}<br>
Last Name :${param["lName"]}<br>
Email :${param["email"]}<br>
Address :${param["address"]}<br>
First Name :${param[0]}<br>
Last Name :${param[1]}<br>
Email :${param[2]}<br>
Address :${param[3]}<br>
</body>
Input
Output

Related

How to generate a download request from html page to controller of spring mvc

<html>
<head>
<title>Student Registration</title>
</head>
<body>
<h3 align="center">New Trainee Registration</h3>
<table align="center" cellpadding="10">
<!-- First Name -->
<tr>
<td>First Name</td>
<td>${traineeData.fName}</td>
</tr>
<!-- Last Name -->
<tr>
<td>Last Name</td>
<td>${traineeData.lName}</td>
</tr>
<!-- Father's Name -->
<tr>
<td>Father's Name</td>
<td>${traineeData.fatherName}</td>
</tr>
<!-- Mobile Number -->
<tr>
<td>Mobile No.</td>
<td>${traineeData.mobile}</td>
</tr>
<tr>
<td>Joining Date</td>
<td>${traineeData.joiningDate}></td>
</tr>
<tr>
<td>Grade</td>
<td>${traineeData.grade}</td>
</tr>
<tr>
<td>Gender</td>
<td>${traineeData.gender}</td>
</tr>
<!-- Course -->
<tr>
<td>COURSES<br />APPLIED FOR
</td>
<td>${traineeData.course}</td>
</tr>
<tr>
<td>Joining Date</td>
<td>${traineeData.joiningDate}</td>
</tr>
<!-- Submit and Reset -->
<tr>
<td colspan="2" align="center">
<button type="button" >Generate Certificate</button>
</td>
</tr>
</table>
</body>
</html>
Let's say, I have a download button on my HTML page. when clicking on this button a request goes through the controller of the spring MVC framework along with "enrollmentNo".
On Controller class, I will use this enrollmentNo.
I have a class which will generate a certificate for the student by fetching data using the student's enrollmentNo.
and response goes back and a file will download(which a pdf file generated by my pre-created class)
I have tried with many ways but not get what I want.
#RequestMapping(value = "/generateCertificate")
public void generateCertificate(#RequestParam("traineeData.enrollmentNo") int enrollmentNo) {
System.out.println("you in /generateCertificate");
// I don't know what should I return...
// please change return type accordingly...
}
I want a button on my HTML page named "Generate certificate". The code should be easy and normal, Ajax can be used here.
Your button isn't actually doing anything, useful.
Try this code around your button...
<tr>
<td colspan="2" align="center">
<form action="/generateCertificate" method="GET">
<input type="hidden" name="enrollmentNo" value="${traineeData.enrollmentNo}"
<button type="submit">Generate Certificate</button>
</form>
</td>
</tr>
Then your controller will need to changed slightly...
#RequestMapping(value = "/generateCertificate")
public void generateCertificate(#RequestParam("enrollmentNo") int enrollmentNo) {
// The #RequestParam was changed to the name of the param from the form and not the object/value.
}

getting null values for textbox for selected checkbox in a row while sending from jsp to servlet

<c:forEach var="product" items="${list}">
<tr>
<td align="center"><%-- <input type="hidden"
value="${product.productId}" name="id"> --%> <c:out
value="${product.productName}" /></td>
<td align="center"><c:out value="${product.productPrice}" /></td>
<td align="center"><input type="text" name="quantity<c:out value="${product.productId}" />"
value="<%=1%>" /></td>
<td align="center"><input type="checkbox" name="selectedItems"
value="<c:out value=" ${product.productId}"/>"/></td>
</tr>
</c:forEach>
getting null value for quantity for particular selected row by checkbox in servlet
The problem is in line name="quantity<c:out value="${product.productId}" />". Try changing to name='quantity<c:out value="${product.productId}" />' if your intention is to have name, e.g. like name=quantity1, name=quantity2, etc.
Or alternatively set the value of the quantity as ${product.productId}, e.g.
<td align="center"><input type="text" name="quantity" value='<c:out value="${product.productId}" />' /></td>
Update:
Try to simplify your problem before jumping into the loop.
Now are you able to process this as JSP POST form and see if you are able retrieve a value out of quantity?
<tr>
<td align="center"><input type="text" name="quantity" value="some default value before user overrides this" /></td>
</tr>

Login with Jsoup Java

How to login via jsoup?
<table border="0" cellpadding="8px">
<tbody>
<tr>
<td align="left"> <span id="ctl00_bodyContent_LabelTurni1" style="font-size: 13pt;">Nome utente</span> </td>
<td align="left"> <input name="ctl00$bodyContent$txtUser" type="text" size="30" id="ctl00_bodyContent_txtUser"> </td>
</tr>
<tr>
<td align="left"> <span id="ctl00_bodyContent_LabelTurni2" style="font-size: 13pt;">Password</span> </td>
<td align="left"> <input name="ctl00$bodyContent$txtPassword" type="password" size="30" id="ctl00_bodyContent_txtPassword" onfocus="this.select();"> </td>
</tr>
<tr>
<td> </td>
<td align="center"> <input type="submit" name="ctl00$bodyContent$btnLogin" value="Conferma" onclick="ValidateConfirm(); return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$bodyContent$btnLogin", "", true, "", "", false, false))" id="ctl00_bodyContent_btnLogin"> <input type="submit" name="ctl00$bodyContent$btnEsci" value="Esci" onclick="window.close(); return false;" id="ctl00_bodyContent_btnEsci"> </td>
</tr>
</tbody>
</table>
I tried this, but it does not work:
Document doc = (Document) Jsoup.connect("http://turni.contacts.com/Default.aspx").data("ctl00_bodyContent_txtUser", "user").data("ctl00_bodyContent_txtPassword", "password").data("ctl00_bodyContent_btnLogin","Conferma")
//.cookies(res.cookies()).timeout(0).post();
Usually login into a web site requires two steps -
You send a GET request to get the page, and you extract from there
some values like session ID etc, and the cookies.
You send a POST request with the values from step 1, and your user name and password.
To know which values you need to send, use your browser in the developer mode (by pressing F12) and examine the traffic. Change the user agent string to match your browser, since some sites send different pages to different clients. You can see an example here.

SpringMVC - Send nested form in jsp

I have a form (addPassForm) which is nested in another form (addStoreFrom) in the JSP page. How can I send the addPassForm form to the controller?
When I try to send the addPassForm form, addStoreFrom form sends instead.
<s:url value="/addStore" var="urlAddStore"/>
<form:form id="addStoreFrom" modelAttribute="newStore" action="${urlAddStore}" method="POST">
<table border="1">
<tbody>
<tr>
<td><form:label path="title">Title*</form:label></td>
<td><form:input path="title"/></td>
</tr>
...
<tr>
<s:url value="/addPassForm" var="addPassForm"/>
<form:form id="addPassForm" action="${addPassForm}" method="post">
...
<td>
<input type="submit" value="Add"/>
</td>
</form:form>
</tr>
<tr>
<td><input type="submit" value="Save"/></td>
<td/>
</tr>
</tbody>
</table>
</form:form>
It is just because nested forms in not a valid HTML pattern. The browser simply ignore the inner <form></form> tags and sees only one form. Reference : Is it valid to have a html form inside another html form?
It is not a JSP problem (nor a Java one !), but only a incorrect HTML problem. You must use successive forms instead of nested forms (or user javascript as other suggested)
Example with successive forms :
<s:url value="/addStore" var="urlAddStore"/>
<table border="1">
<tbody>
<form:form id="addStoreFrom" modelAttribute="newStore" action="${urlAddStore}" method="POST">
<tr>
<td><form:label path="title">Title*</form:label></td>
<td><form:input path="title"/></td>
</tr>
...
<tr>
<td><input type="submit" value="Save"/></td>
<td/>
</tr>
</form:form>
<tr>
<s:url value="/addPassForm" var="addPassForm"/>
<form:form id="addPassForm" action="${addPassForm}" method="post">
...
<td>
<input type="submit" value="Add"/>
</td>
</form:form>
</tr>
</tbody>
</table>
Explicitly call submit() function in javascript.Add a function and bind it to onclick of add button.<input type="button" value="Add" onclick="submitAddPassForm()"/>.In javasrcipt simply use:
function submitAddPassForm(){
$('#addPassForm').submit();
}
You can just make a ajax post call instead of a form submit

How to send values to a servlet from a jsp which has directly retrieved values from another jsp

my motive is data flow from modify.jsp to modifysummary.jsp then to servlet to update details but i dont want to use scriptlets in my jsp page.
modify.jsp
<form action="modifysummary.jsp">
<table border="1">
<tr>
<td><label>FirstName</label></td>
<td><input type="text" name="firstname" value="<%=bean.getFirstname() %>"></td>
</tr>
<tr>
<td><label>Surname</label></td>
<td><input type="text" name="surname" value="<%=bean.getSurname() %>" ></td>
</tr>
</form>
modifysummary.jsp
<table>
<tr >
<th>Employee Details</th>
</tr>
<tr>
<td>First Name</td>
<td><%=request.getParameter("firstname") %></td>
</tr>
<tr>
<td>Surname</td>
<td><%=request.getParameter("surname") %></td>
</tr>
<tr>
<td><input type="button" value="BACK" onclick="javascript:history.go(-1)"></td>
<td><input type="submit" value="Modify" name="Modify"></td>
</tr>
</table>
i have written this code inside a form,on clicking the modify button the data should go to servlet in a bean but how to do that.
i think this is not possible without using scriptlets in your modifysummary.jsp . you must set your username and password into request attributes
using request.setattribute();
and get them in servlet
using request.getattribute();
why you don't want to use scriptlets.
and if you want to use requestdispatcher you have to write all that in scriptlets

Categories