Download DOC - University of Connecticut

Document related concepts
no text concepts found
Transcript
CSE298
CSE300
Distributed Object Computing and Java
-Web Application-
Pinglei Zhou
Junming Wang
Chaopeng Zhou
Computer Science & Engineering Department
The University of Connecticut
191 Auditorium Road, Box U-155
Storrs, CT 06269-3155
CSE.RU-1.1
CSE298
CSE300
Motivation

Web Application Development



Development of Internet and the WWW
Traditional way for for dynamic Web
Application
Java Features Support Web Application






Servlet
Enterprise JavaBeans
JDBC
Java and XML
RMI
…..
CSE.RU-1.2
CSE298
CSE300
Overview of the Presentation

Overview Traditional Web Programming

Brief Introduction of Enterprise System
With J2EE

Description and Evaluation Java-Support
Web Programming

Conclusion and Future Work
CSE.RU-1.3
CSE298
CSE300
Traditional Web Programming & Java

HTML Programming

CGI Programming

Scripting Language

Active Server Pages

Java-Based Web Programming
CSE.RU-1.4
CSE298
CSE300
Generic Enterprise System & Java
Enterprise user Interface
•
AWT & Swing
Enterprise Data Enabling
•
JDBC
Distributing Enterprise Communications Enabling
•
RMI
Common Services for Distributed Communications
•
JINI
Enterprise Systems Assurance
•
Java Security Model
Enterprise Web Application
•
Servlet
Enterprise Model Components
•
EJB
CSE.RU-1.5
CSE298
CSE300
Why Dynamic Web Page ?

The Web Page Is Based On Data
Submitted by the User

The Web Page Is Derived From Data
That Changes Frequently

The Web Page Uses Information From
Corporate Database or Other Server-Side
Sources
CSE.RU-1.6
CSE298
CSE300
System Architecture
Client Program
Presentation Tier
Servlet
EJBs
Servlet
JDBC
JDBC
Middle Tier
JDBC
Oracle Database
Data Tier
CSE.RU-1.7
CSE298
CSE300
Servlet Examples User

Processing and/or store data
submitted by over HTTPS using an
HTML form

Allowing collaboration between
people

Being a community of active agents
CSE.RU-1.8
CSE298
CSE300
Advantages Over Traditional CGI

Efficient
Each resquest each thread
Store the program in momery after terminated

Powerful
Talk Directly to the Web Server
Multiple servlets can share data

Portable
Standard Java API

Secure
A Servlet can be run by a Servlet Engine in
a restrictive Sandbox
CSE.RU-1.9
Servlets vs. Applets
CSE298
CSE300
Servlets
•
Server-side Java programs
•
Implements the javax.servlet.Servlet interface
Faceless
Applets
•
Client-side Java programs
•
Subclassing the java.applet.Applet class
CSE.RU-1.10
CSE298
CSE300
Java Servlet Architecture
Host Machine B
Java Servlet
Component
HttpServlet
Request
Http
Servlet
Servlet
Request
Generic
Servlet
Request
Handling
Http
Servlet
Response
Servlet
Response
Response
Handling
Http
Session
Cooki
e
Session
Management
Net Work
Configureation Service
Management Services
Servlet Container
Web Server Platform
Host Machine A
Net Work
Web Client
Web Client
CSE.RU-1.11
CSE298
CSE300
Typical Servlet Life Cycle
Init
Service
Service
Service
Service
Service
Service
Service
Service
Thread 1
Destroy
Thread 2
Thread 3
CSE.RU-1.12
CSE298
CSE300
Servlet’s Job

Read any data send by the user

Look up other information about the
request that is embedded in the HTTP

Process the request and generate the
results

Format the result inside a document

Send the document back to the client
CSE.RU-1.13
CSE298
CSE300
Three Tiers Architecture of Servlet Web
Application
Server:
JWSDK
Servlet Engineer:
Servlet class
JDK1.3
JDBC
Oracle
Relational
Database
CSE.RU-1.14
CSE298
CSE300
Installation and Setup

Download and install JDK1.3
http://java.sun.com/j2se/1.3/

Download Java Servlet 2.2
http://java.sun.com/products/servlet/index.html

