|
Home TOC |
|
Creating the Getting Started Application
The example application contains a
Converterclass and a Web component. For this example, we will create a top-level project source directory namedgs/. All of the files in this example application are created in this directory.The Converter Class
The
Converterclass used in the example application is used in conjunction with a JavaServer Pages page. The resulting application is a form that enables you to convert American dollars to Euros or Yen. The source code for theConverterclass is in the <JWDSP_HOME>/docs/examples/gs/directory.Coding the Converter Class
The
Converterclass for this example implements two methods,dollarToYenandyenToEuro. The source code for theConverterclass follows.import java.math.*; public class Converter { static BigDecimal yenRate = new BigDecimal("138.7800"); static BigDecimal euroRate = new BigDecimal("0.0084"); public static BigDecimal dollarToYen(BigDecimal dollars) { BigDecimal result = dollars.multiply(yenRate); return result.setScale(2,BigDecimal.ROUND_UP); } public static BigDecimal yenToEuro(BigDecimal yen) { BigDecimal result = yen.multiply(euroRate); return result.setScale(2,BigDecimal.ROUND_UP); } public Converter() {} }The Web Client
The Web client is contained in the JSP page <
JWDSP_HOME>/docs/tutorials/examples/gs/index.jsp. A JSP page is a text-based document that contains both static and dynamic content. The static content is the template data that can be expressed in any text-based format, such as HTML, WML, or XML. JSP elements construct the dynamic content.Coding the Web Client
The JSP page,
index.jsp, is used to create the form that will appear in the Web browser when the application client is running. This JSP page is a typical mixture of static HTML markup and JSP elements. If you have developed Web pages, you are probably familiar with the HTML document structure statements (<head>,<body>, and so on) and the HTML statements that create a form<form>and a menu<select>. The highlighted lines in the example contain the following types of JSP constructs:
- Directives (
<%@page ... %>) import classes in theConverterclass, and set the content type returned by the page.- Scriptlets (
<% ... %>) retrieve the value of theamountrequest parameter, convert it to aBigDecimal, and convert the value to Yen or Euro.- Expressions (
<%= ... %>) insert the value of theamountinto the response.The source code for
index.jspfollows.<%@ page import="Converter,java.math.*" %> <%@ page contentType="text/html; charset=ISO-8859-1" %> <html> <head> <title>Converter</title> </head> <body bgcolor="white"> <h1><center>Converter</center></h1> <hr> <p>Enter an amount to convert:</p> <form method="get"> <input type="text" name="amount" size="25"> <br> <p> <input type="submit" value="Submit"> <input type="reset" value="Reset"> </form> <% String amount = request.getParameter("amount"); if ( amount != null && amount.length() > 0 ) { BigDecimal d = new BigDecimal (amount); %> <p><%= amount %> dollars are <%= Converter.dollarToYen(d) %> Yen. <p><%= amount %> Yen are <%= Converter.yenToEuro(d) %> Euro. <% } %> </body> </html>
|
Home TOC |
|