java - 无法从 Java Servlet 获取变量

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

点击“生成简历”按钮后,它会转到显示输入详细信息的 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();
    }
}

点击“生成PDF”按钮后,跳转到另一个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 无法被访问?

最佳答案

  1. 当您点击“生成简历”时,您的表单将提交至 ProcessDetailsS​​ervlet
  2. ProcessDetailsS​​ervlet 显示结果
  3. 当您点击“生成 PDF”时,您将再次提交给 GeneratePdfServlet

嗯,基本上您不会获得 GeneratePdfServlet 的任何用户详细信息,因为当您单击“生成 PDF”时,您没有向 GeneratePdfServlet 提交任何值。

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

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

编辑: 仅输入字段以表单形式提交。因此表值不会到达 Servlet。

关于java - 无法从 Java Servlet 获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24338551/

相关文章:

java - 如何使用流过滤器和求和

c++ - 是否可以检查变量的存在?

Javascript变量随onclick变化

jsp - 指定我的Java Web应用程序的Servlet和JSP版本

java - servlet 版本 getServletContext().getEffectiveMajorVersion() 给了我 2.5,而我正在使用 3

java - 游戏框架中的订单相关实体

java - 系统清除属性不起作用。怎么会这样?

java - 使用 Java Pattern Regex 从 WebViewClient 中解码的 mailto 中提取信息

java - 通过循环动态赋值 - Android - 通过java编程

java - 使用过滤器在登录尝试失败后重定向