Developing JSR 168 Portlets with NetBeans Portal Pack 2.0 Beta

   
By Satya Ranjan, February 2008  

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

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

 

Contents
 


 
Introducing NetBeans Portal Pack 2.0 Beta
 

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

This plugin can also be used to create JSR 286 portlet applications. For more information, read this article.

Downloads
 

The following software is required to create JSR 168 portlet applications mentioned in this article.

Prerequisites
 

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

  • Download the Java EE 5 Tools Bundle Update 4 to get all the required components for portlet development. The Java EE 5 Tools Bundle Update 4 contains the following software for portlet development:
  • NetBeans IDE 6.0.1
  • Portal Pack 2.0 Beta
  • Sun Java System Application Server 9.1 Update 1 (GlassFish v2 Update Release 1)
  • Portlet Container 1.0.2

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

NetBeans Portal Pack 2.0 Beta 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 4 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.
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 1.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 list box 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 1.0 and set the the Create Portlet and Create Jsps options.
Figure 1: New Portlet Wizard
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.0.

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 1.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 Beta.

  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.


Build and deploy the portlet. When the portlet is executed, it will display a "HelloWorld - VIEW MODE" string.

Output of the HelloWorld Portlet
Figure 2. Output of the HelloWorld Portlet
Click here for a larger image.
 

You can similarly create a Portlet 1.0 application by selecting the "1.0" portlet version during project creation. And the portlet 1.0 application can be deployed on Portlet Container 1.0.2 (comes with Java Application Platform SDK Update 4).

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 1.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.
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.