+ All Categories
Home > Documents > Comp2513 JavaBeans, EJB and J2EE

Comp2513 JavaBeans, EJB and J2EE

Date post: 10-Feb-2016
Category:
Upload: riona
View: 47 times
Download: 0 times
Share this document with a friend
Description:
Comp2513 JavaBeans, EJB and J2EE. Daniel L. Silver, Ph.D. Objectives. To understand how JSPs work with JavaBeans To review examples of JSPs and JavaBeans To introduce Enterprise JavaBeans, EJBs To describe J2EE technologies Paper Reference: EJP - Ch. 13, PJEC - Ch.5 - PowerPoint PPT Presentation
32
Comp2513 Comp2513 JavaBeans, EJB and JavaBeans, EJB and J2EE J2EE Daniel L. Silver, Ph.D. Daniel L. Silver, Ph.D.
Transcript
Page 1: Comp2513 JavaBeans, EJB and J2EE

Comp2513Comp2513

JavaBeans, EJB and J2EEJavaBeans, EJB and J2EE

Daniel L. Silver, Ph.D.Daniel L. Silver, Ph.D.

Page 2: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 2

ObjectivesObjectives

To understand how JSPs work with JavaBeansTo understand how JSPs work with JavaBeans To review examples of JSPs and JavaBeansTo review examples of JSPs and JavaBeans To introduce Enterprise JavaBeans, EJBsTo introduce Enterprise JavaBeans, EJBs To describe J2EE technologiesTo describe J2EE technologies

Paper Reference: EJP - Ch. 13, PJEC - Ch.5Paper Reference: EJP - Ch. 13, PJEC - Ch.5 Web Reference: Web Reference: ServletServlet

and JSP Programming with IBM and JSP Programming with IBM WebsphereWebsphere - - Chapter 5 Chapter 5

Page 3: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 3

The JSP Operational ModelThe JSP Operational Model1.1. HTTP server receives request for .jsp fileHTTP server receives request for .jsp file2.2. The .jsp file is located (can be in any directory)The .jsp file is located (can be in any directory)3.3. Tomcat is called and passed the .jsp fileTomcat is called and passed the .jsp file4.4. Tomcat invokes a special servlet called Tomcat invokes a special servlet called pageCompile pageCompile (the (the

JSP engine, comes as part of JSP distribution)JSP engine, comes as part of JSP distribution)5.5. pageCompile builds a servlet based on JSP code (that may pageCompile builds a servlet based on JSP code (that may

include a reference to a JavaBean)include a reference to a JavaBean)6.6. The servlet .java and .class files are written to disk (internal The servlet .java and .class files are written to disk (internal

to Tomcat)to Tomcat)7.7. The .class file is processed by the JVM within Tomcat like The .class file is processed by the JVM within Tomcat like

any other servletany other servlet8.8. HTML code is generated by the servlet and returnedHTML code is generated by the servlet and returned

Page 4: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 4

Tomcat Java ServletTomcat Java ServletRequest ProcessingRequest Processing

Internet

Browser

Client

HTTP Server

HelloWorld.jsp

mod_jserv

http://eagle.acadiau.ca/store05/HelloWorld.jsp

TomcatApp.

Server

../Store05/HelloWorld.jsp

HTML

HelloJSP1.class

../Store05/WEB-INF/classes/exampleBean/HelloJSP1

HelloWorld.javaHelloWorld.class

1

8

2

43

6

5

7

pageCompile

Page 5: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 5

Putting it all TogetherPutting it all Together

DateDisplayDateDisplay is is a basic JSP that you can a basic JSP that you can begin to learn frombegin to learn from

Here is a link to the Here is a link to the JSP sourceJSP source Here is a link to the Here is a link to the Java sourceJava source resulting resulting

from the JSP compilationfrom the JSP compilation

Page 6: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 6

JavaBeans APIJavaBeans APIThe SUN JavaBean API provides a powerful mechanism for The SUN JavaBean API provides a powerful mechanism for

software reuse and automated support (e.g. introspection = software reuse and automated support (e.g. introspection = beans can tell IDEs about themselves)beans can tell IDEs about themselves)

Defines how to write components in Java that are self-Defines how to write components in Java that are self-contained, reusable software units that can be visually contained, reusable software units that can be visually composed into composite components, applets, composed into composite components, applets, applications, and servlets applications, and servlets

