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


6 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

Noms 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.

Abhi said...

Thanks for info....
SEO Company in Bangalore