Friday, December 16, 2011

opening PDF as popup in jsr 168 portlet,popup in jsr 168

Since JSR 168 has only support for html so we need a servlet to be called from the jsp.

Step1- Create a servlet and in doGet use this code

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

HttpSession httpsession =request.getSession();
Map map = (Map)httpsession.getAttribute("resultMap");
if(map != null)
{
GeneratePDF.generatePDFReciept(map, response);
}
httpsession.removeAttribute("resultMap");
}


Here I am passing map to GeneratePDF method and in this method i am using itext API to generate PDF from the map values.

step2:-  use the following code in the process action and set attribute in the HttpSession.

                  HttpServletRequest httpServletRequest = (HttpServletRequest)request;
   while(httpServletRequest instanceof HttpServletRequestWrapper){
     HttpServletRequestWrapper httpServletRequestWrapper =
  (HttpServletRequestWrapper)httpServletRequest;
     System.out.println("HttpServletRequestWrapper " + httpServletRequestWrapper);
     httpServletRequest =  (HttpServletRequest)httpServletRequestWrapper.getRequest();
   }
 
    HttpSession httpsession = httpServletRequest.getSession();
 
    httpsession.setAttribute("resultMap", receiptmap);


3:-Now invoke the servlet as popup from the portlet jsp by simply  calling javascript on clicking the button or link, as follows:-

function servletCall()
{

 window.open("http://<%=renderRequest.getServerName()%>:<%=renderRequest.getServerPort()%><%=renderRequest.getContextPath()%>/PDFPopup","PrintWindow","toolbar=0 scrollbars=yes");

}

Now you can view your data in popup  :)