How to get HttpServletRequest and HttpServletResponse from portlet request

There are some scenario when you want to get httpServletRequest or httpServletReponse from portletRequest.

IBM provides an utility class which gives you what you are looking for.

Yes, there is a PortletUtil class available in the com.ibm.ws.portletcontainer.portlet package, through which you can directly get httpServletRequest, httpServletResponse, ServletContext and many  more servlet object you want.


Code to get HttpServletRequest from  portletRequest/renderRequest/actionRequest.

HttpServletRequest httpServletRequest =  PortletUtils.getHttpServletRequest(renderRequest);

There are couple of utility methods available in PortletUtil which  gives you ServletContext, ServletConfig, PortletWindow, PortletApplicationDefinition etc.

4 comments:

Anonymous said...

why we need this?can u give me a scenario...

Reema John said...

A scenario would be when you need the HttpServletRequest or HttpServletResponse to be sent as parameters to an interface say to get the Portal State Manager.So at that time interface needs only HttpServletResponse and HttpServletResponse Object.So you can get this as mentioned above.If you try and Typecast the PortletRequest and PortletResponse to HttpServletResponse then you get a Class cast exception.So the best way is as mentioned above.

ViMAL GOSAR said...

How to get Servlet Context from JSR 286 Portlet.

ViMAL GOSAR said...

How to get servlet context in JSR 286 Portlet any package which we can use?