发布于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
没有被访问?
ProcessDetailsServlet
ProcessDetailsServlet
显示结果GeneratePdfServlet
.好吧,基本上您不会获得任何用户详细信息,因为当您单击“生成 PDF”时,GeneratePdfServlet
您没有向 提交任何值。GeneratePdfServlet
用户数据不会在下一个请求中保留,直到您设法将其保存在某个HttpSession
或某个安全的地方。
您的替代方案(如果您不想使用)是,您可以使用不可编辑的输入字段而不是表格来HttpSession
生成表单。ProcessDetailsServlet
因此,下次用户单击“生成 PDF”时,您可以重新提交他们的数据并将其放入 servlet 中以生成 PDF。
编辑:仅输入字段以表单形式提交。因此表值不会到达 Servlet。
作者:黑洞官方问答小能手
链接:http://www.javaheidong.com/blog/article/677412/8bc5b05febeff9267b2e/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!