Developing Portlets with NetBeans Portal Pack 2.0

   
By Satya Ranja, Umachitra Damodaran, April 2008  

This article describes portlet development using NetBeans Portal Pack 2.0 and Java Application Platform SDK Update 5.

Note: See NetBeans Portal Pack 2.0 Release Notes and Installation Instructions.

 

Contents
 


 
Introducing NetBeans Portal Pack 2.0
 

NetBeans Portal Pack 2.0 makes portlet development as easy as writing a normal Java applications; with all the features of portlets made available to you at the click of a mouse. NetBeans Portal Pack 2.0 is a plugin that extends the functionality of NetBeans IDE 6.1 to enable creation of portlets.

This article describes the new features in NetBeans Portal Pack 2.0. The new features supported in this version of Portal Pack are:

  • Support for Java Portlet Specification 2.0 (JSR 286).
  • Wizard-based portlet filter support.
  • Drag and drop support for public render parameters.
  • Deployment support on OpenPortal Portlet Container 2.0.
Downloads
 

The distribution of the software required to create portlet applications can be found at:

Prerequisites
 

You can install the above mentioned software in any of the following ways:

  • Download the Java EE 5 Tools Bundle Update 5 to get all the required components for portlet development. The Java EE 5 Tools Bundle Update 5 contains the following software for portlet development:
  • NetBeans IDE 6.1
  • Portal Pack 2.0
  • Sun Java System Application Server 9.1
  • Portlet Container 2.0

  • Download and install all required software separately to set up portlet development environment. The following individual components are required to set up the portlet development environment in NetBeans IDE 6.1:
  • NetBeans Portal Pack 2.0 on NetBeans IDE 6.1
  • Java Application Platform SDK Update 5 with OpenPortal Portlet Container 2.0

NetBeans Portal Pack 2.0 can be used to deploy and undeploy portlets on an already installed portlet container in any of the following scenarios:

  • A Portlet Container downloaded and installed on a GlassFish application server (see Downloads).
  • A Portlet Container bundled with the Java Application Platform SDK Update 5 release.

For installation instructions, visit:

Configuration Steps
 

You will need to configure a portlet container instance inside your NetBeans IDE. Please see the Appendix for further details.

Scope
 

In this article, we will provide a step-by-step walkthrough on:

  • How to write your first "Hello World" portlet.
  • How to write a simple filter that tracks and logs the number of users who have accessed the portlet. This sample demonstrates the Portlet Filter feature introduced in the JSR 286 Public Review Draft.
Example: HelloWorld Portlet
 

The following section discusses how to create a Hello World portlet.

Create a New NetBeans Project

  1. Select File > New Project > Web > Web Application.
  2. Select an instance of "OpenPortal Portlet Container 2.0" as the server. If you have not already configured your portlets to run on a portlet container, click the Add button and select the portlet container of your choice. A drop-down list provides the various instances that you can configure your portlet application to run on.
  3. Enter the project name as "HelloWorld" and click Next.
  4. Select "Portlet Support" in Framework selection panel. Here, you also need to select the Portlet Version as 2.0 and set the the Create Portlet and Create Jsps options.
Figure 1: New Portlet Wizard
 
  • Click on Finish.

The Hello World portlet application is now created with a default HelloWorld portlet class file and the relevant JSP files. The portlet.xml file is updated with the portlet created in the above step. The HelloWorld portlet will display the string “ HelloWorld - VIEW MODE” when it is executed and viewed on a browser.

The following section describes how to run portlets in NetBeans IDE 6.1.

Execution

To run the portlet, right-click on the project and click "Run." The IDE will first deploy the portlet on the portlet container configured at the time of the project creation. When the deployment succeeds, the IDE will open the NetBeans configured browser and display the portlet "Hello World" within the portlet container driver.

Deployment and Undeployment

The following section provides an approach to deploy the portlet application that one just created.

  1. Right-click on the project and select "Clean and Build." The IDE will delete all the class files and any WAR (web archive) files created earlier. The IDE will recompile and create the WAR files afresh.
  2. Right-click on the project and select "Undeploy and Deploy." The IDE will undeploy the portlet if it is already deployed and re-deploy. This way, the portlet will reflect any new changes that were made to the same portlet deployed earlier.
  3. In the Run Time tab, click Services->Servers->OpenPortal Portlet Container 2.0 global->portlets to view the list of deployed portlets.

You can either undeploy the portlets or view the portlets at this point by right-clicking on the portlet node.

Skeleton Walkthrough

This section provides an overview of the basic template of a portlet application created by NetBeans Portal Pack 2.0.

  1. Note that the portlet plugin creates all the files required for the creation of the portlet.
  2. Observe that there are three files created in the Web Pages folder. The JSP files are created under the WEB-INF/jsp folder. They are: HelloWorld_view.jsp, HelloWorld_edit.jsp, and  HelloWorld_view.jsp. These JSP files help in defining the various modes in which a portlet can execute. The JSP files need to be updated to include any presentation logic.
  3. Note that there is a Java file created in the source folder with the package name specified at the time of creating the project. All the necessary portlet packages that are needed to start developing a portlet application are imported upfront. This Java file handles the portlet request to process the presentation logic of the application. 
