News Ticker

Step 3: JSP template, tag libs and servlet

In the previous article, we saw how to marshal the XML returned by the web service to a Stock object. Now we are going to create a JSP template to create the structure of the popup using tag libs to manipulate the data from the Stock object and a servlet to handle the request and response of the client.

1. I will put the servlet in its own package demo.webservice.servlet and call it ProcessAjaxCallServlet.java. The JSP template will go in the WebContent folder.

Servlet and JSP

Servlet and JSP

2. The purpose of the servlet is to extract the stock code from the request and to pass it to the webservice classes so that the live quote can be obtained in XML format which it then passes to the unmarshaller and then on to the JSP template.

public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException{
      process(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {
      process(request, response);
}

public void process(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {

    String code = request.getParameter("code");

    String xml = XMLStockQuote.getXMLForCode(code);
    Stock stock = StockQuoteMarshaller.unmarshal(xml);

    request.setAttribute("StockQuote", stock);

    RequestDispatcher dis = request.getRequestDispatcher("StockQuoteContent.jsp");
    dis.forward(request, response);

}

The process method extracts the stock from the request parameter and passes it to the getXMLForCode() method. This method calls the web services classes that obtain a live stock quote and returns it as an XML string. Then the method unmarshal() transforms the XML to a Stock object which is then stored as an attribute in the request object and forwarded to the JSP template.

3. The JSP is a template that uses tag libs to retrieve the stock quote data from the Stock object. I am going to use the core and format tags and have downloaded the libraries and put them in the WEB-INF/lib folder.

Core and Format tag libs

Core and Format tag libs

4. I import the tag libs into the JSP and using the format tags I format the data into the styles I require, for example, I want the currency to be in US dollars. The data is retrieved from the request object, formatted and then stored in the page scope.

I must also import the Stock object, which I do in the first line of the JSP code.

<%@ page language="java" import="demo.webservice.marshal.Stock" 
    contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<fmt:parseNumber value="${requestScope.StockQuote.change}"
    type="number"
    var="change" scope="page"
    pattern="-###.###;+###.###"/>
<fmt:formatNumber value="${requestScope.StockQuote.last}"
    type="currency"
    var="price"
    scope="page"
    currencyCode="USD"
    groupingUsed="false"/>
<fmt:formatNumber value="${requestScope.StockQuote.volume}"
    type="number"
    var="volume"
    scope="page"
    groupingUsed="true"/>
<fmt:formatNumber value="${requestScope.StockQuote.previousClose}"
    type="currency"
    var="previousClose"
    scope="page"
    currencyCode="USD"
    groupingUsed="false"/>

 

5. Now that we have the data formatted we can put in an HTML table.

<table>
<tr><td colspan="4" style="font-size: 1.5em;">
<c:out value="${requestScope.StockQuote.name}"/>
</td></tr>

<tr>
<td>Change:</td>
<td>
<c:out value="${ pageScope.change }"/>
<td>Price:</td>
<td>
<c:out value="${ pageScope.price }"/>
</td>
</tr>
<tr>
<td>Volume:</td>
<td>
<c:out value="${ pageScope.volume }"/>
</td>
<td>Previous close:</td>
<td>
<c:out value="${ pageScope.previousClose }"/>
</td>
</tr>

</table>

We now have a template in which the stock data will be displayed and a servlet to manage the request and response. The only thing that remains for us to do Step 4: HTML5 and AJAX (jQuery) interface.

3 Trackbacks / Pingbacks

  1. Step by step guide to setting up a J2EE web application | alex.theedom
  2. JSP, tag libs and servlet | alex.theedom
  3. JSP, tag libs and servlet Boost your career with us Professional Java EE Video Training and Tutorials

Leave a Reply

%d