A JavaBean is Java .class file that contains data A JavaBean is Java .class file that contains data (properties) and methods that follow certain coding (properties) and methods that follow certain coding conventionsconventions

Under JSP – the use of Beans is the preferred method of Under JSP – the use of Beans is the preferred method of separating static HTML from dynamic Java code and separating static HTML from dynamic Java code and business logicbusiness logic

Page 7: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 7

JSP useBean TagJSP useBean Tag

There are standard action JSP tags that allow you There are standard action JSP tags that allow you to work with pre-defined JavaBeansto work with pre-defined JavaBeans

The syntax for referencing a JavaBean within a The syntax for referencing a JavaBean within a JSP can be of two forms:JSP can be of two forms:

<jsp:useBean <jsp:useBean use_bean_attributesuse_bean_attributes /> />oror

<jsp:useBean <jsp:useBean use_bean_attributeuse_bean_attribute > >[option scriptlets and tags] [option scriptlets and tags]

</jsp:useBean></jsp:useBean>

Page 8: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 8

JSP useBean TagJSP useBean Tag

The basic syntax for referencing a JavaBean The basic syntax for referencing a JavaBean within a JSP is as follows:within a JSP is as follows:

<jsp:useBean<jsp:useBean id = "textProvider"id = "textProvider" class = "exampleBean.HelloJSP1"class = "exampleBean.HelloJSP1" scope = “request"scope = “request"/>/>

If an instance of HellpJSP1 does not already If an instance of HellpJSP1 does not already exist then one is createdexist then one is created

Object name used within JSP (case sensitive)

Name of the object’s implementation class

See next slide

Page 9: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 9

JSP useBean TagJSP useBean TagScopeScope DescriptionDescription

PagePage Object is accessible only Object is accessible only by single clientby single client from from this this page page until a response is returneduntil a response is returned

requestrequest Object is accessible by single client Object is accessible by single client for lifetime of for lifetime of client requestclient request (possibly several pages) until a (possibly several pages) until a response is returnedresponse is returned

sessionsession Object is accessible by single client from Object is accessible by single client from anywhere in the application anywhere in the application for lifetime of client for lifetime of client sessionsession (possibly several requests). (possibly several requests). The page in which The page in which you create the Bean must have a <%@ page %> directive with you create the Bean must have a <%@ page %> directive with session=truesession=true..

applicationapplication Object is accessible by Object is accessible by any clientany client from anywhere from anywhere in the application in the application for lifetime of the applicationfor lifetime of the application

Page 10: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 10

Other Useful JSP TagsOther Useful JSP Tags

Include Tag - includes the Include Tag - includes the outputoutput of a of a servlet in a JSPservlet in a JSP

<jsp:include page=“/servlet/ShowAcctBalance” /><jsp:include page=“/servlet/ShowAcctBalance” />

Forward Tag – forwards processing from a Forward Tag – forwards processing from a JSP to a servletJSP to a servlet

<jsp:forward page=“/servlet/CheckCreditLimit” /><jsp:forward page=“/servlet/CheckCreditLimit” />

Page 11: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 11

Getting Bean PropertiesGetting Bean Properties

Other tags allow you to get or set Bean properties (data Other tags allow you to get or set Bean properties (data attributes) … getProperty tag:attributes) … getProperty tag:

JSP code:JSP code:<jsp:getProperty name="textProvider" <jsp:getProperty name="textProvider" property="textMessage"/>property="textMessage"/>

Java Bean (servlet) code:Java Bean (servlet) code:public String getTextMessage() public String getTextMessage() {{ Date now = new Date();Date now = new Date(); return "Hello World! ... It is now " + return "Hello World! ... It is now " + now.toString() + ".";now.toString() + ".";

}}

Page 12: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 12

Setting Bean PropertiesSetting Bean Properties

Using the setProperty Tag:Using the setProperty Tag: JSP code:JSP code:

<jsp:setProperty name=“calcProvider" property=“a” <jsp:setProperty name=“calcProvider" property=“a” value=“3" />value=“3" />

Java Bean (servlet) code:Java Bean (servlet) code:public void setA(int value)public void setA(int value) {{ a = value;a = value; }}

Page 13: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 13

JSP Bean Tags in ActionJSP Bean Tags in Action