public void doView(RenderRequest request,RenderResponse response)
                    

             throws PortletException,IOException {
                    

   response.setContentType("text/html");
                    

   PortletRequestDispatcher dispatcher =
                    

               getPortletContext().getRequestDispatcher("/WEB-
                    

               INF/jsp/HelloWorld_view.jsp");
                    

   dispatcher.include(request, response);
                    

 }
                    

                     

                     

 public void doEdit(RenderRequest request,RenderResponse response)
                    

         throws PortletException,IOException {
                    

   response.setContentType("text/html");
                    

   PortletRequestDispatcher dispatcher =
                    

               getPortletContext().getRequestDispatcher("/WEB-
                    

               INF/jsp/HelloWorld_edit.jsp");
                    

   dispatcher.include(request, response);  
                    

                     

 }
                    

            
                    

                                
                    

   public void doHelp(RenderRequest request, RenderResponse response)
                    

            throws PortletException,IOException {
                    

     response.setContentType("text/html");
                    

     PortletRequestDispatcher dispatcher =
                    

                getPortletContext().getRequestDispatcher("/WEB-
                    

                INF/jsp/HelloWorld_help.jsp");
                    

     dispatcher.include(request, response);
                    

                  
 
  • Traverse to the web->WEB-INF directory to view the portlet.xml configuration file. This configuration file is created at the time of creating the project.
Creating a Portlet Filter Class and Filter Mapping
 

Let us now write a simple filter that tracks the number of times a portlet has been accessed. To know more about Portlet Filters and Filter Mapping, see the JSR 286 Portlet Specification and Understanding Portlet Container 2.0 Software.

To do this, create a Portlet Filter class:

  1. Select the HelloWorld application, right-click and choose New > Other > Web > Portlet Filter. If you have previously created a portlet filter, this option is readily available when you click New. The IDE caches the requests by the user and presents them when you use the next time.
  2. Enter the following details and click Next:
  • The Filter Class name as HitCounterFilter.
  • Package structure with which you want to create the portlet.
  • The location where you want the sources to be available (the default is Source Packages).
  • Select "Render" as the Filter Type for this portlet.
Figure 2: Portlet Filter Wizard
 
  • To map the filter to the portlet, click New and choose HelloWorld portlet and click Finish.

You would find that the a filter class ( HitCounterFilter.java) has been created and is mapped to the portlet. The following snippet shows the mapping in the portlet.xml file:

<filter>
                    

        <filter-name>HitCounterFilter</filter-name>
                    

        <filter-class>com.test.HitCounterFilter</filter-class>
                    

        <lifecycle>RENDER_PHASE</lifecycle>
                    

</filter>
                    

          
                    

<filter-mapping>
                    

        <filter-name>HitCounterFilter</filter-name>
                    

        <portlet-name>HelloWorld</portlet-name>
                    

</filter-mapping>
                    

                  
 

Update the doFilter() method of the filter class ( HitCounterFilter.java) to get the value of the attribute " hitCounter." This instruction will  log the number of times the user has accessed the portlet:

public void doFilter(RenderRequest renderRequest, RenderResponse renderResponse,
                    

FilterChain filterChain) throws IOException, PortletException {
                    

        if (filterConfig == null) {
                    

            return;
                    

        }
                    

                     

      //add pre processing code here
                    

        filterChain.doFilter(renderRequest, renderResponse);
                    

        PrintWriter writer = renderResponse.getWriter();
                    

        int counter = getCounter();
                    

        writer.print("<br>===============");
                    

        writer.println("<br>The number of times this portlet is accessed: " + counter);
                    

        writer.println("<br>===============");
                    

      }
                    

                     

      private int getCounter() {
                    

         Integer counter = (Integer) filterConfig.getPortletContext()
                    

         .getAttribute("hitCounter");
                    

         int count = 1;
                    

         if (counter == null) {
                    

             counter = new Integer(count);
                    

         } else {
                    

             count = counter.intValue();
                    

             counter = new Integer(++count);
                    

         }
                    

         filterConfig.getPortletContext().setAttribute("hitCounter", counter);
                    

         return count;
                    

     }
                    

                  
 

Build and deploy the portlet. When the portlet is executed, it will display the number of times this portlet has been accessed.

Figure 3: Output of the HitCounter Portlet Filter
 

The same filter can also be applied to different portlets by mapping the filter to the portlet in the portlet.xml file. Similarly multiple filters can be mapped to a single portlet. The order of execution of the filter will follow the order in which the mapping is defined in the portlet.xml file. To map a filter to other portlets please check Appendix.

Please send feedback to users@portalpack.netbeans.org.

Appendix
 

Configure Portlet Container in NetBeans

  1. Select Tools > Servers.
  2. Click Add Server.
  3. Select "OpenPortal Portlet Container 2.0" from list of servers.
  4. Enter GlassFish Installation Home, for example, "/software/glassfish." or SDK Home.
  5. Enter GlassFish administrator password. The default password is 'adminadmin'. Click Next.
  6. Provide the installed Portlet Container Home. For example,  "$GLASSFISH_HOME/domains/YOUR_DOMAIN/portlet-container." Click Finish.

Portlet Filter Mapping

You can map a Portlet Filter to other Portlets. To do this:

  1. Expand portlet.xml node in the Project explorer window.
  2. Expand "Filters" node:
Figure 4: Expand dtls

  • Select a filter and drag it to a portlet to which you want to map to. A mapping is created in the portlet.xml file between the selected filter and the portlet.

Public Render Parameters

To add a public render parameter:

  1. Expand portlet.xml node in the Project explorer window.
  2. Right click "Public Render Parameters" node.
  3. Select "Add Public Render Parameter."
  4. Enter the required details for the render parameters. The fields Identifier and Local Part are mandatory. Click OK.
  5. This will add a public render parameter entry in the portlet.xml file.
  6. To associate this newly created public render parameter to a portlet, just drag that public render parameter node to appropriate portlet node.
References
 
Rate and Review
Tell us what you think of the content of this page.
Excellent   Good   Fair   Poor  
Comments:
Your email address (no reply is possible without an address):
Sun Privacy Policy

Note: We are not able to respond to all submitted comments.