IBM®
Skip to main content
    Country/region [select]      Terms of use
 
 
      
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks > WebSphere >
developerWorks
IBM WebSphere Developer Technical Journal: Developing and Deploying an End-to-end J2EE Application to JBoss Application Server using WebSphere Studio V5
Downloade-mail it!
Contents:
Introduction
The J2EE Publishing Server
Architecture for the Compound Interest Calculator application
Walking through the tutorial
Conclusion
Related information
Resources
Download
About the author
Rate this article
Subscriptions:
dW newsletters
dW Subscription
(CDs and downloads)

Derek Koo, P. Eng.
Software Engineer, WebSphere Studio Application Developer , IBM Toronto Lab
19 Feb 2003

This tutorial demonstrates how to use WebSphere Studio Application Developer, Version 5.0 to develop a complete J2EE application, consisting of a session EJB, a servlet, a JSP page, and an HTML home page. It then demonstrates how to deploy the application onto the open source JBoss application server (Version 3.0.4 integrated with Tomcat Version 4.1.12) by using WebSphere Studio's J2EE Publishing Server, and invoking the application or Web client from the workbench.

© Copyright International Business Machines Corporation 2003. All rights reserved.

Introduction
This tutorial demonstrates how to use IBM® WebSphere® Studio Application Developer, Version 5.0 (hereafter called WebSphere Studio) to develop a complete J2EE application, consisting of a session EJB, a servlet, a JSP page, and an HTML home page. It then demonstrates how to deploy the application onto the open-source JBoss application server (Version 3.0.4 integrated with Tomcat Version 4.1.12) by using WebSphere Studio's J2EE Publishing Server, and invoking the application or Web client from the workbench.

In this tutorial, you will build a J2EE application called Compound Interest Calculator . The application consists of a home page that contains three input fields for the user to enter the principal, interest rate, and period. When the user clicks the Calculate button from the home page, the application returns the result, a JSP page.

After completing the tutorial, you will know how to efficiently use WebSphere Studio tools to carry out the tasks. The purpose of this tutorial is not to teach you how to use WebSphere Studio to create J2EE applications -- many such articles already exist on WebSphere Developer Domain. Instead, this tutorial will demonstrate how you can easily integrate WebSphere Studio's J2EE Publishing Server tools with other J2EE-compliant servers.

The J2EE Publishing Server
The J2EE Publishing Server is one of the new server type features of the server tools set in WebSphere Studio Application Developer Version 5.0. This new server type lets you develop J2EE applications, deploy them to the remote J2EE compliant servers that require minimal configuration (for example, Apache, Tomcat, and JBoss servers), and then test the client applications from the WebSphere Studio workbench.

Architecture for the Compound Interest Calculator application
You will use the J2EE Model 2 (MVC, model-view-controller) architecture for this application, where servlets are used as controllers and JSPs are used to display the final output. The model also uses a JavaTM bean and session bean.

Walking through the tutorial
In this tutorial, you will create and configure the J2EE Publishing Server so that you can deploy the Compound Interest Calculator application to the JBoss application server. The download ZIP file below contains the already-created EAR project, EARProject.ear . You can simply import it into WebSphere Studio. After the EAR project is imported into the workbench, the J2EE navigator view of the application will look as follows:

Figure 1.
Screen capture showing the EARProject imported into WebSphere Studio

Creating the J2EE application
The CIC-src.zip file, provided in the download ZIP file below, contains all of the related Java, JSP, HTML, and XML source files for the Compound Interest Calculator application.

In this section, you will complete the following:

Step 1: Creating the EJB project and the IntSessionBean session bean

  1. In WebSphere Studio, create a new EJB V2.0 project called EJBProject , and a new EAR project called, EARProject . Click Finish , and accept all other defaults.
  2. Create the intSession session bean in the EJBProject.
  3. Add:

    public double calculateCompundInterest(double principle, double rate,
    double periods) throws RemoteException;


    interface to the IntSession.java file.

  4. Create a public method, calculateCompoundInterest(...), as shown in Listing 1 below (see the IntSessionBean.java file provided in the CIC-src.zip file).
Listing 1.

public double calculateCompoundInterest(double principle,
   double rate, double periods) {


System.out.println(this + "::calculateCompoundInterest(...) ");
return principle * Math.pow(1 + rate/100, periods) - principle;

}

Step 2: Creating an EJB client wrapper class, IntSessionWrapper.java

Create the IntSessionWrapper.java wrapper class for the intSession EJB in the EJBProject.

Step 3: Creating a Web project

  1. Create a new J2EE Web project called WEBProject . Use the existing EARProject as the Enterprise application project. Click Finish , and accept all other defaults.
  2. Go to the Java Build Path properties page of the WEBProject. In the Projects tab view, select the EJBProject check box (see Figure 2).

