request.getParameter on a submit button giving null value

℡╲_俬逩灬. 提交于 2019-12-10 18:45:11

问题


On click of Generate PDF/Generate Excel submit button it is requesting the servlet FileUpload to 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?

Here is my HTML code:

<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:

String generatePDF= request.getParameter("pdf");//null
if(generatePDF.equals("Generate PDF"))//Giving NullPointerException at this step
{
  System.out.println("generatePDF button pressed");
}

回答1:


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 question seems 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.




回答2:


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.

For the more detailed explanation of your issue read the following answer by BalusC: How to upload files to server using 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).


If you want to check what submit button was pressed, one of the solutions could be naming the submit buttons the same and then checking the values.

Example:
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 @MultipartConfig annotation (available since Servlet 3.0). This way you can correctly process multipart/form-data requests.

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.xml file.

Example (excerpt from 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



回答3:


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.

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.




回答4:


If your form has MIME type of multipart/formdata it should be configured either @MultipartConfig annotation in Servlet or in web.xml as described here




回答5:


Changing the servlet code

String generatePDF= request.getParameter("pdf");

to

String generatePDF= request.getParameter("filename");

because: attribute name for input type will be the getParameter Value

`String generatePDF= request.getParameter("filename");
if(generatePDF.equals("Generate PDF")
{
 System.out.println("generatePDF button pressed");
}`


来源:https://stackoverflow.com/questions/15992282/request-getparameter-on-a-submit-button-giving-null-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!