IBM ® WebSphere® Portal 4.1.2 for Multiplatforms provides the Portal Toolkit plug-in, which integrates with IBM WebSphere Studio Application Developer, Version 4.0.3 (hereafter called WebSphere Studio). Use WebSphere Studio together with the Portal Toolkit plug-in to create portlets. The Portal Toolkit generates XML files needed to package a portlet application, and generates skeleton code for a basic portlet, MVCPortlet, etc. To debug portlet code, you would need to install WebSphere Portal on WebSphere Application Server Advanced Single Server Edition 4.0.2.
This article demonstrates how to create a servlet and a portlet for your Web application, and then package them into a single Web archive (WAR) file using WebSphere Studio and the Portal Toolkit plug-in. This articles then describes how to display an HTML form from the portlet code, and how to invoke the servlet from the portlet by passing the user-submitted form values. The sample code you will create calculates monthly mortgage payments; you can use the servlet code you develop here for your own servlet development. You will use WebSphere Studio to develop a portlet and a servlet, and then you can run the WAR file on WebSphere Portal installed on WebSphere Application Server to run the WAR file. The article assumes that you have a basic understanding of how to deploy a WAR file on WebSphere Portal, and how to customize page to have the portlet. For details on how to install a WAR file and customize the page to have the portlet, refer to Ron Lynn's article, Hello World - The simplest portlet for WebSphere Portal, Version 4.1.
- WebSphere Studio Application Developer, Version 4.0.3
- WebSphere Portal 4.1 and the Portal Toolkit plug-in
Note: If you want to debug portlet code, you also need to install WebSphere Portal on WebSphere Application Server Advanced Single Server Edition 4.0.2.
The sample application demonstrates how to create
a portlet to calculate monthly mortgage payments. The portlet displays
a form which lets the user submit principal, interest rate, and
number of years. Upon submission, the portlet invokes the servlet,
and passes values to the servlet using an object of type, RequestDispatcher.
The portlet then receives monthly payments from the servlet, and
displays the result in the portlet window.
- Launch WebSphere Studio.
- Select File => New => Other to open the Select
wizard.
Figure 1. Creating a new project
- In the Select page of the New wizard, select Portlet development,
and then select Portlet application project on the right.
Figure 2. Selecting portlet application project
- Click Next. The Create a Portlet Project wizard opens.
- In the Project Name field, enter
MortgageCalculator, and in the Enterprise Application project name field, enterMortgageCalculatorEAR.
Figure 3. Entering a project name
- Click Next. The Portlet Selection page of the Create a Portlet Project wizard opens.
- Select Basic portlet.
Figure 4. Selecting portlet type
- Click Next. The Basic Portlet Parameters page of the wizard open.
- In the Portlet class name field, enter
com.ibm.dr.MortgagePortlet.
Figure 5. Basic portlet parameters
- Click Finish. The
portlet.xmlfile now opens in WebSphere Studio's Portlet view. - Select Portlet application. In the UID field, enter
com.ibm.dr.MortgageCalculatorPortlet.
Figure 6. Modifying the UID of the portlet application
- Select Concrete portlet application. In the UID field,
enter
com.ibm.dr.MortgagePortlet.concrete.
You do not need to change the UIDs of the portlet application and concrete portlet application. However, it is conventional to use fully qualified class names as the UID for portlet applications. In this case, append this fully qualified class name withconcreteas the UID for the concrete portlet application.
Figure 7. Modifying the UID of the concrete portlet application
- Expand Concrete portlet application, and select Portlet_1.
- In the Description field, enter
This portlet calculates mortgage. - In the Short title field, enter
Mortgage.
Figure 8. Defining title and description
- Save the file.
Creating the JSP page to display the form
In this section, you will modify the view.jsp
file to display an HTML form. You must encode all variable names
of the form with the encodeNameSpace tag element. This
HTML form displays text fields for Loan Amount, Mortgage
Rate, and Years to Pay.
- In WebSphere Studio's Navigator view, expand MortgageCalculator
=> webApplication => jsp. Delete the
htmldirectory because you will not be using this directory here.
Figure 9. Deleting the html directory
- Click Yes to confirm.
- Double-click on view.jsp under the
jspfolder to open the file. - Select the Source view.
- Replace the contents of
view.jspwith the following code, and save the file.
Listing 1. New contents of view.jsp
<%@ taglib uri='/WEB-INF/tld/portlet.tld' prefix='portletAPI'%>
<portletAPI:init/>
<FORM ACTION="<portletAPI:createURI/>" METHOD="POST">
<TABLE class="Portlet" width="100%" border="0"
cellspacing="0" cellpadding="0">
<TR>
<TD width="100%" align="left">Loan Amount:</TD>
</TR>
<TR>
<TD width="100%" align="left">
$<INPUT type="text" name="<portletAPI:encodeNamespace
value='loan_amount' />" size="10">
</TD>
</TR>
<TR>
<TD width="100%" align="left">
<BR>
Mortgage Rate: </TD>
</TR>
<TR>
<TD width="100%" align="left">
<INPUT type="text" name="<portletAPI:encodeNamespace
value='mortgage_rate' />" size="4">%
</TD>
</TR>
<TR>
<TD width="100%" align="left">
<BR>
Years to Pay: </TD>
</TR>
<TR>
<TD width="100%" align="left">
<INPUT type="text" name="<portletAPI:encodeNamespace
value='years_to_pay' />" size="2">
</TD>
</TR>
</TABLE>
<BR>
<INPUT type="submit" name="Save_Button"
value="Calculate" size=35>
</FORM>
|
Figure 10. Replacing contents of view.jsp

