Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comp2513 JavaBeans, EJB and J2EE Daniel L. Silver, Ph.D.

Similar presentations


Presentation on theme: "Comp2513 JavaBeans, EJB and J2EE Daniel L. Silver, Ph.D."— Presentation transcript:

1 Comp2513 JavaBeans, EJB and J2EE Daniel L. Silver, Ph.D.

2 2001Daniel L. Silver2 Objectives To understand how JSPs work with JavaBeans To understand how JSPs work with JavaBeans To review examples of JSPs and JavaBeans To review examples of JSPs and JavaBeans To introduce Enterprise JavaBeans, EJBs To introduce Enterprise JavaBeans, EJBs To describe J2EE technologies To describe J2EE technologies Paper Reference: EJP - Ch. 13, PJEC - Ch.5 Paper Reference: EJP - Ch. 13, PJEC - Ch.5 Web Reference: Servlet and JSP Programming with IBM Websphere - Chapter 5 Web Reference: Servlet and JSP Programming with IBM Websphere - Chapter 5Servlet and JSP Programming with IBM WebsphereServlet and JSP Programming with IBM Websphere

3 2001Daniel L. Silver3 The JSP Operational Model 1. HTTP server receives request for.jsp file 2. The.jsp file is located (can be in any directory) 3. Tomcat is called and passed the.jsp file 4. Tomcat invokes a special servlet called pageCompile (the JSP engine, comes as part of JSP distribution) 5. pageCompile builds a servlet based on JSP code (that may include a reference to a JavaBean) 6. The servlet.java and.class files are written to disk (internal to Tomcat) 7. The.class file is processed by the JVM within Tomcat like any other servlet 8. HTML code is generated by the servlet and returned

4 2001Daniel L. Silver4 Tomcat Java Servlet Request Processing Internet Browser Client HTTP Server HelloWorld.jsp mod_jserv http://eagle.acadiau.ca/store05/HelloWorld.jsp Tomcat App. Server../Store05/HelloWorld.jsp HTML HelloJSP1.class../Store05/WEB-INF/classes/exampleBean/HelloJSP1 HelloWorld.java HelloWorld.class 1 8 2 4 3 6 5 7 pageCompile

5 2001Daniel L. Silver5 Putting it all Together DateDisplay is a basic JSP that you can begin to learn from DateDisplay is a basic JSP that you can begin to learn from DateDisplay is DateDisplay is Here is a link to the JSP source Here is a link to the JSP sourceJSP sourceJSP source Here is a link to the Java source resulting from the JSP compilation Here is a link to the Java source resulting from the JSP compilationJava sourceJava source

6 2001Daniel L. Silver6 JavaBeans API The SUN JavaBean API provides a powerful mechanism for software reuse and automated support (e.g. introspection = beans can tell IDEs about themselves) JavaBean APIJavaBean API Defines how to write components in Java that are self- contained, reusable software units that can be visually composed into composite components, applets, applications, and servlets Defines how to write components in Java that are self- contained, reusable software units that can be visually composed into composite components, applets, applications, and servlets A JavaBean is Java.class file that contains data (properties) and methods that follow certain coding conventions A JavaBean is Java.class file that contains data (properties) and methods that follow certain coding conventions Under JSP – the use of Beans is the preferred method of separating static HTML from dynamic Java code and business logic Under JSP – the use of Beans is the preferred method of separating static HTML from dynamic Java code and business logic

7 2001Daniel L. Silver7 JSP useBean Tag There are standard action JSP tags that allow you to work with pre-defined JavaBeans There are standard action JSP tags that allow you to work with pre-defined JavaBeans The syntax for referencing a JavaBean within a JSP can be of two forms: The syntax for referencing a JavaBean within a JSP can be of two forms: or [option scriptlets and tags] [option scriptlets and tags]

8 2001Daniel L. Silver8 JSP useBean Tag The basic syntax for referencing a JavaBean within a JSP is as follows: The basic syntax for referencing a JavaBean within a JSP is as follows:<jsp:useBean id = "textProvider" id = "textProvider" class = "exampleBean.HelloJSP1" class = "exampleBean.HelloJSP1"exampleBean.HelloJSP1 scope = “request" scope = “request"/> If an instance of HellpJSP1 does not already exist then one is created If an instance of HellpJSP1 does not already exist then one is created Object name used within JSP (case sensitive) Name of the object’s implementation class See next slide

9 2001Daniel L. Silver9 JSP useBean Tag ScopeDescription Page Object is accessible only by single client from this page until a response is returned request Object is accessible by single client for lifetime of client request (possibly several pages) until a response is returned session Object is accessible by single client from anywhere in the application for lifetime of client session (possibly several requests). The page in which you create the Bean must have a directive with session=true. application Object is accessible by any client from anywhere in the application for lifetime of the application

10 2001Daniel L. Silver10 Other Useful JSP Tags Include Tag - includes the output of a servlet in a JSP Include Tag - includes the output of a servlet in a JSP Forward Tag – forwards processing from a JSP to a servlet Forward Tag – forwards processing from a JSP to a servlet

