Portlet Tutorial – Hello Portlet 2.0 World

Here is an example of a portlet running inside a portal.

Create folder follow structure:

- c:\hello
      + web-inf
            ++ web.xml
            ++ portlet.xml
      + classes
            ++ HelloPortlet.java
            ++ HelloPortlet.class

HelloPortlet.java

public class HelloPortlet extends GenericPortlet {

  public void render(RenderRequest request, RenderResponse response)
                                 throws PortletException, IOException {

    response.setContentType("text/html");
    response.getWriter().write("Hello Portlet 2.0 World");
  }
}

portlet.xml

<portlet-app xmlns=\"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd\"
 xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
 schemalocation=\"http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd
 http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd\">
   <portlet>
       <portlet-name>HelloPortlet</portlet-name>
           <portlet-class>HelloPortlet</portlet-class>
           <supports>
           <mime-type>text/html</mime-type>
           <portlet-mode>view</portlet-mode>
           <portlet-mode>edit</portlet-mode>
           <portlet-mode>help</portlet-mode>
           </supports>
           <portlet-info>
               <title>Hello World Portlet</title>
           </portlet-info>
       </portlet>
</portlet-app>

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>
<display-name>hello</display-name>
</web-app>

display name: hello in web.xml must same with war name when you create by jar command.

– compile class:     javac -classpath c:\lib\portletapi_20.jar   HelloPortlet.java
– create war file:    c:\hello> jar -cvf hello.war .

file hello.war will be create in hello folder.

c:\hello\hello.war
Download hello.war: http://www.mediafire.com/?lnkjimckmfn
Download hello-src.zip: http://www.mediafire.com/?wwduzzzhdkd

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s