Getting Query String of portal URL from inside the portlet

Recently i had a requirement in which i wanted to send email to users with URL of portal server with some query parameter and when the user clicks on the link the portal page opens us and portlet read the query string in the URL to execute some business logic, so i built this sample portlet using technique described in Accessing underlying HttpServletRequest from PortletRequest in WebSphere Portal Server blog entry. You can download the sample code from here

First i did create a QueryStringPortlet portlet like this


public class QueryStringPortlet extends GenericPortlet{
protected void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
System.out.println("Entering QueryStringPortlet.doView()");
response.setContentType("text/html");
HttpServletRequest httpServletRequest = getHttpServletRequest(request);
String queryString = httpServletRequest.getQueryString();

System.out.println("Value of Query String " + queryString);
request.setAttribute("queryString",queryString);
getPortletContext().getRequestDispatcher("/query.jsp").include(request, response);
System.out.println("Exiting QueryStringPortlet.doView()");
}

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;
}
}


As you can see, the doView() method is quite simple, all that it does is use getHttpServletRequest() method to get underlying HttpServletRequest object from the PortletRequest and then calls its httpServletRequest.getQueryString() method to get the query String. Once i have the query string i am setting it in the request object and forwarding control to JSP for rendering output.

After creating this portlet, i did add it to a page and created a URl mapping to map /querystring url to the page containing portlet, when i go to the /querystring page and some query string to it i see output like this


8 comments:

william said...

I get a casting exception for doing this. java.lang.ClassCastException: com.bea.portlet.container.RenderRequestImpl cannot be cast to javax.servlet.http.HttpServletRequest

Gagandeep Singh said...

This casting can only be done for Websphere i.e. this is applicable for IBM because IBM has implemented it that way.

Anurag J said...

Thanks a lot. It works :)

Anonymous said...

Very good example, it works fine, but one note, when the URL contains myportal, all the parameters vanish and become invisible for the request, did you face this before ?

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