0

My project concept is to upload file to data base. The below code is working fine but the problem is I can't get the <select> value from one page to another it always show null value.

How to get the <select> element value from one JSP to another JSP during file uploading time?

  <FORM ENCTYPE="multipart/form-data" ACTION="upload_page.jsp" METHOD=POST>
       <center>
       <table border="0" bgcolor=#ccFDDEE>
       <tr><center><td colspan="2" align="center"><B>UPLOAD THE FILE</B>
      <center></td></tr>
       <tr><td colspan="2" align="center"> </td></tr>
       <tr><td><b>Choose the file To Upload:</b></td><td><INPUT NAME="file" TYPE="file"> </td></tr>
       <tr><td colspan="2" align="center"> </td></tr>
       <tr><td colspan="2" align="center"><input type="submit" value="Send File">            </td></tr>
       <tr>
       <td>
       <select name="t1">
      <option value="mars">Mars</option>
      <option value="moon">Moon</option>
      <option value="sun">Sun</option>
      <option value="earth">Earth</option>
      </select>
      </td>

******** 
<% String s1=request.getParameter("t1"); System.out.println(s1); 
********
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Giri
  • 127
  • 2
  • 5
  • 15

1 Answers1

2

You're submitting the form in multipart/form-data encoding. This is different from default application/x-www-form-urlencoded encoding which getParameterXxx() methods by default use. They would all return null on multipart/form-data because multipart/form-data is by default not supported.

Basically, you need to use the very same API to extract the regular form fields as you're currently using to extract the uploaded file. Usually this is Apache Commons FileUpload, so I'm assuming that you're also using that. You should have somewhere an if (!fileItem.isFormField()) check to grab the file field. You need to hook on the else to grab the normal form field.

Alternatively, if you're already on Servlet 3.0, submit the form to a normal servlet with the @MultipartConfig annotation and use getPart() method to get the file field and you can continue using getParameterXxx() methods to get the normal form field.

See also:


Unrelated to the concrete problem, using 90's style uppercased HTML tag/attribute names and using the since 1998 deprecated <center> element and using discouraged styling attributes of HTML table elements instead of CSS doesn't give me the impression that you're learning HTML based on proper and up to date resources. Ensure that you're doing that. Also, submitting the form to a JSP and using scriptlets is disoucraged since a decade. Ensure that you're also reading sane JSP resources.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555