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:
I get a casting exception for doing this. java.lang.ClassCastException: com.bea.portlet.container.RenderRequestImpl cannot be cast to javax.servlet.http.HttpServletRequest
This casting can only be done for Websphere i.e. this is applicable for IBM because IBM has implemented it that way.
Thanks a lot. It works :)
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 ?
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.
Thanks for info....
SEO Company in Bangalore
Post a Comment