Show TOC Start of Content Area

Procedure documentation Developing the Web Module  Locate the document in its SAP Library structure

Prerequisite

The Window Open Perspective Other Java EE perspective is open.

Creating the Web Module Project

The Web resources are developed in a Web Project in the Java EE perspective.

...

       1.      Choose File New Project… from the main menu.

       2.      Choose Web Dynamic Web Project on the New Project screen. Choose Next.

This graphic is explained in the accompanying text

       3.      Enter ConverterWeb in the Project Name field.

       4.      To assign the Web project to the previously created Enterprise Application Project, check Add Project to an EAR and select ConverterEAR from the EAR Project Name field.

This graphic is explained in the accompanying text

       5.      Choose Finish to create your Web’s project structure.

Creating a Web Client

The Web client in this tutorial is a JSP.

...

       1.      In the Project Explorer choose ConverterWeb   WebContent.

       2.      From the context menu of WebContent choose New Other.

       3.      On the New screen choose Web JSP. Choose Next.

       4.      Enter index.jsp in the File Name field.

       5.      Choose Finish.

       6.      Implement the JSP as follows:

<%@ page import="com.sap.tutorial.javaee.ConverterLocal,

java.math.*, javax.naming.*"%>

 

<%!

  private ConverterLocal converterBean = null;

  public void jspInit() {

    try {

     InitialContext ic = new InitialContext();

     converterBean = (ConverterLocal)

     ic.lookup("java:comp/env/Converter");

    } catch (Exception ex) {

       System.out.println("Couldn't create converter bean."+

       ex.getMessage());

    }

  }

 

 public void jspDestroy() {

  converterBean = null;

  }

 

%>

<html>

<head>

<title>Currency Converter</title>

</head>

 

<body>

<h1> Currency Converter</h1>

<hr>

<p>Enter an amount to convert:</p>

<form method="get">

<input type="text" name="amount" size="20">

<input type="submit" value="Submit">

</form>

<%

  String amount = request.getParameter("amount");

  if ( amount != null && amount.length() > 0 ) {

  BigDecimal value = new BigDecimal(amount);

  BigDecimal euroAmount = converterBean.dollarToEuro(value);

  %>

 

<p>

<%= amount %> USD are <%= euroAmount %> EUR

<br>

<%

  BigDecimal dollarAmount =

  converterBean.euroToDollar(value);

%>

<%= amount %> EUR are <%= dollarAmount %> USD

</p>

<%

  }

%>

</body>

</html>

       7.      Save the index.jsp file.

Editing the web.xml Deployment Descriptor

...

       1.      Expand the WEB-INF folder under WebContent.

       2.      Double-click on the web.xml file to open it for editing.

       3.      Add the following code in the XML source to define a local EJB reference to Converter bean:

<ejb-local-ref>

  <ejb-ref-name>Converter</ejb-ref-name>

  <ejb-ref-type>Session</ejb-ref-type>

    <local>com.sap.tutorial.javaee.ConverterLocal</local>

  </ejb-local-ref>

       4.      To save the file, choose File Save from the main menu.

Configuring the Project References

Since your Web project uses resources from the ConverterEJB project, you have to set these references:

...

       1.      Select ConverterWeb in the Project Explorer.

       2.      From the context menu choose Properties.

       3.      On the Properties for ConverterWeb screen choose Java Build Path Projects.

       4.      Choose Add.

       5.      Select ConverterEJB.

This graphic is explained in the accompanying text

       6.      Choose Project References.

       7.      On the Setting Java Build Path screen that pops up, choose Apply.

       8.      Select the ConverterEAR and ConverterEJB projects.

This graphic is explained in the accompanying text

       9.      Choose OK.

Result

You have successfully created the Web module of the Converter Application.

End of Content Area