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

Slides:



Advertisements
Similar presentations
A plataforma J2EE.
Advertisements

12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.
J.Sant Servlets Joseph Sant Sheridan Institute of Technology.
Servlets Stoney Jackson
An introduction to Java Servlet Programming
Overview of The Java Platform Solution for E-Business Applications : JSP, Servlet and EJB.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
Writing Enterprise Applications with J2EE (Third lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Writing Enterprise Applications with J2EE (Fifth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Introduction to Servlet & JSP
Chapter 4 Servlets Concept of Servlets (What, Why, and How) Servlet API Third-party tools to run servlets Examples of Using Servlets HTML tag with GET.
Servlets. Our Project 3-tier application Develop our own multi-threaded server Socket level communication.
Java Servlets and JSP.
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
Java Server Pages B.Ramamurthy. Topics for Discussion 8/20/20152 Inheritance and Polymorphism Develop an example for inheritance and polymorphism JSP.
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
4-Tier Model Client Tier Web Tier Business Tier EIS Tier.
Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container.
1 Session Bean Chuyên đề Lập trình Java & J2EE Chương 14 Biên soạn: Th.S Nguyễn văn Lành.
J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,
Writing Enterprise Applications with J2EE (Second lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Advanced Java Session 7 New York University School of Continuing and Professional Studies.
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Objectives Java Servlet Web Components
CSC 2720 Building Web Applications
Enterprise JavaBeans Umer Farooq CS6704: Design Patterns & Component FrameworksFebruary 25, 2002.
Java Web Development with NetBeans IDE -- Kai Qian Chapter 6 Session Beans.
Presented By Pradeep K Sahu. What will be the Contents of the Seminar ? What is EJB ? EJB Architecture Types of EJB Session Entity Why EJB ? Writing a.
Enterprise JavaBeans. Lesson 1: Introduction to Server-Side Component Software.
Self-executing Java, J2EE James Atlas July 29, 2008.
Introduction to J2EE Architecture Portions by Kunal Mehta.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
1 Introduction to J2EE CS : Software Design Winter /T13 Based on: Advanced Web Applications Development, cs © Eliezer Dekel, Sara.
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
Session Beans Objectives Introduction to Session Beans Local and Remote Session Beans Stateless and Stateful Session Beans Session Bean Lifecycle Accessing.
Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
A TUTORIAL TO USING EJBs by SHREERAM IYER 09/17/2001.
Java Servlets & Java Server Pages Lecture July 2013.
Enterprise JavaBeans Understanding EJB Components Version 0.1 Kamal Wickramanayake
Writing Enterprise Applications with J2EE (Fourth lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
© jGuru.com Enterprise JavaBeans Fundamentals.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
Java Servlet API CGI / HTTP Concepts Java Servlet API.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
Entity Beans & Persistence Chris Alexander CS 486 Spring 2001.
CS320 Web and Internet Programming Introduction to Java Servlets Chengyu Sun California State University, Los Angeles.
Session Beans Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Java Servlets and Java Server Pages
HTTP protocol Java Servlets. HTTP protocol Web system communicates with end-user via HTTP protocol HTTP protocol methods: GET, POST, HEAD, PUT, OPTIONS,
Session Beans Objectives Introduction to Session Beans Local and Remote Session Beans Stateless and Stateful Session Beans Session Bean Lifecycle Accessing.
Enterprise Java Beans N.V.RAJASEKHAR REDDY. Definition of EJB EJBs are the components that are the set of classes and interfaces deployed within a container.
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
Enterprise JavaBeans: Fundamentals. EJB Fundamentals(c)CDAC(Formerly NCST)2 Contents Introduction Technology Overview EJB Architecture EJB Specification.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
14 Copyright © 2004, Oracle. All rights reserved. Achieving State Management in the Business Tier.
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
©NIIT Session Beans Lesson 1B/ Slide 1 of 37J2EE Server Components Objectives In this lesson, you will learn to: Describe the characteristics of session.
Introduction to Servlets
Servlet Fudamentals.
Java Servlets By: Tejashri Udavant..
J2EE Application Development
Objectives In this lesson, you will learn to:
Objectives In this lesson you will learn about: Need for servlets
Enterprise Java Beans Bina Ramamurthy 1/13/2019 B.Ramamurthy.
Enterprise Java Beans Bina Ramamurthy 4/5/2019 B.Ramamurthy.
Presentation transcript:

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

Thin-Client Multitiered Application

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.

PATH and CLASSPATH Path Settings Unix: [..]/J2EE/jdk1.2.2/bin [..]/J2EE/j2sdkee1.2.1/bin Windows: replace / with \ Class Path Settings Unix: [..]/J2EE/j2sdkee1.2.1/lib/j2ee.jar Windows: replace / with \

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

Interacting Components The session bean executes in the J2EE application server. Data flows between the browser and the session bean.

HTML Code Bonus Calculation <FORM METHOD="GET" ACTION="BonusAlias"> Enter social security Number: Enter Multiplier:

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.

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(); }

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(" "); out.println(title); out.println(" "); 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 = ; theCalculation = homecalc.create(); calc = theCalculation.calcBonus(multiplier, bonus); } catch(Exception CreateException){CreateException.printStackTrace(); }

Servlet Code (III) //cont. for public void doGet ( //Display Data out.println(" Bonus Calculation "); out.println(" Soc Sec: " + socsec + " "); out.println(" Multiplier: " + multiplier + " "); out.println(" Bonus Amount: " + calc + " "); out.println(" "); out.close(); } public void destroy() { System.out.println("Destroy"); }

Interfaces of a EJBean This 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.

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.

CalcHome BonusServlet 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; }

Calc When 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; }

CalcBean The 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() { } }

Compiling Instructions Session Bean Unix #!/bin/sh cd [..]/J2EE J2EE_HOME=[..]/J2EE/j2sdkee1.3.1 CPATH=.:$J2EE_HOME/lib/j2ee.jar javac -d. -classpath "$CPATH" Beans/CalcBean.java Beans/CalcHome.java Beans/Calc.java Windows cd \home\monicap\J2EE Set J2EE_HOME= [..] \J2EE\j2sdkee1.3.1 set CPATH=.;%J2EE_HOME%\lib\j2ee.jar javac -d. -classpath %CPATH% Beans/CalcBean.java Beans/CalcHome.java Beans/Calc.java Servlet Unix [..] javac -d. -classpath "$CPATH" BonusServlet.java Windows [..] javac -d. -classpath "$CPATH" BonusServlet.java

J2EE Application Server & Deploy Tool You need to start the J2EE application server to deploy and run the example. type: j2ee -verbose Note: 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: deploytool Note: If a memory access error is encountered when starting deploytool, add an environment variable called JAVA_FONTS and set the path to c: \, e.g. c:\winnt\fonts.

The Deploy Tool GUI

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

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.

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

Create Session Bean (II) In this step, make sure the following information is selected: classname: CalcBean Home interface: Beans.CalcHome Remote interface: Calc Bean type: Session and Stateless Specify 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..

Creation of the Session Bean Once the session bean has been created, the GUI looks as shown.

Create Web Component (I) Web components (servlets, or JSP  technology) 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.

Create Web Component (II) At the next step of the wizard, select the BonusServlet as a servlet component. Now, the GUI looks as shown.

Component Aliases At 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.

Components’s Deployment: Summary

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.

Specify Root Context

Verify Specification Compliance

Deploy the J2EE Appl.

Run the J2EE Appl. The web server runs on port 8000 by default. To open the bonus.html page point your browser to which is where the Deploy tool put the HTML file.