Reputation: 5472
I have an html form that asks user details for a curriculum vitae:
<form action = "processdetails.html" method = "post">
<table>
<tr><td style = "font-weight: bold">Personal Details:</td></tr>
<tr>
<td>Name:</td>
<td><input type = "text" name = "applicant"/></td>
</tr>
<tr>
<td>Mobile No.:</td>
<td><input type = "text" name = "mobile"/></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type = "text" name = "email"/></td>
</tr>
</table>
<br/>
<input style = "width: 150px" type = "submit" value = "Generate CV"/>
</form>
After clicking "Generate CV" button, it goes to a Servlet that displays the entered details:
@WebServlet("/processdetails.html")
public class ProcessDetailsServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
String applicantName = "";
String mobileNo = "";
String emailAdd = "";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
if(request.getParameter("applicant") != null) {
applicantName = request.getParameter("applicant");
}
if(request.getParameter("mobile") != null) {
mobileNo = request.getParameter("mobile");
}
if(request.getParameter("email") != null) {
emailAdd = request.getParameter("email");
}
PrintWriter out = response.getWriter();
// other necessary html/css here
out.print("<form action = 'generatepdf.html' method = 'post'>");
out.print("<table>");
out.print("<tr><td style = 'font-weight: bold'>Personal Details:</td></tr>");
out.print("<tr>");
out.print("<td>Name:</td>");
out.print("<td>" + applicantName + "</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td>Mobile No.:</td>");
out.print("<td>" + mobileNo + "</td>");
out.print("</tr>");
out.print("<tr>");
out.print("<td>E-mail:</td>");
out.print("<td>" + emailAdd + "</td>");
out.print("</tr>");
out.print("</table>");
out.print("<br/>");
out.print("<input style = 'width: 150px' type = 'submit' value = 'Generate PDF'/>");
out.print("</form>");
// other html
out.close();
}
}
After clicking the "Generate PDF" button, it jumps to another Servlet:
@WebServlet("/generatepdf.html")
public class GeneratePdfServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
generatePdf();
}
protected void generatePdf() {
System.out.println("This is generatePdf().");
ProcessDetailsServlet pds = new ProcessDetailsServlet();
System.out.println("Name: " + pds.applicantName);
}
}
To check if generatePdf()
is getting the details, I printed it to console.
However, applicantName
is not being printed:
Why is applicantName
not being accessed?
Upvotes: 1
Views: 142
Reputation: 280167
The Servlet container creates a single instance of your Servlet
implementation(s), in this case, one for ProcessDetailsServlet
and one for GeneratePdfServlet
. The servlet container uses these single instances to handle all requests.
When your client sends a POST request to /processdetails.html
, the container will find the ProcessDetailsServlet
instance and invoke its doPost(..)
method (actually its service(..)
method but that invokes doPost(..)
). Your ProcessDetailsServlet
has an instance field
String applicantName = "";
which you set to
applicantName = request.getParameter("applicant");
and then produce a response. When the client submits the other form to /generatepdf.html
, the servlet container finds the GeneratePdfServlet
instance and invokes its doPost()
method and then you do this
ProcessDetailsServlet pds = new ProcessDetailsServlet();
System.out.println("Name: " + pds.applicantName);
You're creating a complete new and unrelated ProcessDetailsServlet
instance with its own applicantName
field that is initialized to ""
. It has absolutely no relation to the other field you set previously, it's a different object!
Upvotes: 1
Reputation: 62
ProcessDetailsServlet
ProcessDetailsServlet
displays resultGeneratePdfServlet
.Well, basically you will not get any of the user details to the GeneratePdfServlet
because you are not submitting any value to the GeneratePdfServlet
when you click on 'Generate PDF'.
User data will not persist for the next request until you manage to save it in a HttpSession
or some place safe.
Your alternative (if you do not want to use HttpSession
) is, you can generate a form using ProcessDetailsServlet
using uneditable input fields instead of table. So next time user clicks on 'Generate PDF', you can resubmit their data and get it in servlet to generate a PDF.
Edit: Only input fields are submitted in form. So table values don't reach the Servlet.
Upvotes: 2
Reputation: 1
I would think it would go something like this...
ProcessDetailsServlet pds = new ProcessDetailsServlet();
System.out.println("Name: " + pds. + applicantName);
Upvotes: -2