Sunday, October 14, 2012

Getting Query String of portal page URL from inside the portlet



Getting Query String of portal page URL from inside the portlet:
Below article will explain how we can get query string of portal page url  inside a portlet .
To achieve this we can create a QueryStringPortlet portlet like this.
public class QueryStringPortlet extends GenericPortlet {
public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
            // Set the MIME type for the render response
            response.setContentType(request.getResponseContentType());
            System.out.println("Start Point of QueryStringPortlet.doView()");
            HttpServletRequest httpServletRequest = getHttpServletRequest(request);
            String queryString = httpServletRequest.getQueryString();
            System.out.println("Value of Query String =" + queryString);
            request.setAttribute("queryString",queryString);
            // Invoke the JSP to render
            PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher                           (getJspFilePath(request, VIEW_JSP));
            rd.include(request,response);
            }
Accessing underlying HttpServletRequest from PortletRequest in WebSphere Portal Server
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;
            }

In  doView() method use getHttpServletRequest() method to get underlying HttpServletRequest object from the PortletRequest and then calls its httpServletRequest.getQueryString() method to get the query String. Once we have the query string we can set it in the request object and forwarding control to JSP for rendering output.


The jsp code should be like below:

<h2>Query String url ======  <%=renderRequest.getAttribute("queryString") %></h2>

Deployment and configuration of this portlet.
Below steps to be performed to deploy and configure this portlet through admin console. 

1.      Deploy this portlet 
2.      Create a page like queryString 
3.      Add this QueryStringPortlet on the page queryString 
4.      Create a url mapping for this page as queryString 
5.      Pass some query strign like queryString?value=Kameshwar 
6.      The query string will be printed on the portlet like below screen










No comments:

Post a Comment