# Building Java Portlets

JSR 168 (Java Portlet) is a Java specification that aims at establishing portability between portlets and portals. One of the main goals of the specification is to define a set of standard Java APIs for portal and portlet vendors. These APIs will cover areas such as presentation, aggregation, security, and portlet lifecycle.

Java Portlets are intended for software companies and other enterprises that are concerned with portability across multiple portlet containers. For information about the emerging JSR 168 work, see the article Developing JSR 168 Portlets with WebLogic Portal 8.1 on the BEA dev2dev site.

In the WebLogic Workshop IDE, you can use the Portlet Wizard to create a new Java Portlet. For example, in the /portalApp/sampleportal web project, there is a /portlets folder. In the IDE Application pane, right-mouse click on the portlets folder and select New > Folder...

Give your new folder a name; for example, aJavaPortlet. Then right click on the aJavaPortlet folder and select New > Portlet...

This will invoke the Portlet Wizard. Name your portlet; for example, helloWorld.portlet. Then click the Create button. The Portlet Wizard displays its first screen, on which we have already selected the Java Portlet type from the available options:

Click the Next button. The Portlet Wizard displays this screen, on which we have already filled in three values:

Before we describe the properties on the Wizard dialog, note that there is a separate deployment descriptor for Java Portlets. The file is /WEB-INF/portlet.xml. In addition, there is a Portal-specific deployment descriptor, /WEB-INF/weblogic-portlet.xml, to inject some additional features.

Here is an example of how entries may look in portlet.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <portlet-app version="1.0"
       xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_1_0.xsd"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet">

        <portlet>
            <description>Description goes here</description>
            <portlet-name>helloWorld</portlet-name>
            <portlet-class>aJavaPortlet.HelloWorld</portlet-class>
            <portlet-info><title>Hello World!</title></portlet-info>
        </portlet>
    </portlet-app>

For the properties on the Portlet Wizard dialog:

- Title: This is default title of the portlet, which maps to the <title> element in portlet.xml.
- Definition Label: This is similar to a definition label for any portlet. However this also maps to the name of the portlet in the deployment descriptor; in the simple example above, the <portlet-name> element.
- Class Name: This maps to the <portlet-class> element. After the Wizard runs, in this example a HelloWorld.java file will be created in /WEB-INF/src/aJavaPortlet.
  Note: Another option for developers is to generate a Java Portlet's *.portlet file based on pre-existing classes.

Based on these values, the Wizard creates a .portlet file, and adds an entry to /WEB-INF/portlet.xml. All these fields are required to create a Java portlet.

Enter a title, a definition label, and valid class name for your Java Portlet. Then click the Finish button. WebLogic Workshop displays the newly created portlet and its current properties, as shown here:

You can then modify the Java Portlet by changing the properties on the Property Editor pane, and by editing the generated Java class. In this simple example, the initial HelloWorld.java file contains:

    package aJavaPortlet;

    import java.io.IOException;
    import javax.portlet.PortletException;
    import javax.portlet.GenericPortlet;
    import javax.portlet.RenderResponse;
    import javax.portlet.RenderRequest;

    /**
     * <p>A simple hello world portlet.</p>
     */
     public class HelloWorld extends GenericPortlet

    {

         public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
         {
              response.setContentType("text/html");
              response.getWriter().write("<p>Hello World</p>");
         }

    }

###
Adding an Icon to a Java Portlet

To add an icon to a Java Portlet (a JSR 168 portlet), you need to edit the weblogic-portlet.xml file, as described in this section.

> 1. Place the icon in the images directory of the skin that the portal is using. For example, if the skin name is avitek, icons must be placed in:

> > myPortal/skins/avitek/images

> 2. In the Application panel, locate and double-click the weblogic-portlet.xml file to open it. This file is located in the portal's WEB-INF folder, for example:

> > myPortal/WEB-INF/weblogic-portlet.xml

> 3. Add the following lines to the weblogic-portlet.xml file:

>     <portlet>
>       <portlet-name>myPortlet</portlet-name>
>       <supports>
>         <mime-type>text/html</mime-type>
>         <titlebar-presentation>
>           <icon-url>myIcon.gif</icon-url>
>         </titlebar-presentation>
>       </supports>
>     </portlet>

> 4. Make these substitutions: (a) change myPortlet to the name of the portlet that is specified in WEB-INF/portlet.xml, (b) be sure the mime-type also matches the mime-type found in WEB-INF/portlet.xml, and (c) change myIcon.gif to the name of the icon you wish to add.

Related Topics

Creating Portlets