Show TOC

Procedure documentationRetrieving Multipart Request Messages Locate this document in the navigation structure

 

Use the AS Java specific API to retrieve multipart messages in your servlet code. Multipart messages originate from HTML forms' POST data. They contain multiple entries within their bodies, each of them resembling another HTTP message. The entries of a multipart message can contain some headers in addition to their body part.

When a request arrives, it is parsed by the Web Container. Then you can obtain the parsed body of the request as an attribute of the HttpServletRequest object. The attribute is an instance of the com.sap.engine.services.servlets_jsp.lib.multipart.MultipartMessage class.

Prerequisites

To use the AS Java specific API for retrieving multipart requests, you must import the com.sap.engine.services.servlets_jsp.lib.multipart package in your servlet code.

Procedure

  1. Obtain the request attribute using the getAttribute() method of the HttpServletRequest object.

    Note Note

    The name of this attribute is com.sap.servlet.multipart.body.

    End of the note.
  2. Retrieve the different parts from the multipart message body.

  3. After you obtain particular multipart message entry, you can obtain the input stream or the reader and read its body.

    Example Example

    Here is a code excerpt that gets the input stream to read the body of the entries of a multipart message:

    1. MultipartMessage multipartMessage =
      request.getAttribute("com.sap.servlet.multipart.body");
      	for (int i = 0; i < multipartMessage.getCount(); i++)
      		MultipartPart part = multipartMessage.getBodyPart(i);
      		String filename = part.getFileName();
      		InputStream partInputStream = part.getInputStream();
      
    End of the code.
  4. (optional) Handle multipart requests of type multipart/form-data.

    If you want the data sent with multipart/form-data type of requests to be included in the parameters set of the request, you must use the addFormParametersToRequest() method of the MultipartMessage object:

    Syntax Syntax

    1. MultipartMessage mm = request.getAttribute("com.sap.servlet.multipart.body");
      	if (mm != null) {
      			mm.addFormParametersToRequest();
      		}
      
    End of the code.

    You can then retrieve the multipart form parameters using the getParameter methods of the ServletRequest object.

    Note Note

    The addFormParametersToRequest() method makes sense only when it is used with requests of type multipart/form-data. It forces the Web Container to parse the request input stream and makes data available as request parameters. Retrieving the body parts of the other types of multipart message is done using the sap.com.servlet.multipart.body request attribute as described above.

    End of the note.