Show TOC Start of Content Area

Procedure documentation Retrieving Multipart Request Messages  Locate the document in its SAP Library structure

Use

Use the J2EE Engine 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 J2EE Engine 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

By default, the name of this attribute is com.sap.servlet.multipart.body. However, it is configurable and you can change the name of the attribute using the Web Container Service. For more information, see Configuring the Name of the Multipart Body Request Attribute in the Administration Manual.

       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

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

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();

Handling 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:

MultipartMessage mm = request.getAttribute("com.sap.servlet.multipart.body");

   if (mm != null) {

         mm.addFormParametersToRequest();

      }

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

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.

See also:

 

For more information and additional methods for multipart requests retrieval, see the API documentation of the com.sap.engine.services.servlets_jsp.lib.multipart package in the Help menu of the SAP NetWeaver Developer Studio.

 

End of Content Area