RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Building a Web Service Powered JSR 168 Financial Portlet : Page 5

Broaden the reach of your portlets by making them JSR 168-compliant.

Putting it all Together
So far, you've seen how to build a JSR 168 portlet class, how to create a client to the Xignite web services, how to invoke the client from the portlet's processAction() method, and how the portlet class dispatches to the viewQuotes.jsp in the doView() method. To finish up, here's a quick look at the JSP code.

The stock quote portlet uses this JSP to display its content. The JSP displays a form where users can enter stock symbols, and it submits an actionRequest to the portlet. The portlet dispatches to it during the doView method to display the portlet in the View mode:

   <%@taglib uri="http://java.sun.com/portlet" 
All compliant Portal Servers must implement the Portlet Tag Library. The portlet tags are useful for creating references to several portlet objects in the JSPs, and for creating action and render URLs that trigger methods on the current portlet. The preceding code shows a declaration of the portlet tag library and usage of the tag. The defineObjects tag defines the renderRequest, renderResponse, and portletConfig objects obtained from the JSP's request object.

Next, you need to get a reference to the portlet session:

The renderRequest.getPortletSession() call gets the portlet session. It gets the quotes attribute from the portlet session, and registers it to the pageContext object's quotes attribute with page scope. If you remember, the quotes ArrayList was created to store the data retrieved from the Xignite web services.

This next bit of code checks whether an errors object exists in the requestScope and is not empty. If so, it displays the errors:

   <c:if test="${!empty requestScope.errors}">
      <div id="errors" style="border:1px solid red; color:red; 
         padding:8px; margin:8px 0">
         <c:out value="${requestScope.errors}" />
Here's the form that lets portlet users enter one or more stock symbols:

   <form method="post" action="<portlet:actionURL />">
      <label for="symbols">Stock Symbol(s):</label> 
      <input id="symbols" type="text" name="symbols">
      <input type="submit" value="Submit" />
When users click the Submit button, the form submits to the URL specified in the form's action attribute. Note that here, you specify the action request by using the tag. The submitted data includes the symbols input value as an attribute. The action request triggers the processAction method in the portlet class:

   <table border="1">
       <th>Company Symbol</th>
       <th>Stock Price</th>
   <c:forEach items="${pageScope.quotes}" var="quote">
      <td><c:out value="${quote.company}"  /></td>
      <td><c:out value="${quote.price}" /></td>
The last portion of the JSP code builds a simple table to display the data. It loops through the page's quotes object (which was registered at the top of the JSP), and generates a new table row for each quote in the list.

Author's Note: For clarity, CSS styles were omitted from the HTML shown above.

Running the example
Before running this example, you need to perform two BEA-specific tasks:

  1. Create a StockQuote.portlet file for the Stock Quotes Portlet. To create the BEA-specific .portlet file, right-click the portlets directory and select New --> Portlet. Follow the Portlet Wizard's directions, specifying that this will be a "Java Portlet," and that this portlet will use the StockQuotesPortlet class.
  2. Create a BEA TestStockQuotePortal.portal file in the same directory as the .portlet file and drop the .portlet file on the .portal's main page.
That's it. Now you can run the example by right-clicking the .portal file and selecting Run As --> Run On Server. Follow the prompts to deploy to the portal domain server you created during the preparation steps.

Authors' Note: Please consult the BEA documentation for any questions related to the steps above, because the details are outside the scope of this article.

Figure 4. Portal in Action: The figure shows how the portal looks after a user requested data for the stock symbols "MSFT" and "BEAS."
Figure 4 shows a screen shot of the portal containing the Stock Quotes Portlet. This image was captured after a user entered "MSFT, BEAS" in the Stock Symbol(s) input field, and submitted the form. That fired the StockQuotesPortlet's processAction() method. The screen shot displays the resulting data from the Xignite web service, obtained from the portlet session by the JSP.

You've seen how to build a JSR 168 Portlet and integrate a Xignite financial web service in the portlet. You should now be able to build your own web services-enabled JSR 168 financial portlets. This article only scratched the surface of what you can do with JSR 168 portlets and web services. For more information on the JSR 168 portlet specification, please see these links:

Howard Block is a Founder and Principal of NuWave Solutions. He has over 22 years of software development, project management, lead architect, systems integration, and consulting experience. He is a nationally recognized expert on Internet technology and J2EE, and a frequent guest speaker at conferences held by BEA Systems. As a member of the BEA WebLogic Portal Council, he helps BEA with the future direction of the portal. Howard was a co-author of the book "Creating Web Portals with BEA WebLogic" (March 2003). Howard has a B.S. in Information Systems from the University of Maryland Baltimore County (UMBC) and an M.S. in Computer Science from Johns Hopkins University.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date