The JavaTM Web Services Tutorial
Home
TOC
Index
PREV TOP NEXT
Divider

Creating the Getting Started Application

The example application contains a ConverterBean class, 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 named gs/. All of the files in this example application are created from this root directory.

The ConverterBean Component

The ConverterBean component 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 the ConverterBean component is in the <JWSDP_HOME>/docs/tutorial/examples/gs/src/converterApp/ directory.

Coding the ConverterBean Component

The ConverterBean component for this example contains two properties, yenAmount and euroAmount, and the set and get methods for these properties. The source code for ConverterBean 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:

The source code for index.jsp follows.

<%-- 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>
 
Divider
Home
TOC
Index
PREV TOP NEXT
Divider

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.