无法从Java Servlet获取变量

时间:2014-06-21 05:19:09

标签: java variables servlets

我有一个html表单,询问用户详细信息的简历:

<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>

点击&#34;生成简历&#34;按钮,它转到显示输入细节的Servlet:

@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();
    }
}

点击&#34;生成PDF&#34;按钮,它跳转到另一个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);       
    }
}

要检查generatePdf()是否正在获取详细信息,我将其打印到控制台 但是,applicantName未被打印:

no_name

为什么applicantName没有被访问?

3 个答案:

答案 0 :(得分:2)

  1. 当您点击“生成简历”时,您的表单会提交至ProcessDetailsServlet
  2. ProcessDetailsServlet显示结果
  3. 当您点击&#39;生成PDF&#39;您再次提交至GeneratePdfServlet
  4. 好吧,基本上您不会向GeneratePdfServlet收到任何用户详细信息,因为当您点击“生成PDF&#39;”时,您没有向GeneratePdfServlet提交任何值。

    在您设法将其保存在HttpSession或某个安全的地方之前,用户数据将不会保留用于下一个请求。

    您的替代方案(如果您不想使用HttpSession)是,您可以使用不可编辑的输入字段而不是表格使用ProcessDetailsServlet生成表单。因此,下次用户点击“生成PDF”时,您可以重新提交数据并将其放入servlet中以生成PDF。

    编辑: 只有输入字段以表格形式提交。因此,表值不会到达Servlet。

答案 1 :(得分:1)

Servlet容器创建Servlet实现的单个实例,在这种情况下,一个用于ProcessDetailsServlet,一个用于GeneratePdfServlet。 servlet容器使用这些单个实例来处理所有请求。

当您的客户端向/processdetails.html发送POST请求时,容器将找到ProcessDetailsServlet实例并调用其doPost(..)方法(实际上是service(..)方法但调用doPost(..)方法{1}})。您的ProcessDetailsServlet有一个实例字段

String applicantName = "";

你设置为

applicantName = request.getParameter("applicant");

然后产生回复。当客户端将另一个表单提交给/generatepdf.html时,servlet容器会找到GeneratePdfServlet实例并调用其doPost()方法然后执行此操作

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

您正在创建一个完整的无关 ProcessDetailsServlet实例,其中包含自己的applicantName字段,该字段已初始化为{{1 }}。它与您之前设置的其他字段完全没有关系,它是一个不同的对象!

答案 2 :(得分:-2)

我认为它会像这样......

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