使用NetWeaver开发Web模块

Prerequisite

The Window ® Open Perspective ® Other ® J2EE perspective is open.

Creating the Web Module Project

The Web resources are developed in a Web Project in the J2EE perspective.

...

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

       2.      Choose Web ® Dynamic Web Project 2.5 on the New Project screen.

       3.      Choose Next to proceed to the Create a Dynamic Web 2.5 Project screen.

       4.      Enter ConverterWEB in the Project Name field.

       5.      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.

       6.      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.

       4.      Choose Next.

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

       6.      Choose Finish.

       7.      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>

       8.      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 of ConverterWEB screen choose Java Build Path ® Projects.

       4.      Choose Add.

       5.      Select ConverterEJB.

       6.      Choose OK.

       7.      Choose Project References.

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

       9.      Select the ConverterEAR and ConverterEJB projects.

   10.      Choose OK.

Result

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

posted @ 2006-12-22 17:47  我的IT世界  阅读(213)  评论(0编辑  收藏  举报