Example - handling incoming requests

A servlet relies on the Web server for accepting and handling incoming requests. Once the Web server has decided that the request is for an MQe servlet, it passes the request to MQe using the doPost() method. The following code handles this request:

C example
/**
 * Handle POST......
 */
public void doPost(HttpServletRequest request,
                   HttpServletResponse response) 
              throws IOException
{
  // any request to process ?
  if (request == null)
    throw new IOException("Invalid request");
  try
  {
    int max_length_of_data = request.getContentLength();     
    // data length
    byte[] httpInData = new byte[max_length_of_data];        
    // allocate data area
    ServletOutputStream httpOut = response.getOutputStream();
    // output stream
    ServletInputStream  httpIn  = request.getInputStream();  
    // input stream

    // get the request
    read( httpIn, httpInData, max_length_of_data);

    // process the request
    byte[] httpOutData = channelManager.process(null, httpInData);

    // appears to be an error in that content-
      length is not being set
    // so we will set it here
    response.setContentLength(httpOutData.length);
    response.setIntHeader("content-length", httpOutData.length);

    // Pass back the response
    httpOut.write(httpOutData);
  }
  catch (Exception e)
  {
    // pass it on ...
    throw new IOException( "Request failed" + e );
  }
}

This method:

  1. Reads the http input data stream into a byte array. The input data stream may be buffered so the read() method is used to ensure that the entire data stream is read before continuing.
    Note: MQe only handles requests with the doPost() method, it does not accept requests using the doGet() method
  2. The request is passed to MQe through a connection manager. From this point, all processing of the request is handled by core MQe classes such as the queue manager.
  3. Once MQe has completed processing the request, it returns the result wrapped in http headers as a byte array. The byte array is passed to the Web server and is transmitted back to the client that originated the request.

Terms of use | WebSphere software

(c) Copyright IBM Corporation 2004, 2005. All rights reserved.