11 2001Daniel L. Silver11 Getting Bean Properties Other tags allow you to get or set Bean properties (data attributes) … getProperty tag: JSP code: JSP code: Java Bean (servlet) code: Java Bean (servlet) code: public String getTextMessage() { Date now = new Date(); Date now = new Date(); return "Hello World!... It is now " + now.toString() + "."; return "Hello World!... It is now " + now.toString() + "."; }

12 2001Daniel L. Silver12 Setting Bean Properties Using the setProperty Tag: JSP code: JSP code: Java Bean (servlet) code: Java Bean (servlet) code: public void setA(int value) { a = value; a = value; }

13 2001Daniel L. Silver13 JSP Bean Tags in Action Lets have a look at the useBean and getProperty tags in action within http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp Lets have a look at the useBean and getProperty tags in action within http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp For JSP source see (use browser’s source view) http://eagle.acadiau.ca/demo/jsp/HelloJSP1_jsp.txt http://eagle.acadiau.ca/demo/jsp/HelloJSP1_jsp.txt Note that the example demonstrates how a bean property value can be obtained using the getProperty tag or by an expression: Note that the example demonstrates how a bean property value can be obtained using the getProperty tag or by an expression:

14 2001Daniel L. Silver14 JSP Bean Tags in Action Lets have a look at the setProperty tag in action within http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp First we set the property values of a bean “calcApB.class” using First we set the property values of a bean “calcApB.class” usingcalcApB.class Java Bean (servlet) code: Java Bean (servlet) code: public void setA(int value) { a = value; a = value; } Then we ask the bean to calculate A + B Then we ask the bean to calculate A + B

15 2001Daniel L. Silver15 JSP Bean Tags in Action Lets have a look at passing parameters from forms within http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp Lets have a look at passing parameters from forms within http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp We call another JSP “calcApB.jsp” from an HTML form passing the values of a and b We call another JSP “calcApB.jsp” from an HTML form passing the values of a and bcalcApB.jsp Within calcApB.jsp we call the bean “calcApB” and pass the values as per the JavaBean API Within calcApB.jsp we call the bean “calcApB” and pass the values as per the JavaBean API Then we ask the bean to calculate A + B Then we ask the bean to calculate A + B

16 2001Daniel L. Silver16 Setting Bean Properties Using a JavaBean API convention: HTML code: HTML code: Enter the value of A: Enter the value of A: Enter the value of B: Enter the value of B: </FORM> calcApB.jsp JSP code: calcApB.jsp JSP code: Java Bean (servlet) code: Java Bean (servlet) code: public void setA(int value) { a = value; a = value; } public void setB(int value) { b = value; b = value; }

17 2001Daniel L. Silver17 EnterPrise JavaBean Architecture EJB = Enterprise Java Bean (from SUN) EJB = Enterprise Java Bean (from SUN) EJB is at the heart of the J2EE technologies embedded within most Application Servers EJB is at the heart of the J2EE technologies embedded within most Application Servers Why yet another Java API? Why yet another Java API? –for robust, secure, distributed, persistent, transaction orient applications The goal of EJBs is to manage these issues in a standard way through reusable code The goal of EJBs is to manage these issues in a standard way through reusable code Competes with MS COM and most recently.NET Competes with MS COM and most recently.NET

18 2001Daniel L. Silver18 Reasons for EJB Architecture Object Distribution Object Distribution –Enterprise scale applications are distributed over geography and over different platforms –A common method of object naming (location) and transaction processing are needed –Two distributed object architectures for EJB »OMG’s CORBA – Common Object Request Broker Agent »Sun’s Java RMI – Remote Method Innvocation –EJB combines RMI with IIOP (Internet Inter-ORB Propotocal) and additional APIs to provide distributed object services

19 2001Daniel L. Silver19 Reasons for EJB Architecture Object Persistence Object Persistence –A persistent object is one that preserves it state (the value of its variables) across multiple invocations of the program that references that object (e.g. an order) –Maintaining state accomplished through JAVA serialization mechanism (creates a stream with an identifying serial number) –Hard part of persistence is a standard method of data storage and recall – typically using a relational DBMS –EJB allows the use of : »CMP (Container Managed Persistence) – vendor provided »BMP (Bean Managed Persistence) – user-developed

20 2001Daniel L. Silver20 Reasons for EJB Architecture Transactions Transactions –Transaction integrity must be maintained even when in a distributed environment with several databases –JDBC is commonly used to connect to a DBMS –How can a system be built to fully commit a transaction if and only if each DBMS says OK? –This is not easy – requires another layer of software between application and databases –Transaction monitoring embedded within EJB (JTA – Java Transaction APIs)

21 2001Daniel L. Silver21 Reasons for EJB Architecture Security Security –There was no set of common APIs for handling security –Authentication – Identification of a valid user –Authorization – Access to different parts of system must be restricted by user –Most solutions have been “role-your-own” –EJBs are built on a standard model that can control attribute access by specific user

