Transcript j2ee
Developing Enterprise Applications
Using Java
School of Technology of Setubal
Setubal, Portugal
April 8, 2002
Qusay H. Mahmoud
School of Computing Science
Simon Fraser University, Vancouver, B.C., Canada
[email protected]
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
1
Objectives
Understand the architecture of the J2EE
Understand the technologies involved in
J2EE
Know how to build web-based enterprise
applications
Know what effort is involved in development
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
2
Agenda
The J2EE Platform and SDK
The J2EE APIs
Web Components (Servlets, JSPs)
Enterprise JavaBeans Technology (EJBs)
Duke’s Bank Application
Security issues
Web Services
Online Resources
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
3
Enterprise Applications
Enterprise is a synonym for distributed
Distributed computation done by programs
interacting over the network
Enterprise is a hot buzzword
Reputation for complexity
Enterprise computing takes place in a
heterogeneous network
Involve the use of many different network protocols
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
4
Buzzword: N-tier apps
Used to describe applications that reside on
client and several servers
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
5
Why Java?
It helps alleviate intimidating aspects of
enterprise computing
Java is portable (Write Once, Run Everywhere)
Enterprise APIs form a single standard layer on
top of various proprietary or vendor-enhanced
APIs
The elegance of Java and OO power make the
APIs simpler, easier to understand, and easier to
use
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
6
Java Platforms
[Source: Sun Microsystems Website]
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
7
J2EE Platform
Brings the benefits of component-based
development to enterprise applications
Components are
Simpler to develop
Portable
Reusable
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
8
J2EE Components
Server-side components
Enterprise JavaBeans (EJBs)
Java Servlets
JavaServer Pages (JSPs)
Client-side components
Application client
Configured via deployment descriptors
Deployed into containers
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
9
J2EE Components packed
into Modules
Enterprise Beans (JAR file)
Classes
Deployment descriptor
Web Components (WAR file)
Servlet classes, JSPs, HTML, images
Web application deployment descriptor
Application Clients
Classes
Application client deployment descriptor
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
10
J2EE Applications
Packed into Enterprise ARchives (EAR file)
Configured via application deployment descriptors
<application>
<display-name>BankApp</display-name>
<module>
<ejb>account-ejb.jar</ejb>
</module>
…
<module>
<web>
<web-uri>bank.war</web-uri>
<context-root>bank</context-root>
</web>
</module>
</application>
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
11
J2EE Standard APIs
JDBC
Java Naming and Directory Interface (JNDI)
Java Message Service (JMS)
Remote Method Invocation (RMI)
Enterprise JavaBeans (EJBs)
Technologies
Servlets
JavaServer Pages (JSPs)
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
12
Other J2EE APIs
J2EE Connectors
Java Transaction API (JTA)
JavaIDL
RMI-IIOP
Java Transaction Service (JTS)
JavaMail
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
13
Multi-tiered Architecture
Advantages:
Decouples accessing of data from end-user applications
Encourages component-based code reusability
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
14
J2EE SDK
J2EE Server
Cloudscape database
Application deployment tool
Create and edit deployment descriptors
Package components and J2EE applications
Verify and deploy J2EE applications
Download: http://java.sun.com/j2ee/download.html
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
15
J2EE SDK Tools
j2eeadmin: add/remove resources
cleanup: remove deployed apps from server
cloudscape: start/stop/query database server
deploytool: deploy/undeploy applications
j2ee: start/stop J2EE server
keytool: create keys and generate certificates
packager: package J2EE components
realmtool: add/remove J2EE users and import
certificates
verifier: validate J2EE archive files
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
16
J2EE Server
Standard containers (EJBs and Web)
Life cycle management
Concurrent execution
Standard platform services
Security
Transactions
Resource connections
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
17
J2EE Containers
A container is the interface between a
component and the low-level platform
specific functionality
J2EE server provides services in the form of
a container for every component type
You don’t have to develop these services
yourself; you concentrate on solving the
business problem
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
18
J2EE Containers
Before a component can be executed, it
must be assembled into a J2EE application
and deployed into its container
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
19
Web Components
Used to generate dynamic Web content
Run on the server-side
Portable
Reusable
They can be:
Based on the Java Servlet API
Based on the JavaServer Pages (JSPs)
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
20
HTTP Overview
An application-level protocol
Supports several request methods ( such as
GET and POST)
GET http://www.yahoo.com HTTP/1.0
Request
Reply
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
21
Servlets
A servlet is a Java class
Functions:
Process requests
Create replies
Deployed into a Web container, which
Manages servlet life cycle
Maps URLs to servlets
Converts HTTP requests and responses to objects
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
22
Servlet Example
A simple servlet
public class HelloServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(“<html><head><title>hello</title></head> ");
out.println(<body><h1>Hello</h1></body></html>);
}
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
23
Servlet as a Mediator
Devices that have no support for socket or
datagram connections, access ‘net services
through a mediator
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
24
JavaServer Pages (JSPs)
JSP is a document that contains two types of
text
Template data (HTML, XML, WML) that
generates static content
JSP elements that generate dynamic content
Delegates dynamic processing to EJBs,
JavaBeans components, custom tags
Translates into and executes as a servlet
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
25
JSP Elements
Directive controls translation into servlet class
<%@ page import=“java.io.*” %>
Declaration creates object or method
<%! int i %>
Scriptlet for dynamic processing
<% ArrayList accts =
beanManager.getAcountController().getAccountsOfCusto
mer(request.getRemoteUser()); %>
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
26
JSP: JavaBeans Components
Create components and access their properties
<jsp:useBean id=“accountHistBean”
class=“AccountHistorybean”
scope=“request”/>
scope: page, request, session, or application
<jsp:setProperty name=“accountHistbean”
property=“beanManager” value=“<%=
beanManager %>” />
value can be a String or a JSP expression
<jsp:getProperty name=“accountHistBean”
property=“credits” />
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
27
JSP: Custom Tags
Extensions to JSP that encapsulate dynamic
processing
Tag libraries
Standard Tag Library (JSR 052)
Struts: http://jakarta.apache.org/struts
Jakarta Taglibs: http://jakarta.apache.org/taglibs
Examples (we will see them in action)
Effort involved in developing custom tags
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
28
Enterprise JavaBeans
What is an Enterprise Bean?
Component for EJB technology
Runs on the server
Encapsulates business logic
Portable
Reusable
Transactional
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
29
How do Enterprise beans
simplify development
Enterprise bean developer
Focuses on solving business problems
EJB container
Provides system-level services
Client developer
Focuses on presentation and usability
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
30
Type of Enterprise Beans
Session beans
Entity beans
Message-driven beans
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
31
What is a Session Bean?
Represents a single client in the server
A logical extension of the client
Transient state – not persistent
Examples
Sample session beans in action
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
32
What is an Entity Bean?
Represents a business object in a persistent
storage (e.g. a database)
Often provides an object view of a database
table
Examples
Sample entity beans in action
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
33
What is a Message-Driven
Bean?
Based on Java Message Service (JMS)
technology)
Message listener
Consumes messages from a queue
The onMessage() method is invoked upon
message arrival
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
34
Structure of an EJB
Enterprise Bean Class
Implementation
Home Interface
Life cycle methods
Remote Interface
Business methods
Deployment Descriptor
XML file
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
35
Duke’s Bank Application
Online access to bank accounts
Customers – web clients
Withdraw and deposit funds
Account history
Transfer funds
Administrators (J2EE application client)
Manage customers
Manage accounts
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
36
Duke’s Bank Application
High-level architecture
[source: Sun Microsystems Website]
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
37
Duke’s Bank Client’s View
Defining client’s view with interfaces
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
38
Duke’s Bank Application
Enterprise Beans
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
39
Duke’s Client Application
Client: locate, create, deposit
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
40
Duke’s Bank Application
TxControllerEJB: Deposit
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
41
Duke’s Bank Application
TxEJB: Create
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
42
Duke’s Bank Application
Web Component Integration
[source: Sun]
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
43
Duke’s Bank Application
Security
Secure each container to secure entire
application
Use authentication to prove identity
Use authorization to restrict access
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
44
Application Client Security
Admin uses the application client to create
customers and accounts
Must authenticate to access any
management functions by the application
client
Login screen pops up when the application client
is accessed
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
45
Duke’s Bank Application
Creating a customer account
Admin creates the customer record
Customer ID is embedded in the customer record
Admin uses realmtool to add customer ID,
password, group to default realm
Realm –add 200 j2ee customer
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
46
Duke’s Bank Application
Creating an account
Admin accesses the application client and
creates an account for the customer
Create methods in the enterprise bean can only
be accessed by users in the admin role
The customer ID, found in the customer record,
binds the account to the customer
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
47
Duke’s Bank Application
Securing the Web Components
Web components are protected when the application is
deployed
Customer must authenticate to access web components
A form-based login pops up when the customer tries to access
web component
Web component retrieves the customer ID by calling
getUserPrincipal()
<% ArrayList accts =
beanManager.getAccountController().getAccountsOfCusto
mer(request.getUserPrincipal().getName()); %>
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
48
Duke’s Bank Application
Securing the Enterprise Beans
Enterprise beans provide methods for creating customer
and accounts
Method permissions on the home interface restrict
account and customer creation to admin role
Roles and mapping between users and roles is specified
at application deployment time
Enterprise beans provide methods for crediting, debiting,
transferring funds
Methods permission on the remote interface restricts
these operations to customer role
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
49
Transaction Management
Indivisible unit of work
Atomic (all or nothing)
Committed or rolled back
Bean-managed
Your code sets transaction boundaries
Container-managed
EJB container sets boundaries
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
50
Resources Connections
Types:
Database connections
Java Message Service API-based objects
Mail sessions
URLs
Resource adapters
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
51
Resource Connections
Example
private String dbName =
“java:comp/env/jdbc/BankDB”;
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbName);
Connection con = ds.getConnection();
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
52
Connector Architecture
Enables J2EE components such as EJB to
interact with enterprise information systems
A resource adapter is a J2EE component that
implements the J2EE connector architecture
for a specific enterprise information system
A resource adapter is analogous to a JDBC
driver
For JDBC: the outside resource is DBMS
For resource adapter: the outside resource is
enterprise information system
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
53
Web Services
Services offered through the web and can be
accessed from any web-services enabled
machine with Internet access
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
54
Online Resources
J2EE:
http://java.sun.com/j2ee
J2EE Tutorial:
http://java.sun.com/j2ee/tutorial
J2EE white papers:
http://java.sun.com/j2ee/white
J2EE BluePrints:
http://java.sun.com/blueprints
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
55
Online Resources
JSP Taglibs:
http://java.sun.com/products/jsp/taglibraries.html
Tomcat
http://jakarta.apache.org/tomcat
Web Services
http://java.sun.com/webservices
Deploying Web Services on J2EE:
http://developer.java.sun.com/developer/tec
hnicalArticles/WebServices/wsj2ee
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
56
Thank you
Q&A
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
57
Copyright Info
Java™ and J2EE ™ are registered trademarks of
Sun Microsystems, Inc.
Part of the slides have been adapted from J2EE
Tutorial and JavaOne Sessions 2001.
All trademarked product and company names are
the property of their respective trademark
holders.
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
58
Speaker Bio
Qusay H. Mahmoud is a faculty member in the
School of Computing Science at Simon Fraser
University, Canada. He is widely published on the
Java programming language, including the MIDP
and Palm programming articles for Sun
Microsystems Java Developer Connection. Qusay is
the author of Distributed Programming with Java
(Manning Publications, 1999) and Learning Wireless
Java (O’Reilly & Associates, 2002)
http://www.cs.sfu.ca/~qmahmoud
http://www.javacourses.com
Copyright © 2002 Qusay H. Mahmoud
59