Portlet Factory, Version 6.1.2


 

Modifying the response

Use the HttpServletResponse object to modify the content type of the data returned by a method.

This object sets the ContentType of the response to something other than text or html.

You 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 IBM® WebSphere Portlet Factory 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.

Parent topic: About getting data from the request Parent topic: Overview: working with Java


Library | Support |