Portlet Context


·    The PortletContext interface defines a portlet’s view of the portlet application within which the portlet is running. Using the PortletContext object, a portlet can log events, obtain portlet application resources, application and portlet runtime options and set and store attributes that other portlets and servlets in the portlet application can access. If you need  an object to be shared/used  by all users in portal, stored the object in portletcontext attribute.

·         The portlet context leverages most of its functionality from the servlet context of the portlet application. However, the context objects themselves may be different objects.

·         The context-wide initialization parameters are the same as initialization parameters of the servlet context and the context attributes are shared with the servlet context. Therefore, they must be defined in the web application deployment descriptor (the web.xml file).The initialization parameters accessible through the PortletContext must be the same that are accessible through the ServletContext of the portlet application.

Note: Portlet context attributes can be set by both ways i.e programmatically or declaratively. If you choose declaratively way, context attributes can be declared in web.xml only, not in portlet.xml.

Sample Code:

Set the portletcontext attribute in web.xml

            <?xml version="1.0" encoding="UTF-8"?>
      <web-app id="WebApp_ID" version="2.5"     xmlns="http://java.sun.com/xml/ns/javaee"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <display-name>PortletContext</display-name>
      <context-param>
      <param-name>Admin</param-name>
      <param-value>Admin</param-value>
      </context-param>
      <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
            <welcome-file>default.html</welcome-file>
            <welcome-file>default.htm</welcome-file>
            <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
</web-app>

Reading attribute in portlet

package com.jsrtutorial.portletcontext;
import java.io.*;
import java.util.Enumeration;
import javax.portlet.*;

public class PortletContext extends GenericPortlet {

            public static final String JSP_FOLDER    = "/_PortletContext/jsp/";                public static final String VIEW_JSP      = "PortletContextView";        

            public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
                        // Set the MIME type for the render response
                        response.setContentType(request.getResponseContentType());
                        javax.portlet.PortletContext context=getPortletConfig().getPortletContext();
                        Enumeration names=context.getInitParameterNames();
                        while(names.hasMoreElements())
                        {
                                    String parametername=(String)names.nextElement();
                                    String parametervalue=context.getInitParameter(parametername);
                                    System.out.println(parametername+ "="+parametervalue);
                        }
                        // Invoke the JSP to render
                        PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(getJspFilePath(request, VIEW_JSP));
                        rd.include(request,response);
            }

            private static String getJspFilePath(RenderRequest request, String jspFile) {
                        String markup = request.getProperty("wps.markup");
                        if( markup == null )
                                    markup = getMarkup(request.getResponseContentType());
                        return JSP_FOLDER + markup + "/" + jspFile + "." + getJspExtension(markup);
            }

            private static String getMarkup(String contentType) {
                        if( "text/vnd.wap.wml".equals(contentType) )
                                    return "wml";
        else
            return "html";
            }

            private static String getJspExtension(String markupName) {
                        return "jsp";
            }

}


Ouput :

[11/26/11 18:17:45:747 PST] 00000035 SystemOut     O Admin=Admin

Click here to download the sample code