28-Oct-06 (Created: 28-Oct-06) | More in 'Java-Portlets'

Samplecode: JSPPortlet

Source Code



package com.indent.portals.test;

import java.io.IOException;
import java.net.URLEncoder;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ValidatorException;

public class IndentJSPPortlet extends GenericPortlet {
   
   public static String PREF_JSPPAGE="jsppage";
   public static String ATTRIBUTE_JSPPAGE = "indentJspPortlet.jsppage";

   public void processAction(ActionRequest req, ActionResponse res)
   throws IOException, PortletException 
   {
      PortletPreferences prefs=req.getPreferences();
       boolean editOK;
       String errorMsg = null;
       String jsppage = req.getParameter("jsppage");
       prefs.setValue("jsppage",jsppage);
       try {
          prefs.store();
          editOK = true;
       }
       catch (ValidatorException ex) {
          editOK = false;
          errorMsg = ex.getMessage();
       }
       if (editOK) {
           res.setPortletMode(PortletMode.VIEW);
       }
       else {
           res.setRenderParameter("error","Failed to save preferences");
       }
      
   }

   public void doView(RenderRequest req, RenderResponse res)
   throws IOException, PortletException 
   {
      res.setContentType("text/html");
      PortletPreferences prefs = req.getPreferences();
      String jsppage = prefs.getValue(IndentJSPPortlet.PREF_JSPPAGE,null);
      System.out.println("jsppage:" + jsppage);
      if (jsppage == null)
      {
         res.getWriter().print("<p>Go to edit of this portlet and specify a jsp page.</p>");
         return;
      }
      if (jsppage.trim().equals(""))
      {
         res.getWriter().print("<p>Go to edit of this portlet and specify a jsp page.</p>");
         return;
      }
      includeJsp(jsppage,req,res);
   }
   public void includeJsp(String jsppage,RenderRequest req, RenderResponse res)
   throws IOException,PortletException
   {
      PortletRequestDispatcher rd =
         getPortletContext().getRequestDispatcher(jsppage);
      rd.include(req,res);
   }
   public void doEdit(RenderRequest req, RenderResponse res)
   throws IOException, PortletException
   {
      res.setContentType("text/html");
      PortletPreferences prefs = req.getPreferences();
      String jsppage = prefs.getValue(IndentJSPPortlet.PREF_JSPPAGE,null);
      req.setAttribute(this.ATTRIBUTE_JSPPAGE,jsppage);
      includeJsp("/indent/jspportlet-edit.jsp",req,res);
   }
}//eof-class

Porlet Definition


   <portlet>
      <portlet-name>satya-portlet-jspportlet-2</portlet-name>
      <display-name>IndentJSPPortlet2</display-name>
      <portlet-class>com.indent.portals.test.IndentJSPPortlet</portlet-class>
      <expiration-cache>0</expiration-cache>
      <supports>
         <mime-type>text/html</mime-type>
         <portlet-mode>edit</portlet-mode>
      </supports>
      <portlet-info>
         <title>Indent JSP Portlet 2</title>
         <short-title>Indent JSP Portlet 2</short-title>
         <keywords>Indent JSP Portlet 2</keywords>
      </portlet-info>
      <portlet-preferences>
         <preference>
            <name>jsppage</name>
            <value></value>
         </preference>
      </portlet-preferences>
      <security-role-ref>
         <role-name>power-user</role-name>
      </security-role-ref>
      <security-role-ref>
         <role-name>user</role-name>
      </security-role-ref>
   </portlet>

Edit Preferences JSP Page


<%@ include file="/html/portlet/init.jsp" %>
<!--
***********************************************
* jspportlet-edit.jsp
***********************************************
1. edit the property called "jsppage"
-->
<%
String jsppage = (String)renderRequest.getAttribute("indentJspPortlet.jsppage");
%>
<h3>Edit Preferences for JSP Portlet</h3>

<form method="post" action="<portlet:actionURL/>">
<p>Specify JSP page to display output
<input type="text" name="jsppage" value="<%=jsppage%>">
<input type="submit" value="Submit" name="SubmitButton"/>
</form>

A sample jsp page to test


<p>Hello world from JSP Portlet