WAS v8.5 > Develop applications > Develop Portlet applications > Portlet aggregation and preferences

Portlet preferences

Preferences are set by portlets to store customized information. By default, the PortletServingServlet servlet stores the portlet preferences for each portlet window in a cookie. However, we can change the location to store them in either a session, an .xml file, or a database.


Storing portlet preferences in cookies

The attributes of the cookie are defined as follows:

Path

context/portlet-name/portletwindow
Name:

The name of the cookie has the fixed value of PortletPreferenceCookie.
Value

The value of the cookie contains a list of preferences by mapping to the following structure:

    *['/' pref-name *['=' pref-value]]

    All preferences start with '/' followed by the name of the preference. If the preference has one or more values, the values follow the name separated by the '=' character. A null value is represented by the string '#*!0_NULL_0!*#'. As an example, the cookie value may look like, /locations=raleigh=boeblingen/regions=nc=bw


    Customize the portlet preferences storage

    We can override how the cookie is handled to store preferences in a session, an .xml file or database. To customize the storage, create a filter, servlet or JSP file as new entry point that wraps the request and response before calling the portlet. Examine the following example wrappers to understand how to change the behavior of the PortletServingServlet to store the preferences in a session instead of cookies.

    The following is an example of how the main servlet manages the portlet invocation.

    public class DispatchServlet extends HttpServlet
    {
        ...
      public void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException
      {
     response.setContentType("text/html");
     
     // create wrappers to change preference storage
     RequestProxy req = new RequestProxy(request);
     ResponseProxy resp = new ResponseProxy(request, response);
    
     // create url prefix to always return to this servlet
                    ...
                    req.setAttribute("com.ibm.wsspi.portlet.url.prefix", urlPrefix);
            
     // prepare portlet url
     String portletPath = request.getPathInfo(); 
                    ...
    
     // include portlet using wrappers
     RequestDispatcher rd = getServletContext().getRequestDispatcher(modifiedPortletPath);
                    rd.include(req, resp);
      }}

    In the following example, the request wrapper changes the cookie handling to retrieve the preferences out of the session.

    public class RequestWrapper extends HttpServletRequestWrapper 
    {
        ...
        public Cookie[] getCookies() {
            Cookie[] cookies = (Cookie[]) session.getAttribute("SessionPreferences");
            return cookies;
        }}

    In the following example, the response wrapper changes the cookie handling to store the preferences in the session:

    public class ResponseProxy extends HttpServletResponseWrapper 
    {
       ...
        public void addCookie(Cookie cookie) {
            Cookie[] oldCookies = (Cookie[]) session.getAttribute("SessionPreferences");
            int newPos = (oldCookies == null) ? 0 : oldCookies.length;
            Cookie[] newCookies = new Cookie[newPos+1];
            session.setAttribute("SessionPreferences", newCookies);
    
            if (oldCookies != null) {
            System.arraycopy(oldCookies, 0, newCookies, 0, oldCookies.length);
            } 
            newCookies[newPos] = cookie;
        }}


    Related concepts:

    Portlet aggregation using JSP
    Portlets
    Portlet container


    Related information:

    JSR-000168 Portlet Specification


    +

    Search Tips   |   Advanced Search