Upload
vuquynh
View
220
Download
7
Embed Size (px)
Citation preview
13/1/2012
1
J2EE Web Development
Agenda
� Application servers
� What is J2EE?� Main component types� Application Scenarios� J2EE APIs and Services
� Examples
1. Application Servers
� In the beginning, there was darkness and cold. Then, …
Centralized, non-distributed
terminals
mainframe
terminals
Application Servers
� In the 90’s, systems should be client-server
13/1/2012
2
Application Servers
� Today, enterprise applications use the multi-tier model
Application Servers
� “Multi-tier applications” have several independent components
� An application server provides the infrastructure and services to run such applications
Application Servers
� Application server products can be separated into 3 categories:
� J2EE-based solutions
� Non-J2EE solutions (PHP, ColdFusion, Perl, etc.)
� And the Microsoft solution (ASP/COM and now .NET with ASP.NET, VB.NET, C#, etc.)
J2EE Application Servers
� Major J2EE products:� BEA WebLogic
� IBM WebSphere
� Oracle AS
� Jboss (free open source)
� …
13/1/2012
3
Web Server and Application Server
Web Server(HTTP Server)
App Server 1
App Server 2
Internet Browser
HTTP(S)
2. What is J2EE?
� It is a public specification that embodies several technologies
� J2EE defines a model for developing multi-tier, web based, enterprise applications with distributed components
J2EE Benefits
� High availability
� Scalability
� Integration with existing systems
� Freedom to choose vendors of application servers, tools, components
� Multi-platform
J2EE Benefits
� Flexibility of scenarios and support to several types of clients
� Programming productivity:
� Services allow developer to focus on business
� Component development facilitates maintenance
and reuse
� Enables deploy-time behaviors
� Supports division of labor
13/1/2012
4
Main technologies
� JavaServer Pages (JSP)
� Servlet
� Enterprise JavaBeans (EJB)
� JSPs, servlets and EJBs are application components
JSP
� Used for web pages with dynamic content
� Processes HTTP requests (non-blocking call-and-return)
� Accepts HTML tags, special JSP tags, and scriptlets of Java code
� Separates static content from presentation logic
� Can be created by web designer using HTML tools
Servlet
� Used for web pages with dynamic content
� Processes HTTP requests (non-blocking call-and-return)
� Written in Java; uses print statements to render HTML
� Loaded into memory once and then called many times
� Provides APIs for session management
EJB
� EJBs are distributed components used to implement business logic (no UI)
� Developer concentrates on business logic
� Availability, scalability, security, interoperability and integrability handled by the J2EE server
� Client of EJBs can be JSPs, servlets, other EJBs and external aplications
� Clients see interfaces
13/1/2012
5
J2EE Multi-tier Model
J2EE Application Scenarios
� Multi-tier typical application
J2EE Application Scenarios
� Stand-alone client
J2EE Application Scenarios
� Web-centric application
13/1/2012
6
J2EE Application Scenarios
� Business-to-business
J2EE Services and APIs
� JDBC
� JavaMail
� Java API for XML Parsing (JAXP)
� Web services APIs
Types of EJB
Session Bean
� Stateful session bean:
� Retains conversational state (data) on behalf of an individual client
� If state changed during this invocation, the same state will be available upon the following invocation
� Example: shopping cart
13/1/2012
7
Session Bean
� Stateless session bean:
� Contains no user-specific data
� Business process that provides a generic service
� Container can pool stateless beans
� Example: shopping catalog
Entity Bean
� Represents business data stored in a database � persistent object
� Underlying data is normally one row of a table
� A primary key uniquely identifies each bean instance
� Allows shared access from multiple clients
� Can live past the duration of client’s session
� Example: shopping order
Entity Bean
� Bean-managed persistence (BMP): bean developer writes JDBC code to access the database; allows better control for the developer
� Container-managed persistence (CMP):container generates all JDBC code to access the database; developer has less code to write, but also less control
3. Examples
� JSP example
� Servlet example
� EJB example
13/1/2012
8
JSP example
JSP example<%@ page import="hello.Greeting" %>
<jsp:useBean id="mybean" scope="page" class="hello.Greeting"/>
<jsp:setProperty name="mybean" property="*" />
<html><head><title>Hello, User</title></head><body bgcolor="#ffffff" background="background.gif" >
<%@ include file="dukebanner.html" %>
<table border="0" width="700"><tr><td width="150"> </td>
<td width="550"> <h1>My name is Duke. What's yours?</h1></td></tr>
JSP example<tr> <td width="150" </td> <td width="550"><form method="get"><input type="text" name="username" size="25"> <br><input type="submit" value="Submit"><input type="reset" value="Reset"></td> </tr></form> </table><%
if (request.getParameter("username") != null) {%><%@ include file="response.jsp" %><%
}%></body></html>
Servlet examplepublic class HelloWorldServlet extends HttpServlet {
public void service(HttpServletRequest req,
HttpServletResponse res) throws IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><head><title>Hello
World Servlet</title></head>");
out.println("<body><h1>Hello
World!</h1></body></html>");
}
}
13/1/2012
9
EJB Example
// Shopping Cart example
// Home interface
public interface CartHome extends EJBHome {
Cart create(String person)
throws RemoteException, CreateException;
Cart create(String person, String id)
throws RemoteException, CreateException;
}
EJB Example
// Remote interface
public interface Cart extends EJBObject {
public void addBook(String title)
throws RemoteException;
public void removeBook(String title)
throws BookException, RemoteException;
public Vector getContents()
throws RemoteException;
}
EJB Example// Enterprise bean class
public class CartEJB implements SessionBean {String customerName, customerId;
Vector contents;private SessionContext sc;
public void ejbCreate(String person) throws CreateException {
if (person == null) {throw new CreateException("Null person not allowed.");
}else {
customerName = person;}
customerId = "0";contents = new Vector();
}
EJB Examplepublic void ejbCreate(String person, String id)
throws CreateException {if (person == null) {throw new CreateException("Null person not allowed.");
}else {customerName = person;
}IdVerifier idChecker = new IdVerifier();if (idChecker.validate(id)) {customerId = id;
}else {throw new CreateException("Invalid id: " + id);
}contents = new Vector();
}
13/1/2012
10
EJB Examplepublic void addBook(String title) {
contents. addElement(title);}
public void removeBook(String title) throws BookException {boolean result = contents.removeElement(title);if (result == false) {throw new BookException(title + " not in cart.");
}}
public Vector getContents() {return contents;
}
. . .}
EJB Example// EJB client (stand-alone application)public class CartClient {
public static void main(String[] args) {try {CartHome home = (CartHome)initial.lookup("MyCart");Cart shoppingCart = home.create("Duke DeEarl", "123");shoppingCart.addBook("The Martian Chronicles");shoppingCart.addBook("2001 A Space Odyssey");shoppingCart.remove();
} catch (BookException ex) {System.err.println("Caught a BookException: "
+ ex.getMessage());} catch (Exception ex) {System.err.println("Caught an unexpected exception!");
}}
}
Questions