Figure 2.
Screen capture showing the EJBProject selected in the Projects tab view of the Java Build Path

Step 4: Creating the servlet, IntSessionServlet.java

  1. In the Java Source folder of WEBProject, create a Java package called, org.jboss.test.interest .
  2. Create the IntSessionServlet.java file in the package, org.jboss.test.interest.
  3. Now import the ejbPack1.IntSession, ejbPack1.IntSessionHome, and ejbPack1.IntSessionWrapper classes.

The doPost() method is shown below in Listing 2.

Listing 2.

public void doPost(HttpServletRequest request, 
   HttpServletResponse response)
     throws ServletException, IOException {
     System.out.println(this +"::doPost() ...");
     ServletContext sc;
     RequestDispatcher rd;
   
     String title = "Servlet interface to EJB";
     double principal = getValue("principal", 1000.0, request);
     double rate = getValue("rate", 10.0, request);
     double periods = getValue("periods", 2.0, request);
   
     IntSessionWrapper intSession = null;
     try {
       // instantiate the bean.
       intSession =
         (IntSessionWrapper) java.beans.Beans.instantiate(
           getClass().getClassLoader(),
           "ejbPack1.IntSessionWrapper");
       getServletContext().setAttribute("intSession", intSession);
   
       // Invoke calculateCompoundInterest action on the bean.
       intSession.calculateCompoundInterest(principal, rate, periods);
   
       SC = getServletContext();
       rd = sc.getRequestDispatcher("/IntSessionResult.jsp");
       rd.forward(request, response);
   
     }
     catch (Exception ex) {
       ex.printStackTrace();
     }
   
}

Step 5: Creating the JSP page, IntSessionResult.jsp

In the the Web Content folder of WEBProject, create the IntSessionResult.jsp file. You will use this page to display the result of the Compound Interest Calculator application in the Web browser.

Part of the code source is shown in Listing 3.

Listing 3.

<BODY>
<H2> Compound Interest Calculator Result: 
</H2>

<jsp:useBean id="intSession" class="ejbPack1.IntSessionWrapper"
   scope="application"></jsp:useBean>

<P> Interest on units,<%=intSession.getPrinciple() %> 
at <%= intSession.getRate() %>% per period, 
compounded over <%= intSession.getPeriod() %> periods 
is: <%= intSession.getResult() %>. 
</P> 
</BODY>
      

Step 6: Creating the welcome HTML page, home.html

In the Web Content folder of WEBProject, create the home.html page. You will use this page to display the input page for the Compound Interest Calculator application in the Web browser.

Part of the code source is shown in Listing 4.

Listing 4.

<body>
   <h1>Interest EJB Form</h1>
   <form action="IntSessionServlet" method="POST"   >
      <table cellspacing="2" cellpadding="2" border="0">
         <tr>
              <td>
                 Principal:
              </td>
              <td>
                 <input type="text" name="principal" value="1000">
              </td>
         </tr>
         <tr>
              <td>
                 Rate(%):
              </td>
              <td>
                 <input type="text" name="rate" value="10">
              </td>
         </tr>
         <tr>
              <td>
                 Periods:
              </td>
              <td>
                 <input type="text" name="periods" value="2">
              </td>
         </tr>
         <tr>
              <td>
                 <input type="submit" name="Calculate"
                    value="Calculate">
              </td>
              <td>
                 <input type="Reset">
              </td>
         </tr>
      </table>
   </form>
</body>

(Optional) Step 6b: Verifying the Web Deployment Descriptor, web.xml

Open the Web Deployment Descriptor file, web.xml , from the WEB-INF folder of WEBProject, and verify that all of the tag entries are displayed (see Listing 5).

Listing 5.

<?xml version="1.0" encoding="UTF-8"?>
   <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, 
      Inc.//DTD Web Application 2.3//EN" 
      "http://java.sun.com/dtd/web-app_2_3.dtd">
   <web-app id="WebApp">
       <display-name>WEBProject</display-name>
       <servlet>
           <servlet-name>IntSessionServlet</servlet-name>
           <display-name>IntSessionServlet</display-name>
           <servlet-class>org.jboss.test.interest.IntSessionServlet
              </servlet-class>
       </servlet>
       <servlet-mapping>
           <servlet-name>IntSessionServlet</servlet-name>
           <url-pattern>/IntSessionServlet</URL-pattern>
       </servlet-mapping>
       <welcome-file-list>
           <welcome-file>home.html</welcome-file>
           <welcome-file>index.html</welcome-file>
           <welcome-file>index.htm</welcome-file>
           <welcome-file>index.jsp</welcome-file>
           <welcome-file>default.html</welcome-file>
           <welcome-file>default.htm</welcome-file>
           <welcome-file>default.jsp</welcome-file>
       </welcome-file-list>
   </web-app>