Lets have a look at the useBean and Lets have a look at the useBean and getProperty tags in action within getProperty tags in action within http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsphttp://eagle.acadiau.ca/demo/jsp/HelloJSP1.jspFor JSP source see For JSP source see (use browser’s source view)(use browser’s source view) http://eagle.acadiau.ca/demo/jsp/HelloJSP1_jsp.txthttp://eagle.acadiau.ca/demo/jsp/HelloJSP1_jsp.txt

Note that the example demonstrates how a bean Note that the example demonstrates how a bean property value can be obtained using the property value can be obtained using the getProperty tag or by an expression:getProperty tag or by an expression:<%= textProvider.getTextMessage() %><%= textProvider.getTextMessage() %>

Page 14: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 14

JSP Bean Tags in ActionJSP Bean Tags in Action

Lets have a look at the setProperty tag in action Lets have a look at the setProperty tag in action within within http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsphttp://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp

First we set the property values of a bean First we set the property values of a bean “calcApB.class” using “calcApB.class” using <jsp:setProperty name="calcProvider" property="a" <jsp:setProperty name="calcProvider" property="a"

value="5" />value="5" /> Java Bean (servlet) code:Java Bean (servlet) code:

public void setA(int value)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 <%= calcProvider.calcResult() %><%= calcProvider.calcResult() %>

Page 15: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 15

JSP Bean Tags in ActionJSP Bean Tags in Action

Lets have a look at passing parameters from forms Lets have a look at passing parameters from forms within within http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsphttp://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp

We call another JSP “calcApB.jsp” from an We call another JSP “calcApB.jsp” from an HTML form passing the values of a and bHTML form passing the values of a and b

Within calcApB.jsp we call the bean “calcApB” Within calcApB.jsp we call the bean “calcApB” and pass the values as per the JavaBean APIand pass the values as per the JavaBean API<jsp:setProperty name="calcProvider" property=“*" /><jsp:setProperty name="calcProvider" property=“*" />

Then we ask the bean to calculate A + B Then we ask the bean to calculate A + B <%= calcProvider.calcResult() %><%= calcProvider.calcResult() %>

Page 16: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 16

Setting Bean PropertiesSetting Bean PropertiesUsing a JavaBean API convention:Using a JavaBean API convention: HTML code:HTML code:

<FORM METHOD=POST ACTION="calcApB.jsp"><FORM METHOD=POST ACTION="calcApB.jsp"> Enter the value of A: <INPUT TYPE=TEXT NAME=a SIZE=4><BR>Enter the value of A: <INPUT TYPE=TEXT NAME=a SIZE=4><BR> Enter the value of B: <INPUT TYPE=TEXT NAME=b SIZE=4><BR>Enter the value of B: <INPUT TYPE=TEXT NAME=b SIZE=4><BR> <P><INPUT TYPE=SUBMIT><P><INPUT TYPE=SUBMIT></FORM></FORM>

calcApB.jsp JSP code:calcApB.jsp JSP code:<jsp:useBean id="calcProvider" scope="request" class="exampleBean.calcApB"><jsp:useBean id="calcProvider" scope="request" class="exampleBean.calcApB"> <jsp:setProperty name="calcProvider" property="*" /> </jsp:useBean><jsp:setProperty name="calcProvider" property="*" /> </jsp:useBean>

Java Bean (servlet) code:Java Bean (servlet) code:public void setA(int value)public void setA(int value) {{ a = value;a = value; }}

public void setB(int value)public void setB(int value) {{ b = value;b = value; }}

Page 17: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 17

EnterPrise JavaBean ArchitectureEnterPrise JavaBean Architecture

EJB = Enterprise Java Bean (from SUN)EJB = Enterprise Java Bean (from SUN) EJB is at the heart of the J2EE technologies EJB is at the heart of the J2EE technologies

embedded within most Application Servers embedded within most Application Servers Why yet another Java API?Why yet another Java API?

– for robust, secure, distributed, persistent, transaction for robust, secure, distributed, persistent, transaction orient applications orient applications

The goal of EJBs is to manage these issues in a The goal of EJBs is to manage these issues in a standard way through reusable codestandard way through reusable code

Competes with MS COM and most recently .NETCompetes with MS COM and most recently .NET

Page 18: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 18

Reasons for EJB ArchitectureReasons for EJB Architecture Object DistributionObject Distribution

– Enterprise scale applications are distributed over Enterprise scale applications are distributed over geography and over different platformsgeography and over different platforms

– A common method of object naming (location) and A common method of object naming (location) and transaction processing are neededtransaction processing are needed

