Discussions

Web tier: servlets, JSP, Web frameworks: Servlets, HTML forms, multipart/form-data

  1. Servlets, HTML forms, multipart/form-data (5 messages)

    When I submit this form, the FooServlet receives an HttpServletRequest with a Content Type value of multipart/form-data

    The problem is that the "submitter" attribute and the "serviceClass" attribute are not set. The servlet has no way to read the serviceClass value.

    Perhaps the web browser does not send the serviceClass parameter in the HTTP request?

    Any advice?

    The client web browser is Internet Explorer 6.0 SP1

    <FORM ACTION="/servlet/FooServlet"
          ENCTYPE="multipart/form-data"
          METHOD=POST>

    What is your name? <INPUT TYPE=TEXT NAME=submitter><br>

    What file are you sending? <INPUT TYPE=FILE NAME=secretDocument><br>

    <input type=hidden name="serviceClass" value="FoobarServiceMgr">

    <input type="submit" value=Submit><br>

    </FORM>
  2. Situation:

    javax.servlet.HttpServletRequest.getParameter(String) returns null when the ContentType is multipart/form-data

    Solutions:

    Solution A:
    1. download http://www.servlets.com/cos/index.html
    2. invoke getParameters() on com.oreilly.servlet.MultipartRequest

    Solution B:
    1. download http://jakarta.apache.org/commons/sandbox/fileupload/
    2. invoke readHeaders() in
    org.apache.commons.fileupload.MultipartStream

    Solution C:
    1. download http://users.boone.net/wbrameld/multipartformdata/
    2. invoke getParameter on
    com.bigfoot.bugar.servlet.http.MultipartFormData

    Solution D:
    1. Use Struts. Struts 1.1 handles this automatically

  3. Solution E: use TICL (www.kobrix.com), it handles it automatically too...;)
  4. There is also a better way for the Solution B (easy to use). The steps involved are as follows:

    1. Download one of the versions of UploadFile from http://jakarta.apache.org/commons/fileupload/

    2. Invoke parseRequest(request) on org.apache.commons.fileupload.FileUploadBase which returns list of org.apache.commons.fileupload.FileItem objects.

    3. Invoke isFormField() on each of the above FileItem objects. This determines whether the file item is a form paramater or stream of uploaded file.

    4. Invoke getFieldName() to get parameter name and getString() to get parameter value on FileItem if it's a form parameter. Invoke write(java.io.File) on FileItem to save the uploaded file stream to a file if the FileItem is not a form parameter.
  5. I have downloaded multipartrequest.jar and cos.jar  and put it into WEB-INF lib folder.

    Now how to invoke getParameter in my jsp page?

    I have created 1 jsp "main.jsp" in which there is a form 

    like 

    <form  action="upload.jsp"  method="post" enctype="multipart/form-data">....input fileds with upload file data..</form>

    Now on upload.jsp through "org.apache.commons.fileupload" I have saved my uploaded file into server + here I want to get the other fields from "main.jsp" and insert them into database(mysql)

    As I was using multipart,it was inserting null values to db, nw as per 1 of ur solution 

    I have downloaded multipartrequest.jar and cos.jar  and put it into WEB-INF lib folder.Now how to invoke getParameter in my jsp page?

    Response will be appriciated.

     

  6. I have downloaded multipartrequest.jar and cos.jar  and put it into WEB-INF lib folder.

    Now how to invoke getParameter in my jsp page?

    I have created 1 jsp "main.jsp" in which there is a form 

    like 

     

    ....input fileds with upload file data..

     

    Now on upload.jsp through "org.apache.commons.fileupload" I have saved my uploaded file into server + here I want to get the other fields from "main.jsp" and insert them into database(mysql)

    As I was using multipart,it was inserting null values to db, nw as per 1 of ur solution 

    I have downloaded multipartrequest.jar and cos.jar  and put it into WEB-INF lib folder.Now how to invoke getParameter in my jsp page?

    Response will be appriciated.