Any Web Server That Support Servlet
CSE.RU-1.15
CSE298
CSE300
Options for Web Server

Apache Tomcat
Http://jakarta.apache.org

Java Server Web Development Kit
(JSWDK)
Http://java.sun.com/products/servlet/download.html

Sun’s Java Web Server
Http://freeware.thespere.com
CSE.RU-1.16
CSE298
CSE300
Basic Servlet Structure
Import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpelServlet extends HttpServlet {
public void init(ServletConfig config){
…..}
public void doGet(HttpServletRequest req, HttpServletResponse res )
throws ServletException , IOException {
…….}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException , IOException {
…….}
CSE.RU-1.17
CSE298
CSE300
Project Implementation-Three Tiers
Choose product
(ProjectFrame.htm)
Re-select
Create Query
(CSE300DOC3.java)
Get Price Data from DB
(DatabaseUtilities.java
DriverUtilities.java
DBResult.java
DataUtilities.java)
Present the price
information for chosen
information
(CSE300DOC3.java)
Provide Credit Information
(CreditCard.htm)
N
Information
confirmation
Y
Create Query
(CSE300DOC
04.java)
Present the Data to
User
CSE.RU-1.18
CSE298
CSE300
Project UML documentation
CSE.RU-1.19
CSE298
CSE300
JDBC and Java


What is JDBC
Java Database Connectivity(JDBC) is implemented
in the java.sql package. JDBC is the mechanism
Java application and applets use to perform
database access.
Prior JDBC, the only way to access a database
from a Java program was to do the database access
in C, and use native methods to interface the C
code with Java. JDBC is intended to be a low-level
API that is powerful enough to exploit most
features available on commercial relational
database, but is simple enough to be used by all
levels of programmer.
CSE.RU-1.20
CSE298
CSE300
JDBC and Java


JDBC technology is an API that lets you access
virtually any tabular data source from the Java
programming language. It provides cross-DBMS
connectivity to a wide
range of SQL databases, and now, with the new
JDBC API, it also provides access to other tabular
data sources, such as spreadsheets or flat files.
Benefit
a developer can easily connect all corporate data
even in a heterogeneous environment
CSE.RU-1.21
CSE298
JDBC Technology Core features (part of the JavaTM 2
CSE300
SDK, Standard Edition )
Feature
Result set enhancements
Benefit
Ease of programming
- Scrollable result set
Ability to move a result set's cursor to a
specific row. This feature is used by GUI
tools and for programmatic updating
Updatable result set
Ability to use Java programming language
commands rather than SQL
New data types support
Performance improvement (ability to
manipulate large objects such as BLOB and
CLOB without bringing them to the client
from the DB server)
Batch updates
Performance improvement (sending
multiple updates to the DB for processing
as a batch can be much more efficient than
sending update statements separately
(http://www.javasoft.com/products/jdbc/features.html)
CSE.RU-1.22
CSE298
CSE300
JDBC Optional Package features (the javax.sql package)
JDBC Optional Package features (the javax.sql package)
Feature
Benefit
JNDI support
Ease of deployment (gives JDBC driver independence, makes
JDBC applications easier to manage)
Connection pooling
Performance improvement (a connection pool is a cache of
database connections that is maintained in memory, so that the
connections may be reused)
Distributed
transactions
JavaBeansTM
(RowSet objects)
Important for implementing a distributed transaction
processing system
Send data across a network to thin clients, such as web
browsers, laptops, PDAs, and so on
Access any tabular data source, even spreadsheets or flat files
Make results sets scrollable or updatable when the JDBC driver
does not support scrollability and updatability Encapsulate a
driver as a JavaBeans component for use in a GUI
(http://www.javasoft.com/products/jdbc/features.html)
CSE.RU-1.23
CSE298
CSE300
JDBC Security
Most Java security is centered on the applet and its capacities as code
downloaded via the Internet or Intranet.
restricted in their access to the client machine and cannot read or write
to files on that machine.
the only machine to which they are allowed to make a database
connection is the machine from which they were downloaded.
These restrictions are relaxed when applets are run using applet runners
such as the Java Appletviewer or the HotJava browser (Art Taylor 1997)
CSE.RU-1.24
CSE298
CSE300
JAVA and JDBC

Rather than simply being used to “bring life to
Web pages”, Java with database now can fill the
role of database –aware applications for a
variety of needs
 currently common gateway interface (CGI)
applications are required to access databases
from within Web browsers
 Java applets with JDBC now have the
potential to provide the functionality with
more direct programming and the potential
for improved performance.
CSE.RU-1.25
CSE298
CSE300
Java Applet

A Java applet is a small, compact application
downloaded as Java bytecodes over the Internet
by a World Wide Web browser.
 programs represent one of the more common
uses of a Java application and can perform
most of the functions of a regular Java
application
 An applet can either trusted or untrusted. A
trusted applet has convinced the Java virtual
machine that it can be trusted either by
providing a cryptographic key or some other
means. Once trusted, this applet could have
access to local data.
CSE.RU-1.26
CSE298
CSE300
Java Applet

An applet that has not provided some means
of authentication is considered untrusted and
would not have access to any local data. This
applet could, however. Open a database
connection back to the server from which it
was downloaded.

An applet is designed to embedded in a Web
page and then to supplement the HTML
page being rendered by the browser.

Because an applet is a Java program, it is
much easier to perform complex functions
with an applet than with HTML, which is
not a program language.
CSE.RU-1.27
CSE298
CSE300
Java Applet

An applet can use GUI interface. Some GUI
facilities are available as part of an HTML page,
but Java allows a finer level of programming
with the GUI controls.

Java applet make the programming of specialpurpose applications simpler and practically
eliminate the software distribution cost for these
programs. When combined with JDBC to enable
access to relational databases, the possibility for
special-purpose Java applet is greatly expanded.
CSE.RU-1.28
CSE298
CSE300

Applet Issues
There are also a number of issues to be considered.
These are especially issues that must be considered
with all Web applications, specially performance
and security.
 Access bandwidth over the Internet tends to
vary and impacts performance. An application
that access a database through the Internet
most likely will perform worse than the same
application run over the local network.
 An applet downloaded over the Internet operates
within a fairly restrictive set of security rules. The
object of these rules is to protect the interest of the
user by protecting their client machine, but the
programmer of the applet must consider these
rules when designing the application.
CSE.RU-1.29
CSE298
CSE300
Applet Issues


In general, normal unsigned applets are
considered untrustworthy and are not
allowed access to any local database or local
files.
An applet that manages to convince the local
Java machine that it can be trusted by
providing a password or cryptographic key
CSE.RU-1.30
CSE298
CSE300
JDBC Application
State
Implementation
Interface
Identifier
Principals

Java JDBC can be two-tiered (conventional
client-server) or three-tiered(partitioned
application).

While it is expected that many applications will
make use of two-tiered application development,
there are advantages to the three-tiered
approach
CSE.RU-1.31
CSE298
CSE300
Two-tiered Application

There is a front- end portion of the application that
request data. The data is stored on and retrieved
from a server. The following figure is an example.
Tier One
Client
Database Instances
Tier
Two
CSE.RU-1.32
CSE298
CSE300
Two-tiered Application


This approach has the benefit of moving front-end
processing off the server where there is usually a
significant demand for CPU cycles and places
processing on the client where there are usually
plenty of CPU cycles available.
It has the benefit of allowing data collection to be
performed in what is usually a rich GUI
environment such as windows. The GUI
environment requires CPU cycles and fast access to
the display device.To run such an application on
the server could require CPU resources that could
be scarce on a busy server, and significant
bandwidth to the display device.
CSE.RU-1.33
CSE298
CSE300
Two-tiered Application

Disadvantage
 When an application must retrive a large
number of rows from the server and process the
rows in application memory , then the
traditional two-tiered, client-server model to
suffer performance degradation.
CSE.RU-1.34
CSE298
CSE300
Two-tiered Realization of The CSCC
Project
Client
Tier One
Database Instances
Tier
Two
WebPage
Applet
Data
CSE.RU-1.35
CSE298
CSE300
The Webpage

Dreamwaver3

Firework3

Flash4

Photoshop5.5

Paintshop.
CSE.RU-1.36
CSE298
CSE300
The Applet
Client Program
Customer
Login
Data
Search
Order
Customer
Database
Update
Oracle
Database
CSE.RU-1.37
CSE298
CSE300
Advantages of this Approach

Because the applet runs on the client, so
the approach saves Server CPU cycles
and spaces.

The cost of the realization is less
expensive than three tier’s approaches.
Because it saves one server
CSE.RU-1.38
CSE298
CSE300
The disadvantages of the approach

In a large enterprise, there is a need to control
data access and enforce enterprise rules.

If it is supported by the database server, some
portion of these rules can be enforced using
stored procedures on the database server.

In more complicated business rules, using a
three tiered approach, a central application tier
composed of remote methods and accessed using
RMI can be used to enforce these rules.
Creation and Disposal
CSE.RU-1.39
CSE298
CSE300
Future Work

Because of the equipment restriction, we did not
try the application by putting applet and data to
the same server.

If we use signed applet, it will spend about $400
to get the ID,certificate.So we can not try the
signed applet on the Internet Explorer or
Netscape. We only tested the program by using
Appletviewer.

If in the future someone has chance , to test the
approach in the same server. It will be fine.
CSE.RU-1.40
CSE298
CSE300
Benefits of Enterprise JavaBeans








Developer Architecture Independent
Write once, run anywhere (WORA)
Establish Roles for Application Development
EJBs Takes Care of Transaction Management
Provides Distribute Transaction Support
Creates Portable and Scalable Solution
Seamlessly Integrates with CORBA
Provides for Vendor Specific Enhancement
CSE.RU-1.41
CSE298
CSE300
Modeling using Session and Entity EJBs

Use Session Beans for
Application Logic

Use Session Beans as the
only interface to the
Client


Expect Little Reuse of
Session Beans
Use Session Beans to
Control the Workflow of
a Group of Entity Beans




Use Entity Beans to
develop a persistent
object model
Use Entity Beans to
Enforce
Accuracy/Integrity of the
Database
Insist on Reuse of Entity
Beans
Model Domain Objects
with a Unique Identity
Shared by Multiple
Clients
CSE.RU-1.42
CSE298
CSE300
The Architecture of this Application
J2EE Server
EJB Server
JDBC
Business
Logic
Entity Beans
Entity Beans
… ...
Session Beans
Database
RMI/IIOP
HTML
HTTP
Web Server
Dynamically
generated
presentation
Servlets
CSE.RU-1.43
CSE298
CSE300
Component Diagram of Enterprise
Beans
CSE.RU-1.44
CSE298
CSE300
Developing Web-based Application with
J2EE







Develop Entity Beans
Develop Session Beans
Develop Clients
Start up the Server & Deploy Tool
Build the Bean and War Components (Define the
deployment descriptor)
Deploy the Application
 Specify the JNDI name and Web Context name
Run the Application
CSE.RU-1.45
CSE298
CSE300
Developing & Deploying Entity Beans








Define Home Interface
Define Remote Interface
Define a Primary Key Class
Implement the Bean Class
Compile the Above Classes
Set up the Data Source to the Database
 With BMP, You Should Create the Database
Manually
 With CMP, the Container Create the Database
at Deploying Time
Define the Deployment Descriptor with
Deploytool
Start J2EE Server and Your are Ready to Deploy
CSE.RU-1.46
CSE298
CSE300
Define Home Interface




The Home Interface Defines the Life Cycle of an
Enterprise Bean
It Extends javax.ejb.EJBHome Interface
create<METHOD>(…) Methods
 The Return Type Must Be the Entity Bean’s
Remote Interface Type
 find<METHOD>(…) Methods
 findByPrimaryKey(PrimaryKey pk) Method is
Mandatory for Entity Beans
Home Methods (Only in EJB2.0)
CSE.RU-1.47
CSE298
CSE300
Home Interface Sample
// Sample entity bean home interface
import javax.ejb.*;
import java.rmi.RemoteException;
import java.util.Collection;
public interface CustomerHome extends EJBHome{
Customer create(String customerID,String name,String address,String password)
throws CreateException, RemoteException;
public Customer findByPrimaryKey(String customerID)
throws FinderException,RemoteException;
public Collection findByName(String name)
throws FinderException,RemoteException;
}
CSE.RU-1.48
CSE298
CSE300
Define Remote Interface



The Remote Interface Defines the Business
Methods of an Enterprise Bean
It extends javax.ejb.EJBObject Interface
For Every Method in the Remote Interface, There
is a Corresponding Method in the Bean
Implementation
import javax.ejb.*;
import java.util.Vector;
import java.rmi.RemoteException;
public interface Customer extends EJBObject{
// simple get and set methods
public String getName() throws RemoteException;
public void setName(String name) throws RemoteException;
public String getPassword() throws RemoteException;
public void setPassword(String password) throws RemoteException;
public String getAddress() throws RemoteException;
public void setAddress(String address) throws RemoteException;
}
CSE.RU-1.49
Define Primary Key Class for Entity
Bean
CSE298
CSE300

The Primary Key Class is Used to Uniquely
Identify an Object Reference to an Entity Bean
import java.io.Serializable;
public class AccountPK implements Serializable{
public String accountID;
public AccountPK(){}
public AccountPK(String accountID){
this.accountID = accountID;
}
public int hashCode(){
return accountID.hashCode();
}
public boolean equals(Object other) {
if ((other == null) || !(other instanceof AccountPK))
return false;
AccountPK otherPK = (AccountPK) other;
return accountID.equals(otherPK.accountID);
}
}
CSE.RU-1.50
CSE298
CSE300
Develop Entity Bean Class



Implement the Business Logic Methods
Implement ejbCreate<METHOD>(…) Methods
and ejbPostCreate<METHOD>(…) Methods
Implement ejbFind<METHOD>(…) Methods



ejbFindByPrimaryKey(PrimaryKey pk) Method is Mandatory
Implement ejbHome<METHOD>(…) Methods
(Only in EJB2.0)
Implement EJB Call Back Methods







ejbActivate(): Called When the Bean is Activated
ejbPassivate(): Called When the Bean is Passivated
ejbLoad(): Forces the Bean to Load State from Database
ejbStore() : Forces the Bean to Store State from Database
ejbRemove(): Called When Client Calls Remove()
setEntityContext(...): Called by Container on Creation
unsetEntityContext(): Called by Container Before Being Removed
CSE.RU-1.51
CSE298
CSE300
Sample Entity Bean Class
// This is a Container Managed Persistence Bean
import …
public class CustomerBean implements EntityBean{
protected EntityContext ctx; public String customerID; … …
public CustomerBean(){}
// business methods
public String getCustomerName() throws RemoteException
{
return customerName;
}
public void setName(String name) throws RemoteException
{
this.customerName=customerName;
}
// create and finder methods
public CustomerPK ejbCreate(…) throws CreateException,RemoteException
{ this.customerID=customeID; … ;return null; }
public void ejbPostCreate(…) throws CreateException,RemoteException{}
public CustomerPK ejbFindByPrimaryKey(CustomerPK primaryKey)throws … {}
// EJB call back methods
public void setEntityContext(EntityContext ctx)throws RemoteException {this.ctx=ctx;}
public void unsetEntityContext() throws RemoteException { this.ctx = null; }
public void ejbActivate() throws RemoteException {}
public void ejbPassivate() throws RemoteException {}
public void ejbStore() throws RemoteException{}
public void ejbLoad() throws RemoteException{}
public void ejbRemove() throws RemoteException{}
CSE.RU-1.52
}
CSE298
CSE300
Developing & Deploying Session Beans

Define the Home Interface

Define the Remote Interface

Implement the Bean Class

Compile Your Code and Define the Deployment
Descriptor with Deploytool

Start up J2EE Server and You are Ready to Deploy
CSE.RU-1.53
CSE298
CSE300
Develop War Components

Develop Servlets

Compile Your Code

Use Deploytool to Assemble Your War Component
 Designate Servlet Class
 EJB References
 Role
 Security
 Transaction
CSE.RU-1.54
CSE298
CSE300
State Diagram for the Servlets
Tell what data available
by querying ClimateData
bean
Authenticate user by
querying Customer
entity bean
Storefront
Servlet
Login
Servlet
Avail Servlet
Return to the
main page
Newuser
Servlet
Verify Servlet
Verify an account by
querying Account
bean
Create a new customer and send
mail to the user using Confirm
session bean
Display
Servlet
Fig. 4 State digram for the servlet
Charge a customer through
Teller bean and display the
data.
CSE.RU-1.55
CSE298
CSE300
Develop Servlets




Locate the Bean Container Using JNDI &
RMI/IIOP
Create the Bean if Necessary (When Business
Method Required)
Use the Bean
Dispose of the Bean (Call remove() Method)
CSE.RU-1.56
How the Client Locates the Home
Object
CSE298
CSE300



Get or Create a JNDI Naming Context
Use the Context to Lookup the EJB Home Object
Through RMI/IIOP
Use the Home Object to Create the Beans.
try {
// Get the JNDI naming context
Context initial = new InitialContext();
// Use the context to lookup the EJB home interface
Object objref = initial.lookup("TheAccount");
// Use RMI/IIOP to get the EJB home interface
accountHome = (AccountHome)
PortableRemoteObject.narrow(objref,AccountHome.class);
}
catch(Exception e){
throw new ServletException(e.toString());
}
CSE.RU-1.57
CSE298
CSE300
Sample EJB Clients
… ...
public class NewuserServlet extends HttpServlet{
private CustomerHome customerHome;
public void init() throws ServletException{
try { // Locate the Home Object
Context initial = new InitialContext();
Object objref = initial.lookup("TheCustomer");
customerHome =
(CustomerHome) PortableRemoteObject.narrow(objref,CustomerHome.class);
}
catch(Exception e){… … …}
}
public void doGet((HttpServletRequest request,...)
throws ServletException, IOException{
………
try{ //Call finder methods or obtain Remote Reference
customerHome.findByPrimaryKey(new CustomerPK(customerID));
customerHome.create(…);
}
………
CSE.RU-1.58
CSE298
CSE300
Conclusions and Future Research


Conclusions
 Easy to Develop with J2EE
 Flexible
 Powerful
Future Research
 New Feture in EJB2.0
 Message-driven Bean
 Persistence Provider
 etc.

Fully Explore EJBs.
 JNDI, RMI, JSP, XML, JMS, Java IDL, JTS, JTA,
JavaMail, and JAF
CSE.RU-1.59
CSE298
CSE300
System Evaluation

Learning vs. Developing

Code Length

Efficiency

Influence On Each Approach When Database
Changes

Influence On Each Approaches When Business
Rules Change
CSE.RU-1.60
CSE298
CSE300
Learning vs. Developing

EJBs
 two months to learn while half a month to
develop

Servlet
 Have to know certain knowledge about Java.

Applet
 The easiest to learn.
CSE.RU-1.61
CSE298
CSE300
Code Length

EJBs
 Shortest code length, lots of duplicate codes.

Servlet
 Has the longest code length

Applet
 Between EJBs and Servlet.
CSE.RU-1.62
CSE298
CSE300
Efficiency

EJBs
 Has the worst efficiency in the three
approaches

Servlet
 Faster than EJBs

Applet
 Fastest among the three approaches.
Directly access the database by using the
low-level interface
CSE.RU-1.63
CSE298
CSE300
Influence on Each Approach When
Database Changes

EJBs
 Simply re-designate the data source and redeploy the application, no source code
change needed.

Servlet
 Have to change code and recompile and redeploy.

Applet
 Have to change code and recompile and redeploy
CSE.RU-1.64
CSE298
CSE300
Influence On Each Approaches When
Business Rules Change

EJBs
 Rewrite the session beans

Servlet
 Can update the rules but not flexible.

Applet
 Difficult to update or enhance the rule
CSE.RU-1.65
CSE298
CSE300
Conclusions

JDBC offers a database-independent means to
enable data for use in Java application

Java Servlet architecture provides a component
container model that can result in a significant
amount of simplified HTTP communication

EJB provides a standard component model for
developing distributing application as
components that operate inside of an EJB
container
CSE.RU-1.66
CSE298
CSE300
Future Work

JavaServer Pages
 Combination of markup with Java Code to
produce a dynamic web page

Servlet
 JINI and JavaSpace

Java RMI and IDL
 Homogeneous & heterogeneous network
environments
CSE.RU-1.67