request.getParameter on a submit button giving nul

2019-07-05 03:03发布

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");
}

5条回答
相关推荐>>
2楼-- · 2019-07-05 03:06

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");
}`
查看更多
老娘就宠你
3楼-- · 2019-07-05 03:19

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:

查看更多
▲ chillily
4楼-- · 2019-07-05 03:23

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楼-- · 2019-07-05 03:30

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.

查看更多
Juvenile、少年°
6楼-- · 2019-07-05 03:31

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.

查看更多
登录 后发表回答