Tuesday, November 17, 2009

Portlet Context,Portlet Config and Portal Context

PortletContext

is similar to servletConext and is used to get values declared in web.xml.
Inside doView method of your custom portlet ( implementing GenericPortlet.), you can get context param as below

getPortletContext().getInitParameter("ContextParam")

PortletContext scope is application wide.
If you set any attribute to portletContext, that would be available to all the portlets in that portlet application.


PortletConfig

PortletConfig is similar to ServletConfig and is used to get init parameters declared in portlet.xml. In servlet, we use web.xml for declaring init params and get those using servletConfig.

portletConfig data is attached to a portlet and has a scope to the portlet only.

Code sample to get portlet init parameters....

public void init(PortletConfig portletConfig) throws PortletException{

super.init(portletConfig);

portletConfig.getInitParameter("");

}

PortalContext

this gives information about portal server and other properties...

if you write renderRequest.getPortalContext().getPortalInfo()

this will give you portal name and version as per your env

for me it returned:- IBM WebSphere Portal/6.1

PortalContext has more methods to get properties... you can play with them all as and when needed