java Servlet request.getParameter() 总是返回“null”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25027714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Servlet request.getParameter() always returning "null"
提问by Trent
I have read through all related questions, trying every accepted answer and I still am finding no luck.
我已经通读了所有相关问题,尝试了每一个接受的答案,但我仍然没有找到运气。
I have a website running on tomcat, with a subpage /Demo/ which has four text fields and a Submit button. The submit button looks as follows
我有一个在 tomcat 上运行的网站,其子页面 /Demo/ 有四个文本字段和一个提交按钮。提交按钮如下所示
<form method="post" action="DemoServlet">
<input type="hidden" name="form_action" value="write" />
<table>
<tr>
<td>
First Name:
</td>
<td>
<input type="text"
name="firstname" />
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input type="text"
name="lastname" id = "lastname" />
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<input type="text"
name="recipient" />
</td>
</tr>
<tr>
<td>
Phone1:
</td>
<td>
<input type="text"
name="phone" />
</td>
</tr>
<tr>
<td>
<input type=button onClick="location.href='../demo-servlet'" value='Submit'/>
</td>
<td>
</td>
</table>
</form>
This /demo-servlet is specified in web.xml as follows
这个 /demo-servlet 在 web.xml 中指定如下
<servlet>
<servlet-name>DemoServlet</servlet-name>
<servlet-class>PACKAGENAME.DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoServlet</servlet-name>
<url-pattern>/demo-servlet</url-pattern>
</servlet-mapping>
This servlet looks as follows
这个 servlet 如下所示
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Retrieve First Name from /Demo/ text field
firstName = request.getParameter("firstname");
// Retrieve Last Name from /Demo/ text field
lastName = request.getParameter("lastname");
/* MORE CODE HERE */
request.getRequestDispatcher("/WEB-INF/confirmation.jsp").forward(request, response);
}
Which then forwards to my confirmation.jp file, showing that the process has succeeded.
然后转发到我的confirmation.jp 文件,表明该过程已成功。
My problem is, the variables and both return the value "null" after the request.getParameter() function is called.
我的问题是,在调用 request.getParameter() 函数后,变量和两者都返回值“null”。
Anyone have a clue why this is happening?
任何人都知道为什么会发生这种情况?
回答by Khary Mendez
The method on your form tag is postbut you have implemented doGetin your servlet. Also the action on your form tag is DemoServletbut should be something like ../demo-servletYou probably don't need onClickat all.
您的表单标签上的方法是post但您已经在您的 servlet 中实现了doGet。此外,您的表单标签上的操作是DemoServlet但应该类似于../demo-servlet您可能根本不需要onClick。