Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法从JSP读取表单值_Jsp_Servlets - Fatal编程技术网

无法从JSP读取表单值

无法从JSP读取表单值,jsp,servlets,Jsp,Servlets,我有一个JSP页面,它是一个充满表单的表格。当我单击submit时,它会转到一个servlet,该servlet应该使用getParamValues方法读取调查中表单的值,但它不起作用。当我试图从getParamValues打印字符串数组时,我总是得到NullPointerException 以下是相关的servlet代码: public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOExceptio

我有一个JSP页面,它是一个充满表单的表格。当我单击submit时,它会转到一个servlet,该servlet应该使用getParamValues方法读取调查中表单的值,但它不起作用。当我试图从getParamValues打印字符串数组时,我总是得到NullPointerException

以下是相关的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);
}
以下是在GAE上运行的相关JSP代码:

<%@ 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>

视图调查


根据目前给出的信息,NPE的唯一可能原因是,在提交给servlet的HTML表单中,没有任何
元素带有
name=“questNum”


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



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

我认为代码中有许多形式。当您按下Submit Survey按钮时,除了按钮的值之外,不会向servlet发送任何值

解决方案:(基于您提供的代码)


视图调查


该链接会导致HTTP 500错误页面,并且无论如何都不是特别有用。只是张贴相关的源代码。很抱歉,链接不起作用,这里是JSP的代码,我应该提到的一切是在谷歌应用程序引擎。
     <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>