In this section, you will create a servlet using
WebSphere Studio. You will modify the doPost() method to extract
the values of loan_amount, mortgage_rate,
and years_to_pay from the object of type HttpServletRequest.
The calculated monthly payment is then displayed.
- In the Navigator view, select the MortgageCalcutor project.
Click the Create a servlet icon,
, in the toolbar. The Create the Servlet Class wizard opens.
- In the Servlet Name field, enter
MortgageCalculatorServlet, and in the Package field, entercom.ibm.dr. - Click Next.
Figure 11. Defining the servlet
- Click Finish.
- Click Yes to confirm.
- Edit the
MortgageCalculatorServlet.javafile. - Enter the following code in the doPost() method:
Listing 2. Contents of the doPost() methodPrintWriter pw = response.getWriter(); int loanAmount = Integer.parseInt( (String)request.getAttribute("loan_amount")); float mortgageRate = Float.parseFloat( (String)request.getAttribute("mortgage_rate")); int yearsToPay = Integer.parseInt( (String)request.getAttribute("years_to_pay")); int monthsToPay = yearsToPay * 12; float monthlyRate = mortgageRate/(100 *12); double payment = ((loanAmount * Math.pow((1+ monthlyRate),monthsToPay) *monthlyRate))/ (Math.pow((1+ monthlyRate),monthsToPay) -1); java.text.DecimalFormat dfObj = new java.text.DecimalFormat("#.00"); pw.println("Monthly payment is: $" + dfObj.format(payment) + "" );
- In the editor, right-click on the on the MortgageCalculatorServlet.java file, and select Organize imports from the context menu. This adds import statements of the packages needed for using classes such as PrintWriter.
- Save the file.
When installing the WAR file onto WebSphere Portal,
make sure that all servlet elements have an id
attribute. However, WebSphere Studio does not generate an id
attribute when you create a servlet. You need to manually add the
id attribute for all servlets in order for the WAR
file to install successfully. Execute the following steps to update
the web.xml file. Listing 3 illustrates a sample servlet
element:
- Open the
web.xmlfile. - Select the Source view tab.
- Add
id="servlet_2"to the servlet element of the MortgageCalculatorServlet. - Save the file.
Listing 3. Sample servlet element
<servlet id="Servlet_2"> <servlet-name>MortgageCalculatorServlet</servlet-name> <display-name>MortgageCalculatorServlet</display-name> <servlet-class>com.ibm.dr.MortgageCalculatorServlet</servlet-class> </servlet> |
You do not have to add IDs for portlets since Portal Toolkit automatically assigns IDs for the servlet elements of a portlet.
Invoking the servlet from the portlet
In this section, you will modify the portlet's
doView() method. You will extract the encoded parameters loan_amount,
mortgage_rate, and years_to_pay from the
object of type, PortletRequest, and set the parameters
into this object as attributes using the setAttribute method
of the HttpServletRequest class. Then, you will invoke the MortgageCalculatorServlet
servlet from the portlet using the object of type RequestDispatcher.
- Open the
MortgagePortlet.javafile. - Add the
import javax.servlet.ServletException;statement. - Add the following code at the end of the doView() method:
Listing 4. Contents of the doView() methodString loanAmount = request.getParameter("loan_amount"); String mortgageRate = request.getParameter("mortgage_rate"); String yearsToPay = request.getParameter("years_to_pay"); PrintWriter pw = response.getWriter(); if(loanAmount== null || loanAmount==""|| mortgageRate==null||mortgageRate==""|| yearsToPay==null || yearsToPay== "") return; request.setAttribute("loan_amount", loanAmount); request.setAttribute("mortgage_rate", mortgageRate); request.setAttribute("years_to_pay", yearsToPay); try{ RequestDispatcher rd=this.getServletContext().getRequestDispatcher ("/MortgageCalculatorServlet"); rd.include(request,response); }catch(ServletException se){ }
- In the editor, right-click on the on the MortgageCalculatorServlet.java file, and select Organize imports from the context menu.
- Save the
MortgagePortlet.javafile.
- In the Navigator view, right-click MortageCalculator, and click Export WAR from the context menu.
- In the Where do you want to export resources to? field, enter
a directory structure with the WAR file name, for example,
D:\sampportlets\mortgageportlet.war.
Figure 12. Exporting the WAR file
- Click Finish.
- Install the exported WAR file using
Install Portlet. - Create a page and customize it to contain
MortgageCalculator portlet.
Figure 13. The MortgageCalculator portlet
- Enter values in the Loan Amount, Mortgage Rate, and Years to Pay fields. Click Calculate.
The monthly payment is now displayed:
Figure 14. Mortgage calculator result

