How to get HttpServletRequest and HttpServletResponse from
portlet request
We can only have portletRequest in the portlet. If we are
setting anything in the HttpServletRequest it is not accessible in the portlet.
IBM provides an utility class which gives which converts the “portletRequest”
to HttpServletRequest.
There are some scenarios when you want to get httpServletRequest
or httpServletReponse from portletRequest.
PortletUtil class available in the com.ibm.ws.portletcontainer.portlet
package in “com.ibm.ws.portletcontainer_6.1.0.jar” at “WebSphere/AppServer/plugins/”
on server, through which you can directly get httpServletRequest,
httpServletResponse, ServletContext and many more.
Code to get HttpServletRequest from
portletRequest/renderRequest/actionRequest and HttpServletResponse from portletResponse/renderResponse/actionResponse:
import javax.portlet.ActionRequest;
import javax.portlet.ActionRequest;
import
javax.portlet.ActionResponse;
import
javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import
javax.portlet.RenderRequest;
import
javax.portlet.RenderResponse;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
//from
wp.engine.impl.jar
import
com.ibm.wps.engine.ExtendedLocaleRequest;
//from
wp.engine.impl.jar
import
com.ibm.wps.engine.PortalRequestWrapper;
// from
WAS_HOME\plugins\com.ibm.ws.portletcontainer_6.1.0.jar
import
com.ibm.ws.portletcontainer.portlet.PortletUtils;
public class
RequestRevealer {
public static
HttpServletResponse getResponse (PortletResponse portletResponse) {
HttpServletResponse httpServletResponse;
If (portletResponse instanceof ActionResponse) {
httpServletResponse =
PortletUtils.getHttpServletResponse((ActionResponse)
portletResponse);
} else if (portletResponse instanceof RenderResponse) {
httpServletResponse =
PortletUtils.getHttpServletResponse((RenderResponse) portletResponse);
} else {
throw new RuntimeException ("Wrong
type...:"+ portletResponse);
}
return httpServletResponse;
}
public static HttpServletRequest getRequest
(PortletRequest portletRequest) {
HttpServletRequest httpServletRequest;
If (portletRequest instanceof
ActionRequest) {
httpServletRequest =
PortletUtils.getHttpServletRequest((ActionRequest)portletRequest);
} else if (portletRequest instanceof
RenderRequest) {
httpServletRequest =
PortletUtils.getHttpServletRequest((RenderRequest) portletRequest);
} else {
throw new RuntimeException ("Wrong
type...:"+ portletRequest);
}
PortalRequestWrapper portalRequestWrapper = (PortalRequestWrapper) httpServletRequest;
ExtendedLocaleRequest
extendedLocaleRequest =
(ExtendedLocaleRequest) portalRequestWrapper.getRequest();
HttpServletRequest response = (HttpServletRequest) extendedLocaleRequest.getRequest();
return response;
}
}
There are couple of utility methods available in PortletUtil which gives you ServletContext, ServletConfig, PortletWindow, PortletApplicationDefinition etc.
There are couple of utility methods available in PortletUtil which gives you ServletContext, ServletConfig, PortletWindow, PortletApplicationDefinition etc.