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
I get a casting exception for doing this. java.lang.ClassCastException: com.bea.portlet.container.RenderRequestImpl cannot be cast to javax.servlet.http.HttpServletRequest
ReplyDeleteThis casting can only be done for Websphere i.e. this is applicable for IBM because IBM has implemented it that way.
ReplyDeleteThanks a lot. It works :)
ReplyDeleteVery 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 ?
ReplyDeleteSee my article "Passing query parameters to JSR-286 portlets" here.
ReplyDeleteI 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.
Thanks for info....
ReplyDeleteSEO Company in Bangalore