Developing Portlets with the NetBeans Portlet Plugin

   
By Vihang Pathak and Satya Ranjan, December 14, 2006; updated May 4, 2007  

This article describes portlet development using the NetBeans Portlet Plugin for the Portlet Container 1.0 Beta software bundled with the Java Application Platform SDK Update 2 and for the Portlet Container 1.0 (FCS - final release) bundled with the Java Application Platform SDK Update 3 Preview 2. This article applies to both Portlet Container 1.0 Beta and Portlet Container 1.0 FCS unless otherwise noted.

Note: Note: Also see the NetBeans Portlet Plugin Release Notes and Installation Instructions for Java Application Platform SDK Update 2 and the NetBeans Portlet Plugin Release Notes and Installation Instructions for Java Application Platform SDK Update 3 Preview 2.

 

Contents
 


 
Preface

Have you ever wished for an easy learning curve for an upcoming technology? Have you wished to focus on what your application should do? Have you ever wondered why the adoption of a decent technology could stop due to the absence of tools around it?

If the answer is yes to these questions, then please read on.

Introducing NetBeans Portlet Plugin

The new NetBeans Portlet Plugin will make portlet development as easy as writing a normal Java application, with all the features of portlets made available to you at the click of a mouse.

This article describes how to write that first simple portlet. Without getting into too much theory, let's get hands-on experience.

Downloads

The distribution of the software required here can be found at:


Prerequisites

This article assumes that the following is already installed:

  • NetBeans Portlet Plugin on NetBeans IDE 5.5 or later
  • Java Application Platform SDK Update 2 with Portlet Container 1.0 Beta (Enterprise Class Portlet Container) or Java Application Platform SDK Update 3 Preview 2 with Portlet Container 1.0 (FCS - final release)

The NetBeans Portlet Plugin can be used to deploy/undeploy portlets on the already installed portlet container with one 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 2/Update 3 Preview 2 release.

For installation instructions, please visit:

  1. The Java Application Platform SDK Update 2/Update 3 Preview 2 Installation Instructions and the Portlet Container 1.0 Beta and Portlet Container 1.0 (FCS) Installation Instructions
  2. The NetBeans Portlet Plugin Installation Instructions for Portlet Container 1.0 Beta and Portlet Container 1.0 (FCS)

Configuration Steps

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

Scope

This document is intended to provide a step-by-step walkthrough on:

  • How to write your first "Hello World" portlet.
  • How to enhance the plugin's generated code and introduce some other enhancements. The intention is to serve as a guide so that you can write more complex logic as desired by your application.

Currently, the portlet plugin supports deployment of portlets on:

There will be other integrations to other portlet containers since the portlet plugin already has a flexible interface for customizable integration with other portlet containers.

Portlet Plugin Example
  • The following section uses the Hello World Portlet as an example.

Creation

  1. Create a New NetBeans Project.

  2. Select New Project->Portlet Applications -> JSR 168 Portlet Application.

  3. Enter the name of the Portlet (e.g., Hello World) and follow the self-guided instructions regarding Project Location, Project Folder, Package Definition, and other required fields. (Note that J2EE 1.4 should be selected as the preferred J2EE environment.)

  4. After entering all the above information, click on Finish.

Skeleton Walkthrough

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

  1. Note that the Portlet Plugin creates all the files required by the JSR 168 specification for portlets.

  2. Observe that there are three files created in the Web Pages folder (view.jsp, edit.jsp, and help.jsp). Please refer to the  JSR 168 specification for more details. In brief, these JSP files help one define the various modes in which a portlet can execute. And this is where developers can insert presentation logic to suit their application needs.

  3. Note that there is a Java file created in the source folder with the package name one has specified. This Java file is the one that handles the portlet request to handle the presentation application requests. It imports all the necessary portlet packages that one needs to start developing a portlet application.

  4. Traverse to the web->WEB-INF directory to view the portlet.xml. This is also precreated by portlet pack. One may modify this xml and one may wish to view the portlet specifications for more details in order to correctly modify it.

