Interacting with the HTTP request and response objects

In this topic ...

Getting Data from the Request

Modifying the Response

Related Topics ...

Overview: Working with Java

Factory API Documentation

 

Getting Data from the Request

We can access the HttpServletRequest object by calling the webAppAccess.getHttpServletRequest() method. You can retrieve the following information from the HttpServletRequest object:

  • User name -- webAppAccess.getHttpServletRequest().getUserPrincipal().getName();

  • Server name -- webAppAccess.getHttpServletRequest().getServerName();

  • ContentType -- webAppAccess.getHttpServletRequest().getContentType();

  • Locale -- webAppAccess.getHttpServletRequest().getLocale();

See the Javadoc for the javax.servlet.http.HttpServletRequest class in your JDK installation for a complete description of the public methods.

 

Modify the Response

We can modify the content type of the data returned by a method by using the HttpServletResponse object to set the ContentType of the response to something other than text/html. We can also add headers to the response or otherwise modify the response to the client.

For example, the following code sample sets the ContentType of the response to return an Microsoft Excel spreadsheet:

  public void setContentTypeXL(WebAppAccess webAppAccess) {

     webAppAccess.getHttpServletResponse().setContentType("application/msexcel");

      try {

      File excelSheet = new File(webAppAccess.getSystemProperties().getDocumentRoot()+

         File.separator + "Schedule.xls");

      int numFileBytes = (int)excelSheet.length();

      byte[] fileBytes = new byte[numFileBytes];

      BufferedInputStream bis = new BufferedInputStream(new FileInputStream(excelSheet));

      int i = 0;

      while(i < numFileBytes){

        fileBytes[i] = (byte)bis.read();

        i++;

      }

      webAppAccess.getHttpServletResponse().getOutputStream().write(fileBytes);

     }

    catch (IOException ioe) {

      System.out.println(ioe+"Cannot find file.");

    }

  }

Note: You can find the supported MIME types by looking at the <mime-mapping /> elements in the Factory's WEB-INF/web.xml file.

See the Javadoc for the javax.servlet.http.HttpServletResponse class in your JDK installation for a complete description of the public methods.