Accessing underlying HttpServletRequest from PortletRequest in WebSphere Portal Server

I wanted to figure out if i can access HttpServletRequest object that was passed to the portal server inside the Portlet, and it seems that i can do that using a method like this.

Note:This code is tested only in WebSphere Portal and i am not sure if it will work in the same way in any other standard compliant Portlet Container.


private HttpServletRequest getHttpServletRequest(PortletRequest request){
HttpServletRequest httpServletRequest = (HttpServletRequest)request;
while(httpServletRequest instanceof HttpServletRequestWrapper){
HttpServletRequestWrapper httpServletRequestWrapper =
(HttpServletRequestWrapper)httpServletRequest;
System.out.println("HttpServletRequestWrapper " + httpServletRequestWrapper);
httpServletRequest = (HttpServletRequest)httpServletRequestWrapper.getRequest();
}
return httpServletRequest;
}


The getHttpServletRequest() method takes PortletRequest method and first casts it into the HttpServletRequest object and then checks if its object of HttpServletRequestWrapper type if yes tries to get the wrapped object by calling its getRequest() method, it does this iteratively until it finds the HttpServletRequest object that is not instanceof HttpServletRequestWrapper


[5/9/10 15:48:20:960 PDT] 00000069 SystemOut O HttpServletRequestWrapper com.ibm.ws.portletcontainer.core.impl.RenderRequestImpl@302a302a wrapping com.ibm.wps.engine.PortalRequestWrapper@4be84be8 wrapping com.ibm.wps.engine.ExtendedLocaleRequest@4bbc4bbc wrapping <?xml version="1.0" encoding="UTF-8"?>
<com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper class="com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper">
<contextPath>/wpcert</contextPath>
<servletPath>/mydemo</servletPath>
<parent class="com.ibm.ws.webcontainer.srt.SRTServletRequest">com.ibm.ws.webcontainer.srt.SRTServletRequest@5d765d76</parent>
</com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper>

[5/9/10 15:48:20:961 PDT] 00000069 SystemOut O HttpServletRequestWrapper com.ibm.wps.engine.PortalRequestWrapper@4be84be8 wrapping com.ibm.wps.engine.ExtendedLocaleRequest@4bbc4bbc wrapping <?xml version="1.0" encoding="UTF-8"?>
<com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper class="com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper">
<contextPath>/wpcert</contextPath>
<servletPath>/mydemo</servletPath>
<parent class="com.ibm.ws.webcontainer.srt.SRTServletRequest">com.ibm.ws.webcontainer.srt.SRTServletRequest@5d765d76</parent>
</com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper>

[5/9/10 15:48:20:961 PDT] 00000069 SystemOut O HttpServletRequestWrapper com.ibm.wps.engine.ExtendedLocaleRequest@4bbc4bbc wrapping <?xml version="1.0" encoding="UTF-8"?>
<com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper class="com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper">
<contextPath>/wpcert</contextPath>
<servletPath>/mydemo</servletPath>
<parent class="com.ibm.ws.webcontainer.srt.SRTServletRequest">com.ibm.ws.webcontainer.srt.SRTServletRequest@5d765d76</parent>
</com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper>

[5/9/10 15:48:20:961 PDT] 00000069 SystemOut O HttpServletRequestWrapper <?xml version="1.0" encoding="UTF-8"?>
<com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper class="com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper">
<contextPath>/wpcert</contextPath>
<servletPath>/mydemo</servletPath>
<parent class="com.ibm.ws.webcontainer.srt.SRTServletRequest">com.ibm.ws.webcontainer.srt.SRTServletRequest@5d765d76</parent>
</com.ibm.wps.mappingurl.impl.MappingURLRequestWrapper>


This is output that i see when i call getHttpServletRequest() method from the doView() method of my sample portlet

5 comments:

anu said...

hey

thanks. very nice post.it helped me a lot.thanks again

johnD said...

See my article "Passing query parameters to JSR-286 portlets" here.

I know this is a supported way to do it. The solution presented here depends on a particular implementation which may change in the future, i.e. no guarantees.

Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer

srjwebsolutions said...


We are leading responsive website designing and development company in Noida.
We are offering mobile friendly responsive website designing, website development, e-commerce website, seo service and sem services in Noida.

Responsive Website Designing Company in Noida
Website Designing Company in Noida
SEO Services in Noida
SMO Services in Noida

EG MEDI said...

Egmedi.com is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery


Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store