Step 7: Creating the J2EE Publishing Server

  1. Go to WebSphere Studio's Server perspective by selecting Window => Open Perspective => Server . Then, create a new server project called, SVRProject .
  2. Create a new J2EE Publishing Server server and a server configuration called JBoss (see Figure 3).

    Figure 3.
    Screen capture showing the JBoss server configuration created using the Create a New Server and Server Configuration wizard

  3. Use galileo as the URL of the publishing directory, where galileo is the hostname of the machine that the JBoss server is going to be running on. Create a remote file transfer called galileoJBossNet with the Remote target directory set to: <JBoss_Install_Dir>/server/default/deploy . The value of <JBoss_Install_Dir> in this case is X:/jboss-3.04_tomcat-4.1.12 , where X:/ is the mapped network drive of the JBoss installation directory on the target machine. See Figure 4:

    Figure 4.
    Screen capture showing the Remote file transfer name set to galileoJBossNet in the Create a New Server and Server Configuration wizard

  4. On the last page of the Create a New Server and Server Configuration wizard, set the publishing server HTTP port number to 8080 .

Configuring the J2EE Publishing Server
In this section, you will complete the following:

Step 1: Adding the projects to the configuration

In the Server Configuration pane, add the projects, EARProject , EJBProject , and WEBProject to the JBoss server configuration (see Figure 5):

Figure 5.
Screen capture showing EARProject, EJBProject, and WEBProject added to the JBoss server configuration

Step 2: Editing the configuration

  1. Open and edit the JBoss configuration.
  2. Edit the Document root relative URL column of the Publishing Path Mapping table. The EAR, EJB, and Web projects must have their corresponding <EAR_Project_Name>.ear , <EAR_Project_Name>Ear/<EJB_Project_Name>.jar , and <EAR_Project_Name>Ear/<WEB_Project_Name>.war files respectively.
  3. Set the alias of WEBProject as /WEBProject . The server tool will use this alias to build the HTTP address for the Web client (see Figure 6).

Figure 6.
Screen capture showing the alias of WEBProject set as /WEBProject

Deploying and running the application
In this section, you will complete the following:

Step 1: Starting the JBoss server

Start the JBoss server in the target machine. From a command line of the target machine, change the directory to <JBoss_Install_Dir>\bin . Then, execute the run.bat command to start the JBoss Server.

Step 2: Publishing the JBoss server

From the server perspective, right-click on the JBoss server in the Servers pane, and click Publish from the drop-down menu (see Figure 7) to publish all of the application modules to the target JBoss server. The result of the publishing is shown in Figure 8 below.

Figure 7.
Screen capture showing Publish selected on the JBoss drop-down menu

Figure 8.
Screen capture showing that publishing was successful

The applications are now deployed from the JBoss server's console (see Figure 9):

Figure 9.
Screen capture showing the applications now deployed from the JBoss server's console

Step 3: Running the Web client

  1. Right-click on the home.html file of WEBProject, and click Run on server . The Server Selection dialog opens.
  2. Select the existing JBoss server, and click Finish to start the client (see Figure 10):

    Figure 10.
    Screen capture showing the JBoss server selected in the Server Selection wizard

  3. The Welcome/Home page of the application is now displayed in the Web browser (see Figure 11). Enter a value into the Principal, Rate, and Periods input fields, and click the Calculate button:

    Figure 11.
    Screen capture showing the Welcome page of the application displayed in the Web browser

  4. The application then returns the result page of the Compound Interest Calculator application (see Figure 12).

    Figure 12.
    Screen capture of the Compound Interest Calculator Result page displayed in the Web browser

Conclusion
This tutorial demonstrated how to develop an end-to-end, distributed, multi-tiered J2EE application using the server tools provided with WebSphere Studio Application Developer, Version 5.0. It also showed how you can use WebSphere Studio's J2EE Publishing Server to deploy the server code to the open source JBoss application server, and then run the client from WebSphere Studio.

Related information

Resources

Download
NameSizeDownload method
relatedfiles.zip0.07 MBFTP|HTTP
*Information about download methods
About the author
Derek Koo, P. Eng. is a software engineer working on the IBM WebSphere Studio Application Developer Server Tools team at the IBM Toronto Lab. Specifically, he is responsible for the J2EE Publishing Server and the Static Web Server. Previously, he was a member of the VisualAge for Java WebSphere Tools team, and was the component owner of the Servlet SmartGuide in VisualAge for Java Version 3.5. You can reach Derek at dkoo@ca.ibm.com .


Downloade-mail it!
Rate this article

This content was helpful to me:

Strongly disagree (1)Disagree (2)Neutral (3)Agree (4)Strongly agree (5)

Comments?



developerWorks > WebSphere >
developerWorks
    About IBM Privacy Contact