无法从 JSP 读取表单值

发布于 2024-10-27 12:52:51 字数 2484 浏览 1 评论 0原文

我有一个 JSP 页面,其中有一个充满表单的表格。当我单击“提交”时,它会转到一个 servlet,该 servlet 应该使用 getParamValues 方法读取调查中表单的值,但它不起作用。当我尝试从 getParamValues 打印出字符串数组时,我不断收到 nullpointerExceptions。

这是相关的 servlet 代码:

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    resp.setContentType("text/plain");
    PrintWriter out = resp.getWriter();
    String[] nums = req.getParameterValues("questNum");
    out.println(nums[2]);
}

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    doPost(req, resp);
}

这是相关的 JSP 代码,在 GAE 上运行:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
 <%@ page import="com.surveycreator.service.persistence.SurveyPersistence" %>
 <%@ page import="com.surveycreator.service.model.Survey" %>
 <%@ page import="com.surveycreator.service.model.SurveyQuestion" %>
 <%@ page import="com.surveycreator.service.model.Survey" %>
 <%@ page import="java.util.List" %>


<html>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>

<body>
<p>View Survey</p>
<form action="/storesurvey" method="post">
<input type="submit" value="Submit Survey" />
</form>




<%
SurveyPersistence persist = new SurveyPersistence();
String surveyName = request.getParameter("content");
Survey survey = persist.getSurvey(surveyName);
List<SurveyQuestion> question = survey.getSurvey();
out.println("<table border='1'><tr><th>QuestionNumber</th><th>Survey Question</th><th>(Yes/yes)(No/no)</th></tr>");
for (SurveyQuestion questionObject : question) {
    int tableCounterString = questionObject.getQuestionNumber();
    String quest = questionObject.getQuestion();
    out.println("<tr><td><form><input id ='question' name='questNum' type='text' readonly='readonly' value='"+tableCounterString + " ' /></form></td><td><form><input id ='question' name='quest' type='text' readonly='readonly' value='"+quest + " ' /></form></td><td><form><input id ='ans' name='answer' type='text' value='' /></form></td></tr>");


}  


%>
<%= survey.getSurveyName()%>

</body>
</html>

I have a JSP page which is a table full of forms. When I click submit it goes to a servlet which is suppose to read the values of the forms in the survey using the getParamValues method but it is not working. When I try to print out the String array from the getParamValues I keep getting nullpointerexceptions.

Here is the relevant servlet code:

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    resp.setContentType("text/plain");
    PrintWriter out = resp.getWriter();
    String[] nums = req.getParameterValues("questNum");
    out.println(nums[2]);
}

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    doPost(req, resp);
}

Here is the relevant JSP code, running on GAE:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
 <%@ page import="com.surveycreator.service.persistence.SurveyPersistence" %>
 <%@ page import="com.surveycreator.service.model.Survey" %>
 <%@ page import="com.surveycreator.service.model.SurveyQuestion" %>
 <%@ page import="com.surveycreator.service.model.Survey" %>
 <%@ page import="java.util.List" %>


<html>
<head>
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
</head>

<body>
<p>View Survey</p>
<form action="/storesurvey" method="post">
<input type="submit" value="Submit Survey" />
</form>




<%
SurveyPersistence persist = new SurveyPersistence();
String surveyName = request.getParameter("content");
Survey survey = persist.getSurvey(surveyName);
List<SurveyQuestion> question = survey.getSurvey();
out.println("<table border='1'><tr><th>QuestionNumber</th><th>Survey Question</th><th>(Yes/yes)(No/no)</th></tr>");
for (SurveyQuestion questionObject : question) {
    int tableCounterString = questionObject.getQuestionNumber();
    String quest = questionObject.getQuestion();
    out.println("<tr><td><form><input id ='question' name='questNum' type='text' readonly='readonly' value='"+tableCounterString + " ' /></form></td><td><form><input id ='question' name='quest' type='text' readonly='readonly' value='"+quest + " ' /></form></td><td><form><input id ='ans' name='answer' type='text' value='' /></form></td></tr>");


}  


%>
<%= survey.getSurveyName()%>

</body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

微凉徒眸意 2024-11-03 12:52:51

根据目前提供的信息,NPE 的唯一可能原因是您根本没有任何


根据 JSP 代码更新:提交按钮确实采用与输入字段不同的 HTML 表单。您需要将提交按钮放在与输入字段相同的 HTML 表单中。根据功能需求(目前尚不清楚),您需要将提交按钮移动到表格单元格中,或者删除表格单元格中的

