Web Calculator Application
Part 1: IntroductionIn 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:
DesignWe 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:
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:
Web Application Deployment DescriptorAfter 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.
<!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
DeploymentMove 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:
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:
|Copyright ©1998-2004 GetObjects.com All Rights Reserved