Download Comp1503 Introduction to E

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
Web Reference: Servlet and JSP Programming
with IBM Websphere - Chapter 5
2001
Daniel L. Silver
2
The JSP Operational Model
1.
2.
3.
4.
5.
6.
7.
8.
2001
HTTP server receives request for .jsp file
The .jsp file is located (can be in any directory)
Tomcat is called and passed the .jsp file
Tomcat invokes a special servlet called pageCompile
(the JSP engine, comes as part of JSP distribution)
pageCompile builds a servlet based on JSP code (that
may include a reference to a JavaBean)
The servlet .java and .class files are written to disk
(internal to Tomcat)
The .class file is processed by the JVM within Tomcat
like any other servlet
HTML code is generated by the servlet and returned
Daniel L. Silver
3
Tomcat Java Servlet
Request Processing
Client
http://eagle.acadiau.ca/store05/HelloWorld.jsp
Browser
HelloWorld.java
6 HelloWorld.class
8
1
HTTP
Server
HTML
mod_jserv
3
Internet
Tomcat
App.
Server
7
pageCompile 4
5
2
../Store05/WEB-INF/classes/exampleBean/HelloJSP1
../Store05/HelloWorld.jsp
HelloJSP1.class
HelloWorld.jsp
2001
Daniel L. Silver
4
Putting it all Together
DateDisplay is a basic JSP that you can
begin to learn from
 Here is a link to the JSP source
 Here is a link to the Java source resulting
from the JSP compilation

2001
Daniel L. Silver
5
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)
 Defines how to write components in Java that are selfcontained, 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
 Under JSP – the use of Beans is the preferred method of
separating static HTML from dynamic Java code and
business logic
2001
Daniel L. Silver
6
JSP useBean Tag


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:
<jsp:useBean use_bean_attributes />
or
<jsp:useBean use_bean_attribute >
[option scriptlets and tags]
</jsp:useBean>
2001
Daniel L. Silver
7
JSP useBean Tag

The basic syntax for referencing a JavaBean
within a JSP is as follows:
Object name used within
<jsp:useBean
JSP (case sensitive)
id = "textProvider"
class = "exampleBean.HelloJSP1"
Name of the object’s
scope = “request"
implementation class
/>
See next slide

If an instance of HellpJSP1 does not already
exist then one is created
2001
Daniel L. Silver
8
JSP useBean Tag
Scope
Description
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 <%@ page %> directive with session=true.
application
2001
Object is accessible by any client from anywhere
in the application for lifetime of the application
Daniel L. Silver
9
Other Useful JSP Tags

Include Tag - includes the output of a
servlet in a JSP
<jsp:include page=“/servlet/ShowAcctBalance” />

Forward Tag – forwards processing from a
JSP to a servlet
<jsp:forward page=“/servlet/CheckCreditLimit” />
2001
Daniel L. Silver
10
Getting Bean Properties
Other tags allow you to get or set Bean properties (data
attributes) … getProperty tag:

JSP code:
<jsp:getProperty name="textProvider"
property="textMessage"/>

Java Bean (servlet) code:
public String getTextMessage()
{
Date now = new Date();
return "Hello World! ... It is now " +
now.toString() + ".";
}
2001
Daniel L. Silver
11
Setting Bean Properties
Using the setProperty Tag:
 JSP code:
<jsp:setProperty name=“calcProvider" property=“a”
value=“3" />
 Java
Bean (servlet) code:
public void setA(int value)
{
a = value;
}
2001
Daniel L. Silver
12
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
For JSP source see (use browser’s source view)
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:
<%= textProvider.getTextMessage() %>
2001
Daniel L. Silver
13
JSP Bean Tags in Action
Lets have a look at the setProperty tag in action
within http://eagle.acadiau.ca/demo/jsp/HelloJSP1.jsp
 First we set the property values of a bean
“calcApB.class” using
<jsp:setProperty name="calcProvider" property="a"
value="5" />

Java Bean (servlet) code:
public void setA(int value)
{
a = value;
}

