What Is a Portlet
Pages: 1, 2, 3, 4, 5, 6

Developing a "Hello World" Portlet

Now it is time to develop a sample HelloWorld portlet.



  1. Create a HelloWorld web project. Similar to a normal servlet project, it should have a /WEB-INF/web.xmlfile that will be the deployment descriptor of the project.
  2. Add portlet-api-1.0.jar file to your build path. This .jar is part of the Pluto distribution.
  3. Inside of your Source folder, create a HelloWorld.java file, like this:
    public class HelloWorld extends GenericPortlet{
      protected void doView(RenderRequest request,
      RenderResponse response) throws
      PortletException, IOException {
            response.setContentType("text/html");
            response.getWriter().println("Hello Portlet");
            }
    }

    Every portlet should implement the Portlet interface. This interface defines the life cycle methods for a portlet. Since you don't want to override all of those methods, we will extend the GenericPortlet class, which is an adapter class implementing the Portlet interface. It provides default implementations of all life cycle methods, so we only have to implement necessary methods.

    The only thing that we want to do in our HelloWorld portlet is to display "Hello Portlet." So we will override the doView() method of the GenericPortlet class. This method takes a PortletRequest and a PortletResponse as arguments. Call response.setContentType() first thing in the doView() method to inform the portlet container about what content type the portlet is going to generate--failure to do so will result in an IllegalStateException. Once the content type is set, you can get a PrintWriter from the response object and start writing into it.

  4. Every portlet application should have a portlet.xml file in the /WEB-INF folder, which is the deployment descriptor for a portlet application. Create a portlet.xml file, like this:
    <portlet>
      <description>HelloWorldDescription
            </description>
        <portlet-name>HelloWorld
            </portlet-name>
        <display-name>Hello World
            </display-name>
    
        <portlet-class>com.test.HelloWorld
            </portlet-class>
        <expiration-cache>-1
            </expiration-cache>
            <supports>
              <mime-type>text/html</mime-type>
          <portlet-mode>VIEW
              </portlet-mode>
            </supports>
        <supported-locale>en
            </supported-locale>
    
            <portlet-info>
              <title>Hello World</title>
              <short-title>Hello World
              </short-title>
              <keywords>Hello,pluto</keywords>
          </portlet-info>
    </portlet>

    The <portlet-name> element declares the name of the portlet. The <portlet-class> element specifies the fully qualified class name of portlet. The <expiration-cache> element specifies the time in seconds after which content is considered to be stale. This is a little bit more complicated than that: if you perform some action on the portlet, then new content will be generated, irrespective of cache time.

    The <supports> element specifies which modes are supported for a given <mime-type>. In our example, we are saying that HelloWorld can only generate content of the text/html content type, and for the text/html content type, only the view mode is supported. If you decide to add support for other content types, then you should add new <support> elements and specify what modes are supported for that MIME type. It is very common for a portlet to have VIEW, EDIT, and HELP modes for text/html, and only VIEW mode for the WML MIME type.

    You can also specify what locales the portlet supports by using the <supported-locale> element. The <title> element is used to specify a title for the portlet. If you want an internationalized title, then you can specify the name of the resources (i.e., a .properties) file using the <resource-bundle> element. In that case, the container will choose a title from the appropriate .properties file, based on the user's locale.

  5. Every portlet application is a web application, so it requires a web.xml file in addition to portlet.xml.
    <web-app>
      <display-name>Hello World Portlet
      </display-name>
      <welcome-file-list
        <welcome-file>index.jsp
            </welcome-file>
      </welcome-file-list>
    </web-app>
  6. The next step is compiling this content and packaging it into a .war file. You can do that yourself or download the sample code (see the "Resources" section) that has a build.xml that your can use for creating the .war file.

Pages: 1, 2, 3, 4, 5, 6

Next Pagearrow