k_rollo
k_rollo

Reputation: 5472

Cannot get variable from Java Servlet

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:

no_name

Why is applicantName not being accessed?

Upvotes: 1

Views: 142

Answers (3)

Sotirios Delimanolis
Sotirios Delimanolis

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

Sharad Tank
Sharad Tank

Reputation: 62

  1. When you click on 'Generate CV', your form gets submitted to ProcessDetailsServlet
  2. ProcessDetailsServlet displays result
  3. When you click on 'Generate PDF' you again submit to GeneratePdfServlet.

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

Rapt0R
Rapt0R

Reputation: 1

I would think it would go something like this...

ProcessDetailsServlet pds = new ProcessDetailsServlet();
System.out.println("Name: " + pds. + applicantName);

Upvotes: -2

Related Questions