并包裹整个表格在一个大的内。


与具体问题无关,在这种特殊情况下混合使用 doGet()doPost() 是一种糟糕的做法。如果您通过在浏览器地址栏中输入 URL 来打开 servlet,而不传递任何参数,那么您肯定会得到 NPE。不要让他们也这样做。仅使用 doGet()预处理请求,使用doPost()后处理请求。例如,JSP scriptlet <% %> 中的所有 Java 代码都应在 doGet() 中完成方法和 HTML 代码应通过 JSTL 标记重复。

Based on the information given as far, the only possible cause of a NPE is that you simply don't have any <input>, <select> or <textarea> element with a name="questNum" anywhere in the HTML form which submits to the servlet.


Update as per the JSP code: the submit button is indeed in a different HTML form than the input fields are. You need to put the submit button in the same HTML form as the input field is. Depending on the functional requirement -which is unclear at this point-, you need to move the submit button into the table cell, or to remove the <form> in the table cell and wrap the entire table inside a big <form>.


Unrelated to the concrete problem, mixing doGet() and doPost() is a poor practice in this particular case. If you ever open the servlet by entering its URL in browser address bar, without passing any parameters, then you will definitely get NPEs. Don't let them do the same. Use doGet() only to preprocess requests and use doPost() only to postprocess requests. As an example, all the Java code which you have inside the JSP scriptlet <% %> should be done in the doGet() method and the HTML code should be repeated by JSTL <c:forEach> tag.

七七 2024-11-03 12:52:51

我认为您的代码中有多种形式。当您按下“提交调查”按钮时,除了按钮的值之外,不会有任何值发送到 servlet。

解决方案:(根据您提供的代码)

     <head>
     <link type="text/css" rel="stylesheet"
 href="/stylesheets/main.css" />
     </head>

     <body>
     <p>View Survey</p>
     <form action="/storesurvey" method="post">
     <input type="submit" value="Submit Survey" />

     <%
     SurveyPersistence persist = new SurveyPersistence();
     String surveyName = request.getParameter("content");
     Survey survey = persist.getSurvey(surveyName);
     List<SurveyQuestion> question = survey.getSurvey();
     out.println("<table border='1'><tr><th>QuestionNumber</th><th>Survey Question</th><th>(Yes/yes)(No/no)</th></tr>");
     for (SurveyQuestion questionObject : question) {
         int tableCounterString = questionObject.getQuestionNumber();
         String quest = questionObject.getQuestion();
         out.println("<tr><td><input id
 ='question' name='questNum' type='text' readonly='readonly'
 value='"+tableCounterString + " '
 /></td><td><input id
 ='question' name='quest' type='text' readonly='readonly' value='"+quest + "
 ' /></form></td><td><input id
 ='ans' name='answer' type='text' value='' /></td></tr>");


     }  


     %>
</form> <-----(this is the change)
     <%= survey.getSurveyName()%>

     </body>
     </html>

请注意,我删除了您代码中的一些标签。

尝试一下。如果有bug,请在这里发帖。
希望这有帮助。

内森·潘

I think there are many forms in your code. when you press Submit Survey button, there is no value will be sent to the servlet but the value of button.

Solution: (base on your provided code )

     <head>
     <link type="text/css" rel="stylesheet"
 href="/stylesheets/main.css" />
     </head>

     <body>
     <p>View Survey</p>
     <form action="/storesurvey" method="post">
     <input type="submit" value="Submit Survey" />

     <%
     SurveyPersistence persist = new SurveyPersistence();
     String surveyName = request.getParameter("content");
     Survey survey = persist.getSurvey(surveyName);
     List<SurveyQuestion> question = survey.getSurvey();
     out.println("<table border='1'><tr><th>QuestionNumber</th><th>Survey Question</th><th>(Yes/yes)(No/no)</th></tr>");
     for (SurveyQuestion questionObject : question) {
         int tableCounterString = questionObject.getQuestionNumber();
         String quest = questionObject.getQuestion();
         out.println("<tr><td><input id
 ='question' name='questNum' type='text' readonly='readonly'
 value='"+tableCounterString + " '
 /></td><td><input id
 ='question' name='quest' type='text' readonly='readonly' value='"+quest + "
 ' /></form></td><td><input id
 ='ans' name='answer' type='text' value='' /></td></tr>");


     }  


     %>
</form> <-----(this is the change)
     <%= survey.getSurveyName()%>

     </body>
     </html>

Notice that I removed some tag in your code.

try it out. If there is bug, please post here.
Hope this help.

NathanPhan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文