Articles
Java Platform, Enterprise Edition
|
|
| By Satya Ranjan, Umachitra Damodaran, September 2007 |
|
This article describes portlet development using NetBeans Portal Pack 2.0 Beta and Java Application Platform SDK Update 3 Beta.
Note: See NetBeans Portal Pack 2.0 Beta Release Notes and Installation Instructions.
NetBeans Portal Pack 2.0 Beta 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.0 Beta 1 to enable creation of portlets.
This article describes the new features in NetBeans Portal Pack 2.0 Beta. The new features supported in this version of Portal Pack are:
The distribution of the software required to create portlet applications can be found at:
You can install the above mentioned software in any of the following ways:
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:
For installation instructions, visit:
You will need to configure a portlet container instance inside your NetBeans IDE. Please see the Appendix for further details.
In this article, we will provide a step-by-step walkthrough on:
The following section discusses how to create a Hello World portlet.
|
Figure 1: New Portlet Wizard
|
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 Beta 1.
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.
The following section provides an approach to deploy the portlet application that one just created.
You can either undeploy the portlets or view the portlets at this point by right-clicking on the portlet node.
This section provides an overview of the basic template of a portlet application created by NetBeans Portal Pack 2.0 Beta.
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);
|
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 Beta Software.
To do this, create a Portlet Filter class:
New > Other > Web > Portlet Filter ." If you have previously created a portlet filter, this option will already be available when you click New. The IDE caches the requests by the user and presents them when you use the next time.|
Figure 2: Portlet Filter Wizard
|
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.
Configure Portlet Container in NetBeans
You can map a Portlet Filter to other Portlets. To do this:
|
Figure 4: Expand dtls
|
Public Render Parameters
To add a public render parameter:
|