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.
- Create a
HelloWorldweb project. Similar to a normal servlet project, it should have a /WEB-INF/web.xmlfile that will be the deployment descriptor of the project. - Add portlet-api-1.0.jar file to your build path. This .jar is part of the Pluto distribution.
- Inside of your Source folder, create a
HelloWorld.javafile, 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
Portletinterface. This interface defines the life cycle methods for a portlet. Since you don't want to override all of those methods, we will extend theGenericPortletclass, which is an adapter class implementing thePortletinterface. 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
HelloWorldportlet is to display "Hello Portlet." So we will override thedoView()method of theGenericPortletclass. This method takes aPortletRequestand aPortletResponseas arguments. Callresponse.setContentType()first thing in thedoView()method to inform the portlet container about what content type the portlet is going to generate--failure to do so will result in anIllegalStateException. Once the content type is set, you can get aPrintWriterfrom the response object and start writing into it. - 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 thatHelloWorldcan only generate content of thetext/htmlcontent type, and for thetext/htmlcontent type, only theviewmode 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 haveVIEW,EDIT, andHELPmodes fortext/html, and onlyVIEWmode 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. - 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> - 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.
