concept PortletContext in category java

This is an excerpt from Manning's book Portlets in Action.
The portlet container is responsible for invoking lifecycle methods on the portlet instances and providing them with the required runtime environment. A portlet container is an extension to a servlet container; it provides what a servlet container provides, and it additionally manages the portlet instances. The portlets access their runtime environment using the PortletContext object (similar to ServletContext), which allows portlets to share data with other portlets and with servlets in the same portlet application.
Portlet containers provide the PortletSession object so you can store user-specific data across portlet requests. PortletSession is similar to HttpSession, and it provides methods to maintain session attributes, get and set the maximum interval of time for which the session can remain inactive, get PortletContext, and so on. Just as each web application has a unique HttpSession per user, each portlet application has a unique PortletSession per user.
PortletContext provides portlets with access to its environment, which includes the portlet application and the portlet container. Each portlet application has its own unique PortletContext; if you have portlets in different portlet applications, they don’t share attributes set in the PortletContext. You saw earlier that you can use PortletContext to obtain a PortletRequestDispatcher to generate portlet content by including or forwarding portlet requests to a JSP page using the PortletRequestDispatcher object’s getRequestDispatcher method. In this section, we’ll look at some of the important methods of the PortletContext interface.
Table 3.6 describes some of the important PortletContext methods.
Table 3.6. Important PortletContext methods
Method
Description
getAttribute, setAttribute, removeAttribute Get, set, and remove context attributes. The context attributes that are available to portlets through PortletContext are also accessible to JSPs and servlets (that form part of the same portlet application) through ServletContext, and vice versa. getInitParameter Obtains the context initialization parameters. Context initialization parameters are different from portlet initialization parameters in the sense that they’re available to all portlets in the same portlet application. Context initialization parameters are specified in web.xml using the contextparam element. getContainerRuntimeOptions Returns an enumeration of portlet container-runtime options supported by the portlet container. The return type is Enumeration<String>. getNamedDispatcher Accepts the servlet name to which the returned RequestDispatcher refers. getMimeType Accepts a filename as an argument and returns its MIME type (such as text/html and application/pdf). A portlet application is also a web application, and like any web application it has a ServletContext. Like HttpSession and PortletSession, PortletContext and ServletContext mirror attributes stored in the context and they follow the same lifecycle. If you store an attribute in PortletContext, it’s available to ServletContext, and vice versa. If the ServletContext is destroyed by the servlet container, the PortletContext is also destroyed by the portlet container. Because PortletContext mirrors the ServletContext attributes and follows the same lifecycle, you can use the following ServletContext listeners for your PortletContext: ServletContextListener and ServletContextAttributeListener.