34
Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

  • View
    217

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

WritingEnterprise Applications

with J2EE(First lesson)

Alessio Bechini

June 2002(based on material by Monica Pawlan)

Page 2: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

A Simple Session Bean• Example Thin-Client

Multitiered Application

• Path and ClassPath Settings

• J2EE Application Components

• Create the HTML Page

• Create the Servlet

• Create the Session Bean

• Compile the Session Bean and Servlet

• Start the J2EE Application Server

• Start the Deploy Tool

• Deploy Tool

• Assemble the J2EE Application

• Verify and Deploy the J2EE Application

• Run the J2EE Application

• Updating Component Code

Page 3: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Thin-Client Multitiered Application

Page 4: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

J2EE Download: Contents

• The typical download has the J2EE application server, Cloudscape database, a Web server using secure socket layer (SSL) also known as HTTP over HTTPS, development and deployment tools, and the Java APIs for the Enterprise.

Page 5: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

PATH and CLASSPATH

Path SettingsUnix:[..]/J2EE/jdk1.2.2/bin

[..]/J2EE/j2sdkee1.2.1/bin

Windows: replace / with \

Class Path SettingsUnix:[..]/J2EE/j2sdkee1.2.1/lib/j2ee.jar

Windows: replace / with \

Page 6: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

J2EE Appl. Components

The J2EE specification defines the following application components:

• Application client components

• Enterprise JavaBeans components

• Servlets and JavaServer Pages components (also called Web components)

• Applets

Page 7: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Interacting Components

• The session bean executes in the J2EE application server.

Data flows between the browser and the session bean.

Page 8: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

HTML Code

<HTML>

<BODY BGCOLOR = "WHITE">

<BLOCKQUOTE>

<H3>Bonus Calculation</H3>

<FORM METHOD="GET"

ACTION="BonusAlias">

<P>

Enter social security Number:

<P>

<INPUT TYPE="TEXT" NAME="SOCSEC"></INPUT>

<P>

Enter Multiplier:

<P>

<INPUT TYPE="TEXT" NAME="MULTIPLIER"></INPUT>

<P>

<INPUT TYPE="SUBMIT" VALUE="Submit">

<INPUT TYPE="RESET">

</FORM>

</BLOCKQUOTE>

</BODY>

</HTML>

Page 9: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Servlet Duties

At run time, the servlet code does the following:

• Retrieves the user data• Looks up the session bean• Passes the data to the session bean• Upon receiving a value back

from the session bean, creates an HTML page to display the returned value to the user.

Page 10: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Servlet Code (I)import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import javax.naming.*;

import javax.rmi.PortableRemoteObject;

import Beans.*;

public class BonusServlet extends HttpServlet {

CalcHome homecalc;

public void init(ServletConfig config) throws ServletException {

//Look up home interface

try{

InitialContext ctx = new InitialContext();

Object objref=ctx.lookup("calcs");

homecalc = (CalcHome)PortableRemoteObject.narrow(objref,CalcHome.class);

} catch (Exception NamingException) {

NamingException.printStackTrace();

}

}

}

Page 11: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Servlet Code (II)public void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String socsec = null; int multiplier = 0; double calc = 0.0; PrintWriter out; response.setContentType("text/html"); String title = "EJB Example"; out = response.getWriter(); out.println("<HTML><HEAD><TITLE>"); out.println(title); out.println("</TITLE></HEAD><BODY>"); try{ Calc theCalculation;//Get Multiplier and Soc.Sec. Info String strMult=request.getParameter("MULTIPLIER"); Integer integerMult = new Integer(strMult); multiplier = integerMult.intValue(); socsec = request.getParameter("SOCSEC");//Calculate bonus double bonus = 100.00; theCalculation = homecalc.create(); calc = theCalculation.calcBonus(multiplier, bonus); } catch(Exception CreateException){CreateException.printStackTrace(); }

Page 12: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Servlet Code (III)

//cont. for public void doGet (

//Display Data

out.println("<H1>Bonus Calculation</H1>");

out.println("<P>Soc Sec: " + socsec + "<P>");

out.println("<P>Multiplier: " + multiplier + "<P>");

out.println("<P>Bonus Amount: " + calc + "<P>");

out.println("</BODY></HTML>");

out.close();

}

public void destroy() {

System.out.println("Destroy");

}

}

Page 13: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Interfaces of a EJBeanThis picture shows how the servlet and session bean application components work as a complete J2EE application once they are assembled and deployed. The container is the interface between the session bean and the low-level platform-specific functionality that supports the session bean. The container is created during deployment.

Page 14: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Creating a session bean

A session bean represents a transient conversation with a client. If the server or client crashes, the session bean and its data are gone.

In contrast, entity beans are persistent and represent data in a database. If the server or client crashes, the underlying services ensure the entity bean data is saved.

Because the enterprise bean performs a simple calculation at the request of BonusServlet, and the calculation can be reinitiated in the event of a crash, it makes sense to use a session bean in the current example.

Page 15: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

CalcHomeBonusServlet does not work directly with the session bean,

but creates an instance of its home interface.

The home interface extends EJBHome and has a create method

for creating the session bean in its container. CreateException is thrown if the session bean cannot be created,

and RemoteException is thrown if a communications-related exception occurs during the execution of a remote method.

package Beans;

import java.rmi.RemoteException;

import javax.ejb.CreateException;

import javax.ejb.EJBHome;

public interface CalcHome extends EJBHome {

Calc create() throws CreateException, RemoteException;

}

Page 16: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

CalcWhen the home interface is created, the J2EE application server

