|
Home
TOC Index |
|
Creating the Getting Started Application
The example application contains a
ConverterBeanclass, a Web component, a file to build and run the application, and a deployment descriptor. For this example, we will create a top-level project source directory namedgs/. All of the files in this example application are created from this root directory.The ConverterBean Component
The
ConverterBeancomponent used in the example application is used in conjunction with a JSP page. The resulting application is a form that enables you to convert American dollars to Yen, and convert Yen to Euros. The source code for theConverterBeancomponent is in the <JWSDP_HOME>/docs/tutorial/examples/gs/src/converterApp/directory.Coding the ConverterBean Component
The
ConverterBeancomponent for this example contains two properties,yenAmountandeuroAmount, and thesetandgetmethods for these properties. The source code forConverterBean follows.//ConverterBean.java package converterApp; import java.math.*; public class ConverterBean{ private BigDecimal yenRate; private BigDecimal euroRate; private BigDecimal yenAmount; private BigDecimal euroAmount; /** Creates new ConverterBean */ public ConverterBean() { yenRate = new BigDecimal ("138.78"); euroRate = new BigDecimal (".0084"); yenAmount = new BigDecimal("0.0"); euroAmount = new BigDecimal("0.0"); } public BigDecimal getYenAmount () { return yenAmount; } public void setYenAmount(BigDecimal amount) { yenAmount = amount.multiply(yenRate); yenAmount = yenAmount.setScale(2,BigDecimal.ROUND_UP); } public BigDecimal getEuroAmount () { return euroAmount; } public void setEuroAmount (BigDecimal amount) { euroAmount = amount.multiply(euroRate); euroAmount = euroAmount.setScale(2,BigDecimal.ROUND_UP); } }The Web Client
The Web client is contained in the JSP page <
JWSDP_HOME>/docs/tutorial/examples/gs/web/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 theConverterBeanclass, and set the content type returned by the page.- The
jsp:useBeanelement declares that the page will use a bean that is stored within and accessible from the specified scope. The default scope ispage, so we do not explicitly set it in this example.- The
jsp:setPropertyelement is used to set JavaBeans component properties in a JSP page.- The
jsp:getPropertyelement is used to retrieve JavaBeans component properties in a JSP 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.<%-- index.jsp --%> <%@ page import="converterApp.ConverterBean,java.math.*" %> <%@ page contentType="text/html; charset=ISO-8859-5" %> <html> <head> <title>Currency Conversion Application</title> </head> <body bgcolor="white"> "<jsp:useBean id="converter" class="converterApp.ConverterBean"/> <h1><FONT FACE="ARIAL" SIZE=12>Currency Conversion Application </FONT></h1> <hr> <p><FONT FACE="ARIAL" SIZE=10>Enter an amount to convert:</p> </FONT> <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 ) { %> <p><FONT FACE="ARIAL" SIZE=10><%= amount %> dollars are <jsp:setProperty name="converter" property="yenAmount" value="<%= new BigDecimal(amount)%>" /> <jsp:getProperty name="converter" property="yenAmount" /> Yen. <p><%= amount %> Yen are <jsp:setProperty name="converter" property="euroAmount" value="<%= new BigDecimal(amount)%>" /> <jsp:getProperty name="converter" property="euroAmount" /> Euro. </FONT> <% } %> </body> </html>
|
Home
TOC Index |
|
This tutorial contains information on the 1.0 version of the Java Web Services Developer Pack.
All of the material in The Java Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.