22 2001Daniel L. Silver22 Hierarchy of EJB Types EJB Entity Bean (persistent object) Session Bean (nonpersistent) Bean Managed Persistence Container Managed Persistence Stateful (client session) Stateless

23 2001Daniel L. Silver23 Todays E-Commerce Apps Need the EJB Architecture Standard, portable component based architecture independent of platform or application server (well at least to some degree) Standard, portable component based architecture independent of platform or application server (well at least to some degree) Access to enterprise data and shared business logic from multuiple client types (HTML browsers, cell phones, PDAs) … XML Access to enterprise data and shared business logic from multuiple client types (HTML browsers, cell phones, PDAs) … XML Concurrent read/update to shared data by many users Concurrent read/update to shared data by many users Access to multiple disparate data sources with transactional integrity Access to multiple disparate data sources with transactional integrity Method-level security can restrict access to data by user Method-level security can restrict access to data by user Scalability to to multiple servers to handle throughput Scalability to to multiple servers to handle throughput

24 2001Daniel L. Silver24 J2EE Java 2 Enterprise Edition Technology is an EJB specification that includes: Java 2 Enterprise Edition Technology is an EJB specification that includes: –Programming models and APIs for developing components and web applications –A set of enterprise APIs that provide services such as transactions, naming, messaging, DBMS access –A runtime environment that can host EJB applications and functions as per the APIs

25 2001Daniel L. Silver25 J2EE Architecture Java 2 Standard Edition Java 2 Enterprise Edition J2EE services Web Container Servlets JSPs J2EE services EJB Container EJBs Servlets JSPs Browser Client Enterprise System

26 2001Daniel L. Silver26 J2EE Conforming vendors: Conforming vendors: –Jakarta - Tomcat –BEA - Weblogic AS –IBM - WebSphere AS –Sun-AOL alliance - iPlanet AS –Borland – Borland AS –Iona – iPortal AS

27 2001Daniel L. Silver27 J2EE API Services JNDI – JavaNaming and Directory Innterface JNDI – JavaNaming and Directory Innterface JTA – Java Transaction API JTA – Java Transaction API JDBC – Java Database Connectivity JDBC – Java Database Connectivity JMS – Java Messaging Service JMS – Java Messaging Service JavaMail – Java Mail API JavaMail – Java Mail API JAXP – Java API for XML Parsing JAXP – Java API for XML Parsing Connector – standard for connecting to enterprise applications such as ERPs Connector – standard for connecting to enterprise applications such as ERPs JAAS – Java Authentication and Authorization Service JAAS – Java Authentication and Authorization Service

28 2001Daniel L. Silver28 Links To the SUN Java Tutorial on Java To the SUN Java Tutorial on Java http://java.sun.com/docs/books/tutorial/index.html To the SUN Java Tutorial on JavaBeans To the SUN Java Tutorial on JavaBeans http://java.sun.com/docs/books/tutorial/javabeans/TOC.html

29 THE END danny.silver@acadiau.ca

30 2001Daniel L. Silver30 Database Connectivity via JSP WebSphere provides a number of extensions to the JSP tags … prefix is <tsx: WebSphere provides a number of extensions to the JSP tags … prefix is <tsx: We will use these JSP tags for connecting to and accessing DB2 data We will use these JSP tags for connecting to and accessing DB2 data <tsx:dbconnect id="conn" … … … …

31 2001Daniel L. Silver31 Database Connectivity via JSP Lets have a look at these tags in action via a JSP example that returns a request store’s name http://eagle.acadiau.ca/store35/sampleJSP.jsp?cgmenbr=3035 Lets have a look at these tags in action via a JSP example that returns a request store’s name http://eagle.acadiau.ca/store35/sampleJSP.jsp?cgmenbr=3035 http://eagle.acadiau.ca/store35/sampleJSP.jsp?cgmenbr=3035 Here is a servlet that does the same thing Here is a servlet that does the same thing http://eagle.acadiau.ca/store35/servlet/sampleServlet?cgmenbr=3035

32 2001Daniel L. Silver32 Links to JSP software getProperty and method call to a bean that returns the current date and time http://eagle.acadiau.ca/store35/HelloJSP1.jsp getProperty and method call to a bean that returns the current date and time http://eagle.acadiau.ca/store35/HelloJSP1.jsp http://eagle.acadiau.ca/store35/HelloJSP1.jsp JSP form and bean combo that inputs to values A and B and calculates C = A + B http://eagle.acadiau.ca/store35/calcApB.jsp JSP form and bean combo that inputs to values A and B and calculates C = A + B http://eagle.acadiau.ca/store35/calcApB.jsp http://eagle.acadiau.ca/store35/calcApB.jsp JSP that returns the requested stores name JSP that returns the requested stores name http://eagle.acadiau.ca/store35/sampleJSP.jsp?cgmenbr=3035 Servlet that does the same thing Servlet that does the same thing http://eagle.acadiau.ca/store35/servlet/sampleServlet?cgmenbr=3035


Download ppt "Comp2513 JavaBeans, EJB and J2EE Daniel L. Silver, Ph.D."

Similar presentations


Ads by Google