PortalContext, PortletContext, PortletConfig and their scope

PortletContext
is similar to servletConext and is used to get <context-param> 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("<paramName>");
}
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.

5 comments:

Surya said...

Superb information guru.....

Tonmoy said...

Is there any xml file which the WindowPresentationContext represents?
Just like the portlet.xml is to the PortletConfig?

Anonymous said...

1 Portlet context does not allow you change? a)portletmode, b)portlet state, c)portlet preferences, d)portlet windows positon please can any one answerfor this question

shravan said...

good

Anonymous said...

If you use DispatcherPortlet and want get custom init-param use following code:
String serviceType = ((PortletConfig) request.getAttribute("javax.portlet.config")).getInitParameter("serviceType");

getted from https://web.liferay.com/community/forums/-/message_boards/message/5588326