4.6.5 PortletResponse

The PortletResponse interface extends from the HttpServletResponse and ServletResponse interfaces. This object encapsulates the response sent to the Portal Server for aggregation. Unlike the ServletResponse, the response is sent to the Portal Server, not the client machine directly. Therefore, attempting to influence the overall request, such as setting a status code, will have no effect. Some of the most commonly used methods of this object are listed below:

getWriter
This method returns a java.io.PrintWriter object that can be used to return markup to the Portal Server. The content returned by the PrintWriter is aggregated into the entire portal page. While it is possible to use a PrintWriter as well as include a JSP, it is generally considered bad practice to do so.

encodeNamespace
This method takes a String and attaches the name of the portlet application as a prefix. For example, the value "variable_one" when encoded would be returned as "PC_175_variable_one". Any variables that will become part of the aggregated portal page should be encoded. JavaScript functions and variables are good examples of values that should be encoded to prevent name collisions.

addCookie
This method allows you to add a cookie to the ultimate HTTP response that is sent by the Portal Server to the client. In order to ensure the name of cookie is unique throughout the portal, it is recommended that you use the encodeNameSpace method.

addHeader/setHeader/containsHeader
This method provides access to the headers sent back to the client via the portal server.

encodeURL
This method will append the passed string to the complete URL of the Portal Server. For example, the string "example.gif" becomes "http://www.yourco.com/wps/WPS_PA_351/example.gif" when passed to the encodeURL method.

createURI/createReturnURI
These methods will create URI object that contains a URL pointing the portlet in particular mode. For more information see 4.6.18, PortletURI.

Example 4-3 Working with the PortletResponse

java.io.PrintWriter out = response.getWriter();
out.println("Hello World");
PortletURI uri = response.createURI();
String functionName = response.encodeNamespace("myFunction");


Redbooks
ibm.com/redbooks