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");
}
Changing the servlet code
to
String generatePDF= request.getParameter("filename");
because: attribute name for input type will be the getParameter Value
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 useenctype="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
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
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):
More useful links:
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
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.
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.