Home  |  Software Components  |  Examples  |  Books  
The JavaServer (tm) page has five entry fields: present value, future value, interest rate, number of payments, and amortized payment. Enter any four and calculate the fifth.

Web Calculator Application

Part 1: Introduction

In this example, we will create a very simple web financial calculator application from reusable components. Users will see an HTML form like the one on the right when they access the application. They will typically fill in four of the five fields, select a field to calculate from the drop-down list near the bottom, and then click the Calculate button.

For example, If they want to find the monthly payment for a $200,000 loan at 7% for 30 years, they would:

  • enter 200,000 in Present Value
  • enter 0 in Future Value
  • enter 7 in Interest Rate
  • enter 360 (30x12) in Periods
  • select Payment from the drop-down list
  • click the Calculate button


Design

We will use the "Service to Worker" pattern. This pattern helps to separate the application control, business logic, and presentation details into distinct modules. Applications are easier to maintain when business logic and presentation are not mixed together in one module. Also, for larger applications, appropriate personnel can be assigned to each module. Software developers can work on the overall control and business logic while page layout experts can concentrate on the presentation.

The application will have five modules and components:

  • Controller/Dispatcher Servlet
  • TVM (Time Value Of Money) JavaBean
  • HashTable (for errors)
  • Calculator Form JSP
  • Calculator Errors JSP
All client requests are directed to a controller/dispatcher servlet. The controller part of the servlet validates each input parameter, populates the TVM bean, and invokes a method on the bean to calculate the result. If all goes well, the dispatcher will then forward the bean to the calculatorForm.jsp JavaServer page for display. If there are errors, the request and an error table will be forwarded to the calculatorErrors.jsp page for display instead. This cycle will continue until the user leaves the application.

The TVM bean contains all of the business logic and maintains the state of the calculator between requests. The controller will instantiate a new bean at the beginning of each session and place it into the session scope where it will be available to all other modules in the application during that session.

A HashTable will contain a field name key and error message pair for each error the controller discovers while validating the request. The dispatcher will place it in the request scope before forwarding the request to the Calculator Errors page.

Follow these links to learn more about each module:
Source Code:

Components:
  • TVM Time Value Of Money JavaBean
  • JSTL Java Standard Tag Library
 

Web Application Deployment Descriptor

After the controller servlet and the JSP pages have been written, create a Web Application Deployment Descriptor that describes the application to the server. The descriptor is an XML document that is always named web.xml. We will describe the servlet controller and map the url /calculator to the servlet name.

Web.xml



<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
  
  <display-name>Calculator</display-Name>

         <servlet>
   		<servlet-name>calculator</servlet-name>
		<servlet-class>CalculatorServlet</servlet-class>
	  </servlet>

	  <servlet-mapping>
		<servlet-name>calculator</servlet-name>
		<url-pattern>/calculator</url-pattern>
	  </servlet-mapping>

</web-app>

Web ARchive (WAR)

Next, create a directory structure that conforms to the WAR format and place all of the application resources within it. The name of the root directory (tools) will become the name of the application. All servers that conform to Servlet API Specification, version 2.2 should accept this format. It combines all the resources that the server needs to run an application into one convenient package that can be dropped onto the server. There is no need to set a classpath or do any other configuration.

The application resources consist of the two JSP pages, the TVM bean jar, the servlet controller, and the web.xml application descriptor. Arrange them in the root directory and sub-directories as follows:

tools/
    calculatorForm.jsp
    calculatorErrors.jsp
    standard.css
 
    META-INF/
       web.xml

    classes/
       ControllerServlet.class

    lib/
       TVM.jar
       bigdecimal.jar
       standard.jar
       jstl.jar


Deployment

Move the entire application directory structure (sometimes called an unpacked web archive) to the server. For a Tomcat server, it should be moved under the webapps/ subdirectory, but the location will vary for others. Then execute the application from any browser with the URL:

http://mycompany.com/tools/calculator

After the application has been tested, pack the directory into a Web ARchive (.war) file to make it more portable. A WAR file is just a JAR file with a .war extention that contains a directory hierarchy like the unpacked archive structure that was just created. Change the default to the tools directory and execute the jar utility:

jar -cvf tools.war .

This includes all the files and sub-directories under the tools directory, but not the directory itself.
Copyright ©1998-2004 GetObjects.com  All Rights Reserved
Privacy | Legal & Terms of Use | Trademarks | Feedback | About