Tutorial 2: Step 3 – Create a J2EE Application

In this step, developer 2 creates a simple web application that uses the technology library.

Prerequisites

...

You have started the SAP NetWeaver Developer Studio.

Create New DCs

       1.      Open the Development Configuration perspective and switch to the Inactive DCs view.

       2.      Login to the DTR by pressing the  button in the toolbar. Enter USER2 and the corresponding password when prompted.

Enterprise Application DC

...

       1.      Open the node of your development configuration Appl_dev and select the entry example.org_application.

       2.      In the context menu, choose Create New DC.

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

                            a.      Vendor: example.org 

                            b.      Name:

                            c.      (prefix from selection list):  appl/

                            d.      (text input): taxappl

                            e.      Caption: Tax Application

                              f.      Domain: Financial Services, if available

                            g.      Type: J2EE ® Enterprise Application.

       4.      Choose Next.

The dialog window DTR Activity appears.

       5.      Choose New Activity.

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

The dialog window DTR Activity appears.

       7.      Choose Next.

The dialog window Enterprise Application Project appears.

       8.      Choose Finish.

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

Web Module DC

...

       1.      Return to the Inactive DCs view 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:

                            c.      (prefix from selection list):  appl/

                            d.      (text input): tax/servlet

                            e.      Caption: Tax Servlet

                              f.      Domain: Financial Services, if available

                            g.      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 Development perspective 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 of your Web Module DC (...appli~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 Source pane 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 changes.

Create a New Class

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

...

       1.      Open the Web Module project node (...appl~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 changes.

The class TaxUIController imports ITaxCalculator und TaxFactory from component appl/tax/calc.

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

Create Use Dependencies on Another DC (Public Part api)

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

To build the TaxUIController, you must create a dependency to component appl/tax/calc.

...

       1.      Under the node of your DC (...appl~tax~servlet...), open the node DC MetaData ® DC Definition.

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

       3.      Select tab page Inactive.

       4.      Choose in the component tree Appl_dev ® example.org_application ® application/tax/calculator ® DC MetaData ® Public Parts ® api.

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

       6.      Choose Finish.

The TaxUIController is rebuilt and any compilation errors should now disappear.

Add Modules

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

...

       1.      Select the node of the Enterprise Application Project (...~appl~taxappl~...) and from the context menu, choose Add Modules.

A dialog window appears.

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

You now add the servlet to your Enterprise Application.

Up to now, the Enterprise Application has only one compile time dependency to appl/tax/calculator.  In order to run this application, you must also create a runtime dependency to the J2EE component application/tax.

Create a New Reference

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

...

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

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: appl/tax

                            b.      Reference type: hard

                            c.      Reference target type: library

                            d.      Provider Name: example.org

       7.      Save your changes.