gpt4 book ai didi

java - 无法从 Java Servlet 获取变量

转载 作者:太空宇宙 更新时间:2023-11-04 06:43:43 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com