Bill:
Even I have just entered the world of protlets a month back. I'm using the JSR-168 api too. Here's how I would go about explaining portlets:
A portlet application is a web application in itself. A portlet also dishes up web content and in fact uses an api very similar to that of the Servlet api. But it does not inherit/implement anything from the servlet api, mind you. E.g. you have PortletRequest, PortletResponse, PortletRequestDispatcher. etc. etc. doing similar functions as in the servlet api, but they do not inherit from the servlet api. However, there is a major difference between servlets and portlets which I'm going to try to explain below-
A portlet dishes up web content that forms PART not whole of a browser page. A web portal will typically be composed of many portlets, each taking up space on the page. Now comes the interesting part - in a servlet the action and rendering tasks are performing in one go through the service method. i.e. you would call the business validators and do any other tasks before calling the relevant JSP page all through the service() method. this is because the content served by the servlet forms the entire page. so the servlet just processes all the tasks and then serves the content through one service() method.
But, in a portlet, since it renders only part of the browser page, it cannot do this processing and rendering all at one go. This is because, in the case of a portal having say 2 portlets, portlet A and portlet B, a user may perform some action on portlet B resulting in iot needing to be refreshed. But since the entire page will be refreshed, portlet A will also be rendered again. No action was performed on portlet A, yet it was refreshed again. in the case of portlet B, the action was performed on it and then it was rendered again automaticlly. the action on the portlet happens thru the processAction(...) method and the rendering takes place thru the render(...) method. the action changes the state of the portlet, while rendering shows the current state of the portlet.
It is the job of the portlet container to tranlate HTTP into portlet api and vice-versa. So portlets live inside a portlet container and dish out content which is passed on to the portlet container which then translates into HTTP and browser readable format.
Also - just as we have doGet(), doPost() called from the service() method, same way we have in Portlets the doView() and other methods called from the render(...) method
here's a very simple example of a portlet:
HelloPortlet.java
=====================
package hello;
import java.io.*;
import javax.portlet.*;
/**
*
* A sample portlet based on GenericPortlet
*
*/
public class HelloPortlet extends GenericPortlet {
/**
* @see javax.portlet.GenericPortlet#doView(RenderRequest, RenderResponse)
*/
protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
response.setContentType(request.getResponseContentType());
response.getWriter().println("Hello World");
}
/**
* Process an action request.
*
* @see javax.portlet.Portlet#processAction(ActionRequest, ActionResponse)
*/
public void processAction(ActionRequest request, ActionResponse response) throws PortletException, java.io.IOException {
// Add action request handler here
}
}
============================
web.xml
========
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp_ID">
<display-name>Hello</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<taglib id="PortletTLD">
<taglib-uri>
http://java.sun.com/portlet</taglib-uri>
<taglib-location>/WEB-INF/tld/std-portlet.tld</taglib-location>
</taglib>
</web-app>
===================================
portlet.xml
===========
<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd" version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd
http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd" id="hello.HelloPortlet.dbff84bf80">
<portlet>
<description>This is the Hello World portlet</description>
<description xml:lang="en">This is the Hello World portlet</description>
<portlet-name>Hello</portlet-name>
<display-name>Hello portlet</display-name>
<display-name xml:lang="en">Hello portlet</display-name>
<portlet-class>hello.HelloPortlet</portlet-class>
<init-param>
<name>wps.markup</name>
<value>html</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
<supported-locale>en</supported-locale>
<portlet-info>
<title>Hello portlet</title>
</portlet-info>
</portlet>
</portlet-app>
=======END==================
Originally posted by William Moore:
Hi,
I am totally new to the world of Portlets, I don't even know how to start though i have set up a workspace and am currently running the HelloWorldPortlet in WSAD 5.1.2 but i need some very basic help on how to code a Portlet using JSR168.
Thanks in advance,
Bill