java request.getParameter 在提交按钮上给出空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15992282/
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
request.getParameter on a submit button giving null value
提问by Arpit
On click of Generate PDF/Generate Excel submit button it is requesting the servlet FileUploadto do the processing.Now when I am trying to get the value of the submit button that i have pressed,it is giving me the value as null.I wonder why is it happening?
单击生成 PDF/生成 Excel 提交按钮时,它请求 servlet FileUpload进行处理。现在,当我尝试获取我按下的提交按钮的值时,它给我的值为null。我想知道为什么会这样?
Here is my HTML code:
这是我的 HTML 代码:
<form action="FileUpload" method="post" enctype="multipart/form-data">
<input type="file" id="filename" name="filename"><br>
<input type="button" value="Upload"><br>
<input type="submit" value= "Generate PDF" name="pdf">
<input type = "submit" value="Generate Excel" name="xls">
</form>
This is my servlet code:
这是我的 servlet 代码:
String generatePDF= request.getParameter("pdf");//null
if(generatePDF.equals("Generate PDF"))//Giving NullPointerException at this step
{
System.out.println("generatePDF button pressed");
}
回答by tomor
As far as I can see the problem seems to be with the fact that you have a multipart request being sent to the server. The second answer in this questionseems to have the solution to your problem. Essentially, you have to use the methods provided by FileItem class in the Apache Commons FileUpload package. Or, as suggested here, you may have to use the getPart()
method of the HttpServletRequest class.
据我所知,问题似乎在于您将多部分请求发送到服务器。这个问题的第二个答案似乎可以解决您的问题。本质上,您必须使用 Apache Commons FileUpload 包中 FileItem 类提供的方法。或者,如建议在这里,你可能要使用getPart()
HttpServletRequest的类的方法。
回答by informatik01
UPDATE
更新
Sorry, I initially missed the enctype="multipart/form-data"
part. So, as the user @tomor correctly noted, the reason behind your problem is that you use enctype="multipart/form-data"
. Read his answer.
抱歉,我最初错过了该enctype="multipart/form-data"
部分。因此,正如用户@tomor 正确指出的那样,问题背后的原因是您使用enctype="multipart/form-data"
. 阅读他的回答。
For the more detailed explanation of your issue read the following answer by BalusC: How to upload files to server using JSP/Servlet?.
有关您的问题的更详细说明,请阅读 BalusC 的以下回答:如何使用 JSP/Servlet 将文件上传到服务器?.
The following solution is for the Servlet v3.0. For the older versions, the most popular solution is using Apache Commons FileUpload(see the details in the BalusC answer).
以下解决方案适用于 Servlet v3.0。对于旧版本,最流行的解决方案是使用Apache Commons FileUpload(请参阅 BalusC 答案中的详细信息)。
If you want to check what submit button was pressed, one of the solutions could be naming the submit buttons the sameand then checking the values.
如果您想检查按下了什么提交按钮,解决方案之一可能是将提交按钮命名为相同,然后检查值。
Example:
HTML
示例:
HTML
<form action="FileUpload" method="post" enctype="multipart/form-data">
<input type="file" id="filename" name="filename"><br>
<input type="button" value="Upload"><br>
<input type="submit" value="Generate PDF" name="submitAction">
<input type="submit" value="Generate Excel" name="submitAction">
</form>
Servlet
NB!Pay attention to the @MultipartConfigannotation (available since Servlet 3.0). This way you can correctly process multipart/form-datarequests.
小程序
注意!注意@MultipartConfig注释(从Servlet 3.0 开始可用)。这样您就可以正确处理多部分/表单数据请求。
TestServlet.java
测试服务程序
package com.example;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
@WebServlet(urlPatterns = {"/TestSubmit.do"})
@MultipartConfig
public class TestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String submitAction= request.getParameter("submitAction");
if (submitAction != null) {
if (submitAction.equals("Generate PDF")) {
System.out.println("Generate PDF button pressed");
} else if (submitAction.equals("Generate Excel")) {
System.out.println("Generate Excel button pressed");
}
} else {
// do something about it
}
}
}
NOTE:
Instead of @WebServlet
(again since Servlet 3.0) you can, of course, make servlet configurations inside web.xml. Same for the @MultipartConfig
: instead of this annotation you could add the <multipart-config>
as a child element of the servlet configuration element in the web.xmlfile.
注意:当然,您可以在web.xml 中进行 servlet 配置,
而不是@WebServlet
(再次从 Servlet 3.0 开始)。与:相同,您可以在web.xml文件中添加servlet 作为 servlet 配置元素的子元素,而不是此批注。@MultipartConfig
<multipart-config>
Example (excerpt from web.xml):
示例(摘自web.xml):
<servlet>
<servlet-name>Test Servlet</servlet-name>
<servlet-class>com.example.TestServlet</servlet-class>
<multipart-config>
<!-- In this case empty, but can contain additional nested elements -->
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>Test Servlet</servlet-name>
<url-pattern>/TestSubmit.do</url-pattern>
</servlet-mapping>
More useful links:
更多有用的链接:
- The official Java EE 6 documentaion: Uploading Files with Java Servlet Technology
- An article by BalusC: Uploading files in Servlet 3.0
- Java EE 6 官方文档:使用 Java Servlet 技术上传文件
- BalusC 的一篇文章:在 Servlet 3.0 中上传文件
回答by Romski
You should change your code to test for null first. This will prevent unwanted errors later. Try inspecting the request that is submitted to see what it contains. Firefox has a plugin called tamper data that allows you to catch and edit a request before it is sent. I'm sure there will be others if you search.
您应该先更改代码以测试是否为空。这将防止以后出现不需要的错误。尝试检查提交的请求以查看其中包含的内容。Firefox 有一个名为篡改数据的插件,它允许您在发送请求之前捕获和编辑请求。如果您搜索,我相信还会有其他人。
This will allow you to see the data just before it is sent. Remember that by the time it hits your server it's been through several steps so seeing it early helps. Also consider having a filter to log the request state, parameter names, etc. It can help when first developing and can be easily removed later without intruding on your business code.
这将允许您在发送之前查看数据。请记住,当它到达您的服务器时,它已经经历了几个步骤,因此尽早看到它会有所帮助。还可以考虑使用过滤器来记录请求状态、参数名称等。它可以在第一次开发时提供帮助,并且可以在以后轻松删除而不会干扰您的业务代码。
回答by laksys
If your form has MIME type of multipart/formdatait should be configured either @MultipartConfig annotation in Servlet or in web.xmlas described here
如果您的表单具有 MIME 类型的multipart/formdata,则应按照此处所述在 Servlet 或web.xml 中配置 @MultipartConfig 注释
回答by Pavan
Changing the servlet code
更改 servlet 代码
String generatePDF= request.getParameter("pdf");
to
到
String generatePDF= request.getParameter("filename");
String generatePDF= request.getParameter("filename");
because: attribute name for input type will be the getParameter Value
因为:输入类型的属性名称将是 getParameter 值
`String generatePDF= request.getParameter("filename");
if(generatePDF.equals("Generate PDF")
{
System.out.println("generatePDF button pressed");
}`