– Two distributed object architectures for EJBTwo distributed object architectures for EJB» OMG’s CORBA – Common Object Request Broker AgentOMG’s CORBA – Common Object Request Broker Agent» Sun’s Java RMI – Remote Method InnvocationSun’s Java RMI – Remote Method Innvocation

– EJB combines RMI with IIOP (Internet Inter-ORB EJB combines RMI with IIOP (Internet Inter-ORB Propotocal) and additional APIs to provide distributed Propotocal) and additional APIs to provide distributed object servicesobject services

Page 19: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 19

Reasons for EJB ArchitectureReasons for EJB Architecture Object PersistenceObject Persistence

– A persistent object is one that preserves it state (the A persistent object is one that preserves it state (the value of its variables) across multiple invocations of the value of its variables) across multiple invocations of the program that references that object (program that references that object (e.g.e.g. an order) an order)

– Maintaining state accomplished through JAVA Maintaining state accomplished through JAVA serialization mechanism (creates a stream with an serialization mechanism (creates a stream with an identifying serial number)identifying serial number)

– Hard part of persistence is a standard method of data Hard part of persistence is a standard method of data storage and recall – typically using a relational DBMSstorage and recall – typically using a relational DBMS

– EJB allows the use of : EJB allows the use of : » CMP (Container Managed Persistence) – vendor providedCMP (Container Managed Persistence) – vendor provided» BMP (Bean Managed Persistence) – user-developedBMP (Bean Managed Persistence) – user-developed

Page 20: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 20

Reasons for EJB ArchitectureReasons for EJB Architecture

TransactionsTransactions– Transaction integrity must be maintained even when in Transaction integrity must be maintained even when in

a distributed environment with several databasesa distributed environment with several databases– JDBC is commonly used to connect to a DBMSJDBC is commonly used to connect to a DBMS– How can a system be built to fully commit a transaction How can a system be built to fully commit a transaction

if and only if each DBMS says OK?if and only if each DBMS says OK?– This is not easy – requires another layer of software This is not easy – requires another layer of software

between application and databasesbetween application and databases– Transaction monitoring embedded within EJB (JTA –Transaction monitoring embedded within EJB (JTA –

Java Transaction APIs) Java Transaction APIs)

Page 21: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 21

Reasons for EJB ArchitectureReasons for EJB Architecture

SecuritySecurity– There was no set of common APIs for handling There was no set of common APIs for handling

securitysecurity– Authentication – Authentication – Identification of a valid userIdentification of a valid user– Authorization – Authorization – Access to different parts of Access to different parts of

system must be restricted by usersystem must be restricted by user– Most solutions have been “role-your-own”Most solutions have been “role-your-own”– EJBs are built on a standard model that can EJBs are built on a standard model that can

control attribute access by specific user control attribute access by specific user

Page 22: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 22

Hierarchy of EJB TypesHierarchy of EJB Types

EJB

Entity Bean(persistent object)

Session Bean(nonpersistent)

Bean Managed

Persistence

Container Managed

Persistence

Stateful(client session)

Stateless

Page 23: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 23

Todays E-Commerce Apps Need Todays E-Commerce Apps Need the EJB Architecturethe EJB Architecture

Standard, portable component based architecture Standard, portable component based architecture independent of platform or application server (well at least independent of platform or application server (well at least to some degree) to some degree)

Access to enterprise data and shared business logic from Access to enterprise data and shared business logic from multuiple client types (HTML browsers, cell phones, multuiple client types (HTML browsers, cell phones, PDAs) … XMLPDAs) … XML

Concurrent read/update to shared data by many usersConcurrent read/update to shared data by many users Access to multiple disparate data sources with Access to multiple disparate data sources with

transactional integritytransactional integrity Method-level security can restrict access to data by userMethod-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

Page 24: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 24

J2EEJ2EE Java 2 Enterprise Edition Technology is an Java 2 Enterprise Edition Technology is an

EJB specification that includes:EJB specification that includes:– Programming models and APIs for developing Programming models and APIs for developing

components and web applicationscomponents and web applications– A set of enterprise APIs that provide services A set of enterprise APIs that provide services

such as transactions, naming, messaging, such as transactions, naming, messaging, DBMS accessDBMS access

– A runtime environment that can host EJB A runtime environment that can host EJB applications and functions as per the APIs applications and functions as per the APIs

Page 25: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 25

J2EE ArchitectureJ2EE Architecture

Java 2 Standard Edition