Then we ask the bean to calculate A + B
<%= calcProvider.calcResult() %>
2001
Daniel L. Silver
14
JSP Bean Tags in Action



Lets have a look at passing parameters from forms
within 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
Within calcApB.jsp we call the bean “calcApB”
and pass the values as per the JavaBean API
<jsp:setProperty name="calcProvider" property=“*" />

Then we ask the bean to calculate A + B
<%= calcProvider.calcResult() %>
2001
Daniel L. Silver
15
Setting Bean Properties
Using a JavaBean API convention:
 HTML code:
<FORM METHOD=POST ACTION="calcApB.jsp">
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>
<P><INPUT TYPE=SUBMIT>
</FORM>

calcApB.jsp JSP code:
<jsp:useBean id="calcProvider" scope="request" class="exampleBean.calcApB">
<jsp:setProperty name="calcProvider" property="*" /> </jsp:useBean>

Java Bean (servlet) code:
public void setB(int value)
{
b = value;
}
public void setA(int value)
{
a = value;
}
2001
Daniel L. Silver
16
EnterPrise JavaBean Architecture



EJB = Enterprise Java Bean (from SUN)
EJB is at the heart of the J2EE technologies
embedded within most Application Servers
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
Competes with MS COM and most recently .NET
2001
Daniel L. Silver
17
Reasons for EJB Architecture

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
2001
Daniel L. Silver
18
Reasons for EJB Architecture

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
2001
Daniel L. Silver
19
Reasons for EJB Architecture

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)
2001
Daniel L. Silver
20
Reasons for EJB Architecture

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
2001
Daniel L. Silver
21
Hierarchy of EJB Types
EJB
Entity Bean
(persistent object)
Container
Managed
Persistence
2001
Session Bean
(nonpersistent)
Bean
Managed
Persistence
Daniel L. Silver
Stateful
(client session)
Stateless
22
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)
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
Access to multiple disparate data sources with
transactional integrity
Method-level security can restrict access to data by user
Scalability to to multiple servers to handle throughput
2001
Daniel L. Silver
23
J2EE

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
2001
Daniel L. Silver
24
J2EE Architecture
Browser
Client
Enterprise
System
Web Container
Servlets
Servlets
EJB Container
JSPs
JSPs
J2EE services
EJBs
EJBs
EJBs
J2EE services
Java 2 Enterprise Edition
Java 2 Standard Edition
2001
Daniel L. Silver
25
J2EE

Conforming vendors:
–
–
–
–
–
–
2001
Jakarta - Tomcat
BEA - Weblogic AS
IBM - WebSphere AS
Sun-AOL alliance - iPlanet AS
Borland – Borland AS
Iona – iPortal AS
Daniel L. Silver
26
J2EE API Services








JNDI – JavaNaming and Directory Innterface
JTA – Java Transaction API
JDBC – Java Database Connectivity
JMS – Java Messaging Service
JavaMail – Java Mail API
JAXP – Java API for XML Parsing
Connector – standard for connecting to enterprise
applications such as ERPs
JAAS – Java Authentication and Authorization
Service
2001
Daniel L. Silver
27
Links

To the SUN Java Tutorial on Java
http://java.sun.com/docs/books/tutorial/index.html

To the SUN Java Tutorial on JavaBeans
http://java.sun.com/docs/books/tutorial/javabeans/TOC.html
2001
Daniel L. Silver
28
THE END
[email protected]
Database Connectivity via JSP


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
<tsx:dbconnect id="conn" …
…
<tsx:dbquery connection="conn“ id="stname">
…
<tsx:repeat index="newidx">
<tsx:getProperty name="stname"
property="MESTNAME" />
2001
Daniel L. Silver
30
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

Here is a servlet that does the same thing
http://eagle.acadiau.ca/store35/servlet/sampleServlet?cgmenbr=3035
2001
Daniel L. Silver
31
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

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 that returns the requested stores name
http://eagle.acadiau.ca/store35/sampleJSP.jsp?cgmenbr=3035
 Servlet that does the same thing
http://eagle.acadiau.ca/store35/servlet/sampleServlet?cgmenbr=3035
2001
Daniel L. Silver
32