本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

无法从 Java Servlet 获取变量

发布于2023-09-25 20:21     阅读(898)     评论(0)     点赞(21)     收藏(5)


我有一个 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没有被打印:

无名

为什么applicantName没有被访问?


解决方案


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

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

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

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

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



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/677412/8bc5b05febeff9267b2e/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

21 0
收藏该文
已收藏

评论内容:(最多支持255个字符)