Tips for using WebSphere Studio to develop portlets
The portlet is only a part of a page, so the portlet's
output stream must not contain tags such as, <body>,
<html>, etc. When you create a JSP page using
WebSphere Studio, it generates all of these HTML tags into the JSP
file. To disable this function in WebSphere Studio, complete the
following:
- From the menu, select Window => Preferences.
- Expand Webtools, and select Files.
- Uncheck the Insert this DOCTYPE on and Include GENERATOR in HTML source check boxes.
- Click OK.
Figure 15. Preferences window
When you assign <portletAPI:createURI>
to the action attribute of the form, WebSphere Studio
will complain that it is a broken link. To prevent WebSphere Studio
from doing this, complete the following:
- In the Tasks view, click on the Filter icon
.
The Filter Tasks
wizard opens. - Uncheck the Broken Links check box under Problem.
- Click OK.
Figure 16. Filter Tasks window
The article demonstrated how to develop portlets and servlets using WebSphere Studio Application Developer and the Portal Toolkit plug-in. If you have existing servlets, you can import them into the portlet project, and then invoke the servlet from the portlet code. If desired, you can install WebSphere Portal on WebSphere Application Server Advanced Single Server Edition to debug the portlets.
The author would like to thank Doug Phillips and William Trotman for reviewing the article, and providing valuable suggestions and contributions.
Sukumar Konduru is an Advisory Software Engineer at the IBM Dallas, Developer Technical Support Center. He holds an M. S. in Computer Science from the University of Houston. You can reach Sukumar at konduru@us.ibm.com.




