Entering content frame

Procedure documentation Scenario 3: Step 3 – Create a J2EE Application Locate the document in its SAP Library structure

In this step, developer 2 creates a simple web application that uses the technology library. This is done in the development configuration of the Application software component (SC), which is using the Technology SC.

Prerequisites

...

You have started the SAP NetWeaver Developer Studio.

Create New DCs

       1.      Open the Development Configurationperspective and switch to the Inactive DCs view.

       2.      To log on to the DTR, choose This graphic is explained in the accompanying text in the toolbar. Enter USER2 and the corresponding password.

Enterprise Application DC

...

       1.      Open the node of your development configuration Appl_dev and select the entry example.org_application. In the context menu, choose Create New DC.

       2.      In the Development Component Project wizard, enter the following:

                            a.      Vendor: example.org

                            b.      Name: application/tax

                            c.      Caption: Tax Application

                            d.      Type: J2EE ® Enterprise Application.

       3.      Choose Next.

The dialog window DTR Activity appears.

       4.      Choose New Activity.

       5.      Enter New J2EE Application as display name and choose OK.

The dialog window DTR Activity appears.

       6.      Choose Next.

The dialog window Enterprise Application Project appears.

       7.      Choose Finish.

The wizard will now create the component directly in the DTR as a shared component. TheJ2EE Development perspective appears and you see a new Enterprise Application project in the J2EE DC Explorer view.

Web Module DC

...

       1.      Switch back to the Active DCsview in the Development Configuration perspective.

       2.      Again choose Create New DC in the context menu of the example.org_application.

A wizard appears.

       3.      In the Development Component Project wizard, enter the following:

                            a.      Vendor: example.org

                            b.      Name: application/tax/servlet

                            c.      Caption: Tax Servlet

                            d.      Type: J2EE ® Web Module.

       4.      Choose Next.

The dialog window DTR Activity appears.

       5.      Select New J2EE Application and choose Next.

The dialog window Web Module Project appears.

       6.      Choose Finish.

The J2EE Developmentperspective appears and you should see a new Web Module project in the J2EE DC Explorer view.

Create a JSP

You are in the J2EE perspective in the J2EE DC Explorer view.

...

       1.      Open the project node in your Web Module DC (...application~tax~servlet~...).

       2.      In the context menu, choose New ® JSP.

       3.      In the dialog, enter tax and choose Finish.

A dialog window DTR Activity appears.

       4.      Select New J2EE Application and choose OK.

The JSP editor is opened automatically.

       5.      Switch to the Sourcepane and enter the following code:

 

<%@ page language="java" %>
<html>
<head><title>Tax Calculator</title></head>
<body bgcolor="#c8e3ff">
   <form action="tax.jsp" method="POST" >
        <table border="0" cellspacing="5" cellpadding="5" >
          <colgroup>
              <col width="20%"/>
              <col width="80%"/>
          </colgroup>
            <% 
            org.example.tax.web.TaxUIController controller= 
              new  org.example.tax.web.TaxUIController();
            controller.setIncome(request.getParameter("income"));
            %>
          <tr>
            <td>Income:</td> 
            <td>
              <input name="income" 
                value="<%= controller.formatIncome() %>"
                type="text" size="30" maxlength="40">
            </td>
          </tr>
          <tr>
            <td>Tax:</td>
            <td> <%= controller.formatTax() %> </td>
          </tr>
        </table>
        <table border="0" cellspacing="5" cellpadding="5">
          <colgroup>
              <col width="30%"/>
              <col width="40%"/>
              <col width="30%"/>
          </colgroup>
          <tr>
            <td></td>
            <td><input type="submit" value="calculate tax" ></td>
            <td></td>
          </tr>
        </table>
      </form>
  </body>
</html>

 

       6.      Save your entries.

Create a New Class

You are in the J2EE perspective in the J2EE DC Explorer view.

...

       1.      Open the Web module project node (...application~tax~servlet...) and select the entry Source.

       2.      From the context menu, choose New ® Java Class.

A dialog window Java Class appears.

       3.      Enter the following:

                            a.      Package: org.example.tax.web

                            b.      Name: TaxUIController

       4.      Choose Finish.

The dialog window Select Activity appears.

       5.      Select New J2EE Application and choose OK.

The Java editor is opened automatically.

       6.      Enter the following code:

 

package org.example.tax.web;

import java.text.NumberFormat;
import  java.util.Locale;
import  org.example.tax.ITaxCalculator;
import  org.example.tax.TaxFactory;


public class TaxUIController {
    private static  NumberFormat usCurrencyFormatter =
        NumberFormat.getCurrencyInstance(Locale.US);
    private double  income = 0;
    private double  tax = 0;


    public void setIncome(String s) {
        if  (s == null) {
            income = 0;
        } else  {
            try  {
                if  ((s.charAt(0) == '$') && (s.length() > 1)) {
                    s = s.substring(1);
                }
                s = s.trim();
                income = Double.parseDouble(s);
            } catch  (Exception e) {
                income = 0;
            }
        }
        ITaxCalculator calculator = TaxFactory.createInstance();
        tax = calculator.calculateTax(income);

    }

    public double getIncome() {
        return  income;
    }


    public double getTax() {
        return  tax;
    }


    public String formatIncome() {
        return  usCurrencyFormatter.format(income);
    }

    public  String formatTax() {
        return  usCurrencyFormatter.format(tax);
    }
}

 

       7.      Save your entries.

The class TaxUIController imports ITaxCalculator and TaxFactoryfrom the component technology/tax/calculator.

Note

You see compilation errors in the Tasks view: They will be repaired in the next step.

Create Use Dependencies to Another DC (Public Part api)

You are in the J2EE perspective in the J2EE DC Explorer view.

In order to build TaxUIController, you must create a dependency to component technology/tax/calculator.

...

       1.      Under ...application~tax~servlet..., open the node DC MetaData® DC Definition.

       2.      Select the node Used DCs. From the context menu, choose Add Used DC.

       3.      From the component tree, select the node of your development configuration ® example.org_technology ® technology/tax/calculator ® DC MetaData ® Public Parts ® api.

       4.      Select only the checkboxes Build-Time and Run Time remove other selections if necessary.

       5.      Choose Finish.

The TaxUIController class is rebuild automatically and all compilation errors should vanish.

Add Modules

You are in the J2EE perspective in the J2EE DC Explorer view.

...

       1.      Select the Enterprise Application Project node (...~application~tax~...) and in the context menu, choose Add Modules.

A dialog window appears.

       2.      Select the servlet project (...application~tax~servlet...) and choose OK.

You now add the servlet to your Enterprise Application.

Note

The enterprise application up to now has only a compile-time dependency to technology/tax/calculator.  In order to run this application, you must also create a runtime dependency to the J2EE component technology/tax.

Create a New Reference

You are in the J2EE perspective in the J2EE DC Explorer view.

...

       1.      Double-click the entry application-j2ee-engine.xml in your Enterprise Application Project (...~application~tax~...).

An editor appears.

       2.      Switch to the General tab

       3.      Select References.

       4.      Choose Add.

A dialog window appears.

       5.      Choose Create New...

       6.      Enter the following values for the new reference and save your changes:

                            a.      Referenced Target: technology/tax

                            b.      Reference type: hard

                            c.      Reference target type: library

                            d.      Provider Name: example.org

       7.      Save your entries.

 

 

 

 

Leaving content frame