request.getParameter on a submit button giving null value

Arpit picture Arpit · Apr 13, 2013 · Viewed 18.5k times · Source

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

Answer

tomor picture tomor · Apr 14, 2013

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.