Java 2 Enterprise Edition

J2EE services

Web ContainerServlets

JSPs

J2EE services

EJB ContainerEJBsEJBsEJBsServlets JSPs

BrowserClient

EnterpriseSystem

Page 26: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 26

J2EEJ2EE

Conforming vendors: Conforming vendors: – Jakarta - TomcatJakarta - Tomcat– BEA - Weblogic ASBEA - Weblogic AS– IBM - WebSphere ASIBM - WebSphere AS– Sun-AOL alliance - iPlanet ASSun-AOL alliance - iPlanet AS– Borland – Borland ASBorland – Borland AS– Iona – iPortal ASIona – iPortal AS

Page 27: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 27

J2EE API ServicesJ2EE API Services JNDI – JavaNaming and Directory InnterfaceJNDI – JavaNaming and Directory Innterface JTA – Java Transaction APIJTA – Java Transaction API JDBC – Java Database ConnectivityJDBC – Java Database Connectivity JMS – Java Messaging ServiceJMS – Java Messaging Service JavaMail – Java Mail APIJavaMail – Java Mail API JAXP – Java API for XML ParsingJAXP – Java API for XML Parsing Connector – standard for connecting to enterprise Connector – standard for connecting to enterprise

applications such as ERPsapplications such as ERPs JAAS – Java Authentication and Authorization JAAS – Java Authentication and Authorization

ServiceService

Page 28: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 28

LinksLinks

To the SUN Java Tutorial on JavaTo the SUN Java Tutorial on Javahttp://java.sun.com/docs/books/tutorial/index.htmlhttp://java.sun.com/docs/books/tutorial/index.html

To the SUN Java Tutorial on JavaBeansTo the SUN Java Tutorial on JavaBeanshttp://http://java.sun.com/docs/books/tutorial/javabeans/TOC.htmljava.sun.com/docs/books/tutorial/javabeans/TOC.html

Page 29: Comp2513 JavaBeans, EJB and J2EE

THE ENDTHE END

[email protected]@acadiau.ca

Page 30: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 30

Database Connectivity via JSPDatabase Connectivity via JSP WebSphere provides a number of extensions to the JSP WebSphere provides a number of extensions to the JSP

tags … prefix is <tsx:tags … prefix is <tsx: We will use these JSP tags for connecting to and We will use these JSP tags for connecting to and

accessing DB2 dataaccessing DB2 data<tsx:dbconnect id="conn" …<tsx:dbconnect id="conn" ………<tsx:dbquery connection="conn“ id="stname"> <tsx:dbquery connection="conn“ id="stname"> ……

<tsx:repeat index="newidx"><tsx:repeat index="newidx"><tsx:getProperty name="stname" <tsx:getProperty name="stname"

property="MESTNAME" />property="MESTNAME" />

Page 31: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 31

Database Connectivity via JSPDatabase Connectivity via JSP

Lets have a look at these tags in action via a JSP Lets have a look at these tags in action via a JSP example that returns a request store’s name example that returns a request store’s name http://eagle.acadiau.ca/store35/sampleJSP.jsp?cgmenbr=3035http://eagle.acadiau.ca/store35/sampleJSP.jsp?cgmenbr=3035

Here is a servlet that does the same thingHere is a servlet that does the same thinghttp://eagle.acadiau.ca/store35/servlet/sampleServlet?cgmenbr=3035http://eagle.acadiau.ca/store35/servlet/sampleServlet?cgmenbr=3035

Page 32: Comp2513 JavaBeans, EJB and J2EE

2001 Daniel L. Silver 32

Links to JSP softwareLinks to JSP software getProperty and method call to a bean that returns the getProperty and method call to a bean that returns the

current date and time current date and time http://eagle.acadiau.ca/store35/HelloJSP1.jsphttp://eagle.acadiau.ca/store35/HelloJSP1.jsp

JSP form and bean combo that inputs to values A and B JSP form and bean combo that inputs to values A and B and calculates C = A + B and calculates C = A + B http://eagle.acadiau.ca/store35/calcApB.jsphttp://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=3035http://eagle.acadiau.ca/store35/sampleJSP.jsp?cgmenbr=3035 Servlet that does the same thingServlet that does the same thinghttp://eagle.acadiau.ca/store35/servlet/sampleServlet?cgmenbr=3035http://eagle.acadiau.ca/store35/servlet/sampleServlet?cgmenbr=3035


Recommended