Deployment/Undeployment

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

  1. Right-click on a project. Click on properties and for category RUN select the portlet container on which one wishes to deploy the project. The options can be as of now "Open Source Portlet Container."

  2. Right-click on the project. Click on "Clean and Build Project." Once it is successful, click on "Deploy Project."

  3. Go to Runtime->Servers->Open Source Portlet Container->global->portlets, to view the list of "portlets."

You can either undeploy the portlets or view the portlets at this point.

Execution

The following section is where one gets to see how the portlet execution will look. This portlet that has just been created will be deployed on the portlet container and will use the portlet container driver to display the portlet (refer to Portlet Container documentation for more details).

  1. Go to Runtime > Servers > Open Source Portlet Container > Right-click >Click on Admin Tool.

  2. It will open the NetBeans configured browser and display the portlet "Hello World" within the portlet container driver.

Accept a User Name and Greet the User Name With a Hello

After having created a simple portlet, let's walk through how one can modify the source code generated and pass parameters across the jsp and the portlet doView() methods, etc.

For this we will modify the below mentioned sources:

  1. In "view.jsp,"
    <%@page contentType="text/html"%>
    <%@page pageEncoding="UTF-8"%>
    <%@taglib uri="#" prefix="portlet"%>
    <!-- The portlet specification defines the portlet tag library with the aim of
         solving common problems faced by portlet developers. 
     -->
    <portlet:defineObjects/>
    <% 
            String requestParameter = request.getParameter("helloname"); 
            if(requestParameter == null || requestParameter.equals("")) { 
    %>
    <!-- In order to pass parameters and invoke processAction(), i.e., 
         Initiate an ActionRequest, the following code renderResponse.createActionURL() 
         is being used.Creates a portlet URL targeting the portlet. If no portlet mode,
         window state, or security modifier is set in the PortletURL, the
         current values are preserved. If a request is triggered by the
         PortletURL, it results in an action request. The returned URL can be
         further extended by adding portlet-specific parameters, portlet
         modes, and window states. The created URL will per default not contain
         any parameters of the current render request. ProcessAction() is
         otherwise invoked when an action is performed via the "Edit"
         mode of a portlet. -->
    <form name="testForm" method="POST" 
    action="<%=renderResponse.createActionURL().toString()%>">
    
     First name: <INPUT type="text" name="firstname"> 
     <INPUT type="submit" value="Submit">
    
    </FORM>
    <%} else {
    %>
                            <%=requestParameter%>
    <a href="<%=renderResponse.createActionURL().toString()%>">Back</a>
    <%}%>
    
                        
     
  2. Add a method in *.java file, which has the doView() etc., defined.
    public void processAction(ActionRequest request, ActionResponse response) throws
     PortletException, IOException {
    
            try {
    
                    // Gets the parameter passed from the form.
    
    
                    String name = request.getParameter("firstname");
    
                    if(name != null || !name.equals("")) {
    
                            response.setRenderParameter("helloname","Hello " + name);
    
                    }
            }catch(Exception e){
                    e.printStackTrace();
            }
    
    }
    
     

Follow the same steps as mentioned for building and deployment. Enter user name and press "submit." The portlet will appear with a greeting "Hello" to the user name entered earlier.

References



Appendix

Configure Portlet Container in NetBeans

  1. Select Tools > Server Manager.
  2. Click Add Server.
  3. Select "Open Source Portlet Container" from list of servers.
  4. Give installed Portlet Container Home e.g., "$GLASSFISH_HOME/domains/YOUR_DOMAIN/portlet-container." Click next.
  5. Enter GlassFish Installation Home e.g., "/software/glassfish."
  6. Enter Domain Dir e.g., "$GLASSFISH_HOME/domains/YOUR_DOMAIN."
  7. Enter Glassfish, admin password; default password is "adminadmin."

 

For Java Application Platform SDK Update 3 Preview 2:
  1. Select Tools > Server Manager.
  2. Click Add Server.
  3. Select "Open Source Portlet Container" from the list of servers.
  4. Enter GlassFish Installation Home e.g., "/software/glassfish."
  5. Enter Glassfish admin password. (Default password is "adminadmin") Click next.
  6. Check Portlet Container Home(PC Home). It should be "$GLASSFISH_HOME/domains/YOUR_DOMAIN/portlet-container." Click Finish.




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.