Show TOC

Procedure documentationDeveloping the Web Module Locate this document in the navigation structure

Prerequisites

The   Window   Open Perspective   Other   Java EE   perspective is open.

Procedure

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 ConverterW   eb   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:

    Syntax Syntax

    1. <%@ page import="com.sap.tutorial.javaee.*,java.math.*,javax.naming.*"%>
      
      <%!private ConverterBeanLocal converterBean = null;
      
      	public void jspInit() {
      		try {
      			InitialContext ic = new InitialContext();
      			converterBean = (ConverterBeanLocal) ic
      					.lookup("java:comp/env/ConverterBean");
      		} 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>
      
    End of the code.
  7. Save the index.jsp file.

Editing the web.xml Deployment Descriptor
  1. Expand the WEB-INF folder under WebContent.

  2. Double-click 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:

    Syntax Syntax

    1. lt;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>
      
    End of the code.
  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. In the context menu, choose Properties.

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

  4. Choose Add.

  5. Select the ConverterEJBcheckbox. Choose OK.

    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.