creates the remote interface and session bean.

The remote interface extends EJBObject and declares the calcBonus method for calculating the bonus value.

This method is required to throw javax.rmi.RemoteException, and is implemented by the CalcBean class.

package Beans;

import javax.ejb.EJBObject;

import java.rmi.RemoteException;

public interface Calc extends EJBObject {

public double calcBonus(int multiplier, double bonus)

throws RemoteException;

}

Page 17: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

CalcBeanThe session bean class implements the SessionBean interface and provides behavior for the calcBonus method. The setSessionContext and ejbCreate methods are called in that order by the container after BonusServlet calls the create method in CalcHome.

package Beans;

import java.rmi.RemoteException;import javax.ejb.SessionBean;import javax.ejb.SessionContext;

public class CalcBean implements SessionBean { public double calcBonus(int multiplier, double bonus) { double calc = (multiplier*bonus); return calc; }//These methods are not described here public void ejbCreate() { } public void setSessionContext(SessionContext ctx) { } public void ejbRemove() { } public void ejbActivate() { } public void ejbPassivate() { } public void ejbLoad() { } public void ejbStore() { }

}

Page 18: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Compiling InstructionsSession Bean

Unix#!/bin/shcd [..]/J2EEJ2EE_HOME=[..]/J2EE/j2sdkee1.3.1CPATH=.:$J2EE_HOME/lib/j2ee.jarjavac -d . -classpath "$CPATH" Beans/CalcBean.java Beans/CalcHome.java

Beans/Calc.java

Windowscd \home\monicap\J2EESet J2EE_HOME=[..]\J2EE\j2sdkee1.3.1set CPATH=.;%J2EE_HOME%\lib\j2ee.jarjavac -d . -classpath %CPATH% Beans/CalcBean.java Beans/CalcHome.java

Beans/Calc.java

ServletUnix[..] javac -d . -classpath "$CPATH" BonusServlet.java

Windows[..] javac -d . -classpath "$CPATH" BonusServlet.java

Page 19: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

J2EE Application Server & Deploy Tool

• You need to start the J2EE application server to deploy and run the example.

type: j2ee -verboseNote: Sometimes the J2EE server will not start if Outlook is running.

• To assemble and deploy the J2EE application, you have to start the deploy tool.

type: deploytoolNote: If a memory access error is encountered when starting

deploytool, add an environment variable called JAVA_FONTS and set the path to c: \<font directory>, e.g. c:\winnt\fonts.

Page 20: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

The Deploy Tool GUI

Page 21: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Assemble the J2EE Application

Assembling a J2EE application involves creating a new application, and adding the application components to it, according to the following steps:

1. Create a new J2EE application (BonusApp.ear).

2. Create a new enterprise bean (CalcBean.jar).

3. Create a new web component (Bonus.war).

4. Specify JNDI name for the enterprise bean (calcs).

5. Specify the Root Context for the J2EE application (BonusRoot).

Page 22: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Create J2EE Application

J2EE components are assembled into J2EE application Enterprise Archive (EAR) files.

From File menu: Select New Application.

New Application dialog box:

Fill the data about the application file name (BonusApp.ear) and the application display name (BonusApp), specifying the ditectory where you want to place the application EAR file.

The GUI looks as shown.

Page 23: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Create Session Bean (I)

Enterprise beans (entity and session beans) are bundled into a Java Archive (JAR) file.

File menu: Select New Enterprise Bean. The New Enterprise Bean Wizard starts and displays an Introduction dialog box.Click Next.

EJB JAR dialog box: Specify the jar display name (CalcJar, within BonusApp), and possibly other ancillary info.

Add Files to Contents dialog box:Calc.class, CalcHome.class,

CalcBean.class.

The GUI looks as shown.

Go to the next step

Page 24: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Create Session Bean (II)

In this step, make sure the following information is selected:

classname: CalcBeanHome interface: Beans.CalcHomeRemote interface: CalcBean type: Session and StatelessSpecify the display name (the name that appears when when the JAR file is added toBonusApp in the Local Applications window) Description: e.g., This JAR file contains the CalcBean session bean.No further info is needed for the bean..

Page 25: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Creation of the Session Bean

Once the session bean has been created, the GUI looks as shown.

Page 26: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Create Web Component (I)Web components (servlets, or JSPtechnology) are bundled into a WAR file.

File menu: Select New Web Component. The New Web Component Wizard starts.

Provide the name to be displayed for the WAR, and then add contents:

First, bonus.html

And then the servlet class,BonusServlet.class

The GUI looks as shown.

Go to the next step.

Page 27: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Create Web Component (II)

At the next step of the wizard, select the BonusServlet as a servlet component.

Now, the GUI looks as shown.

Page 28: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Component AliasesAt the “component aliases” step of the wizard, type “BonusAlias.” This is the same alias name you put in the ACTION field of the HTML form embedded in the bonus.html file.

Now, the GUI looks as shown.

Page 29: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Components’s Deployment: Summary

Page 30: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Specify JNDI Name

Before you can deploy the BonusApp application and its components, you have to specify the JNDI name BonusServlet uses to look up the CalcBean session bean.

Page 31: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Specify Root Context

Page 32: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Verify Specification Compliance

Page 33: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Deploy the J2EE Appl.

Page 34: Writing Enterprise Applications with J2EE (First lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)

Run the J2EE Appl.The web server runs on port 8000 by default. To open the bonus.html page point your browser to

http://localhost:8000/BonusRoot/bonus.htmlwhich is where the Deploy tool put the HTML file.