第十四章 J2EE 介绍 1.Java 2 Platform Enterprise Edition (J2EE) 1.3 API 2.J2EE Architecture 3.J2EE Application Development Lifecycle 4.JSP 5.Servlet 6.JavaBean.

Slides:



Advertisements
Similar presentations
Java Server Pages (JSP)
Advertisements

JSP1 Java Server Pages (JSP) Introducing JavaServer Pages TM (JSP TM ) JSP scripting elements.
 2002 Prentice Hall. All rights reserved. Chapter 9: Servlets Outline 9.1 Introduction 9.2 Servlet Overview and Architecture Interface Servlet and.
Java Servlet & JSP © copyright 2005 SNU OOPSLA Lab.
Objectives Ch. D - 1 At the end of this chapter students will: Know the general architecture and purpose of servlets Understand how to create a basic servlet.
An introduction to Java Servlet Programming
Overview of The Java Platform Solution for E-Business Applications : JSP, Servlet and EJB.
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
JSP Java Server Pages Reference:
Writing Enterprise Applications with J2EE (Third lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Introduction to Servlet & JSP
A Case Study on the J2EE Platform CPSC550 Graduate Student Seminar Presentation by Jeffrey A. Brown.
JSP Architecture  JSP is a simple text file consisting of HTML or XML content along with JSP elements  JSP packages define the interface for the compiled.
CSCI 6962: Server-side Design and Programming History and Background.
Java Servlets and JSP.
Servlet and JSP Programming: An Introduction Spiros Papadimitriou
Gayle J Yaverbaum, PhD Professor of Information Systems Penn State Harrisburg.
15 - RMI/EJBCSC4071 Distributed Objects Java Remote Method Invocation Enterprise Java Beans.
Writing Enterprise Applications with J2EE (Second lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
Servlets. - Java technology for Common Gateway Interface (CGI) programming. - It is a Java class that dynamically extends the function of a web server.
Introduction to JSP Based on: Marty Hall, Larry Brown, Core Servlets and JavaServer Pages.
JSP Java Server Pages Softsmith Infotech.
Java support for WWW Babak Esfandiari (sources: Qusay Mahmoud, Roger Impey, textbook)
Java Servlets and Java Server Pages Carol Wolf Computer Science.
J2EE training: 1 Course Material Usage Rules PowerPoint slides for use only in full-semester, for-credit courses at degree-granting.
Li Tak Sing COMPS311F. Static attributes in Servlets Since Servlets are also Java classes, you can also use static attributes to store values that can.
Chapter 7 Java Server Pages. Objectives Explain how the separation of concerns principle applies to JSP Describe the operation and life-cycle of a JSP.
Introduction to JavaServer Pages (JSP) Slides from Dr. Mark Llewellyn.
CMPUT 391 – Database Management Systems Department of Computing Science University of Alberta CMPUT 391 Database Management Systems Web based Applications,
Web Server Programming 1. Nuts and Bolts. Premises of Course Provides general introduction, no in-depth training Assumes some HTML knowledge Assumes some.
JAVA SERVER PAGES. 2 SERVLETS The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
A TUTORIAL TO USING EJBs by SHREERAM IYER 09/17/2001.
JAVA SERVER PAGES CREATING DYNAMIC WEB PAGES USING JAVA James Faeldon CS 119 Enterprise Systems Programming.
Slides © Marty Hall, book © Sun Microsystems Press 1 JSP Scripting Elements Core Servlets & JSP book:
Java Servlets & Java Server Pages Lecture July 2013.
Java Servlets Lec 27. Creating a Simple Web Application in Tomcat.
 2000 Prentice Hall, Inc. All rights reserved. Java Multimedia: Images, Animation, Audio and Video Outline 30.1 Introduction 30.2Loading, Displaying and.
JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.
CSC 2720 Building Web Applications JavaServer Pages (JSP) The Basics.
Chapter 2 Web app architecture. High-level web app architecture  When a client request coming in and needs servlet to serve dynamic web content, what.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
20-Nov-15introServlets.ppt Intro to servlets. 20-Nov-15introServlets.ppt typical web page – source Hello Hello.
A seminar on j2ee by saritha. s. What is J2EE J2EE (Java 2 Platform, Enterprise Edition) is a Java platform designed for the mainframe-scale computing.
Chapter 11 Invoking Java Code with JSP Scripting Elements.
Java Servlets and Java Server Pages Norman White Stern School of Business.
JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the page is HTML –Dynamic portion is Java Easy way to develop.
Middleware 3/29/2001 Kang, Seungwoo Lee, Jinwon. Description of Topics 1. CGI, Servlets, JSPs 2. Sessions/Cookies 3. Database Connection(JDBC, Connection.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, Responds oriented other.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
©SoftMoore ConsultingSlide 1 Overview of Servlets and JavaServer Pages (JSP)
Java Servlets and Java Server Pages
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
INVOKING JAVA CODE WITH JSP SCRIPTING ELEMENTS. Creating Template Text A large percentage of your JSP document consists of static text (usually HTML),
 Java Server Pages (JSP) By Offir Golan. What is JSP?  A technology that allows for the creation of dynamically generated web pages based on HTML, XML,
JSP JavaServer Pages. What is JSP? Java based technology that simplifies the development of dynamic web sites JSP pages are HTML pages with embedded code.
Java Programming: Advanced Topics 1 Building Web Applications Chapter 13.
Java Server Pages. 2 Servlets The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little.
Apr 3, 2013 JSP Java Server Pages. 2 A “Hello World” servlet (from the Tomcat installation documentation) public class HelloServlet extends HttpServlet.
Enterprise JavaBeans™ Trademark of Sun Microsystems, Inc.
Bayu Priyambadha, S.Kom. Static content  Web Server delivers contents of a file (html) 1. Browser sends request to Web Server 3. Web Server sends HTML.
14 Copyright © 2004, Oracle. All rights reserved. Achieving State Management in the Business Tier.
1 Web Programming with Servlets & JSPs WEB APPLICATIONS – AN OVERVIEW.
Speaker Name Speaker Title Speaker Title Oracle Corporation Olivier Le Diouris Principal Product Manager Oracle Corporation Building Servlet and JSP Applications.
Enterprise Java Beans Overview
Java Servlets By: Tejashri Udavant..
Java Server Pages.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Servlets Servlets are modules that extend the functionality of a “java-enabled” web-server They normally generate HTML code and web content dynamically.
Java Chapter 7 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

第十四章 J2EE 介绍 1.Java 2 Platform Enterprise Edition (J2EE) 1.3 API 2.J2EE Architecture 3.J2EE Application Development Lifecycle 4.JSP 5.Servlet 6.JavaBean 7.EJB

1 、 J2EE 1.3 APIs and Technologies

2 、 N-tier J2EE Architecture

2 、 Application distribute multi-tier

2 、 J2EE Containers & Components

3 、 J2EE Application Development Lifecycle Write and compile component code Servlet, JSP, EJB Write deployment descriptors for components Assemble components into ready-to- deployable package Deploy the package on a server

4 、 JSP JSP (Java Server Pages) is an alternate way of creating servlets JSP is written as ordinary HTML, with a little Java mixed in The Java is enclosed in special tags, such as The HTML is known as the template text JSP files must have the extension.jsp or other client sees only the resultant HTML, as usual JSP is translated into a Java servlet, which is then compiled Servlets are run in the usual way

JSP scripting elements There is more than one type of JSP “ tag, ” depending on what you want done with the Java The expression is evaluated and the result is inserted into the HTML page The code is inserted into the servlet's service method This construction is called a scriptlet The declarations are inserted into the servlet class, not into a method

Example JSP Hello! The time is now Notes: The tag is used, because we are computing a value and inserting it into the HTML The fully qualified name ( java.util.Date ) is used, instead of the short name ( Date ), because we haven ’ t yet talked about how to do import declarations

Example Hello World Example Hello World Example Hello !

Page is in the proj web application: tomcat_home/webapps/proj/HelloWorld.jsp Invoked with URL: : /proj/HelloWorld.jsp?name=snoopy

Invoked with URL (no parameter): : /proj/HelloWorld.jsp

Variables You can declare your own variables, as usual JSP provides several predefined variables request : The HttpServletRequest parameter response : The HttpServletResponse parameter session : The HttpSession associated with the request, or null if there is none out : A JspWriter (like a PrintWriter) used to send output to the client Example: Your hostname:

What does a JSP-Enabled Server do? receives a request for a.jsp page parses it converts it to a Servlet (JspPage) with your code inside the _jspService() method runs it

Translation of JSP to Servlet Two phases: Page translation: JSP is translated to a Servlet. Happens the first time the JSP is accessed Request time: When page is requested, Servlet runs No interpretation of JSP at request time!

Design Stategy Do not put lengthy code in JSP page Do put lengthy code in a Java class and call it from the JSP page Why? Easier for Development (written separately) Debugging (find errors when compiling) Testing Code Reuse

5 、 Servlet The purpose of a servlet is to create a Web page in response to a client request Servlets are written in Java, with a little HTML mixed in The HTML is enclosed in out.println( ) statements

A “Hello World” servlet public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = " \n";out.println(docType + " \n" + " Hello \n" + "<BODY BGCOLOR= \"#FDF5E6\">\n" + " Hello World \n" + " "); }} This is mostly Java with a little HTML mixed in

// HTTPGetServlet.java: Creating and sending a page //to the client import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class HTTPGetServlet extends HttpServlet { public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException { PrintWriter output; response.setContentType( "text/html" ); // content type output = response.getWriter(); // get writer

// create and send HTML page to client StringBuffer buf = new StringBuffer(); buf.append( " \n" ); buf.append( "A Simple Servlet Example\n" ); buf.append( " \n" ); buf.append( " Welcome to Servlets! \n" ); buf.append( " " ); output.println( buf.toString() ); output.close(); // close PrintWriter stream }}

Servlet HTTP GET Example <FORM ACTION=" servlet/HTTPGetServlet" METHOD="GET"> Click the button to have the servlet send an HTML document

Relationships In servlets: HTML code is printed from java code In JSP pages: Java code is embedded in HTML code Java HTML Java HTML

6 、 JavaBean // LogoAnimator.java: Animation bean package jhtp3beans; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import javax.swing.*; public class LogoAnimator extends JPanel implements ActionListener, Serializable { protected ImageIcon images[]; protected int totalImages = 30, currentImage = 0, animationDelay = 50;

protected Timer animationTimer; public LogoAnimator() { setSize( getPreferredSize() ); images = new ImageIcon[ totalImages ]; URL url; for ( int i = 0; i < images.length; ++i ) { url = getClass().getResource( "deitel" + i + ".gif" ); images[ i ] = new ImageIcon( url ); } startAnimation(); }

public void paintComponent( Graphics g ) { super.paintComponent( g ); if ( images[ currentImage ].getImageLoadStatus() == MediaTracker.COMPLETE ) { g.setColor( getBackground() ); g.drawRect( 0, 0, getSize().width, getSize().height ); images[ currentImage ].paintIcon( this, g, 0, 0 ); currentImage = ( currentImage + 1 ) % totalImages; }

public void actionPerformed( ActionEvent e ) { repaint(); } public void startAnimation() {if ( animationTimer == null ) { currentImage = 0; animationTimer = new Timer( animationDelay, this ); animationTimer.start(); } else if ( ! animationTimer.isRunning() ) animationTimer.restart(); } public void stopAnimation() { animationTimer.stop(); } public Dimension getMinimumSize() { return getPreferredSize(); }

public Dimension getPreferredSize() { return new Dimension( 160, 80 ); } public static void main( String args[] ) { LogoAnimator anim = new LogoAnimator(); JFrame app = new JFrame( "Animator test" ); app.getContentPane().add( anim, BorderLayout.CENTER ); app.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); app.setSize( anim.getPreferredSize().width + 10, anim.getPreferredSize().height + 30 ); app.show(); } }

7 、 EJB Architecture

Exemple // Definition of the EJB Remote Interface package com.titan.cabin; import java.rmi.RemoteException; public interface Cabin extends javax.ejb.EJBObject { public String getName() throws RemoteException; public void setName(String str) throws RemoteException; public int getDeckLevel() throws RemoteException; public void setDeckLevel(int level) throws RemoteException; public int getShip() throws RemoteException; public void setShip(int sp) throws RemoteException; public int getBedCount() throws RemoteException; public void setBedCount(int bc) throws RemoteException; }

//Interface CabinHome package com.titan.cabin; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.FinderException; public interface CabinHome extends javax.ejb.EJBHome { public Cabin create (int id) throws CreateException, RemoteException; public Cabin findByPrimaryKey (CabinPK pk) throws FinderException, RemoteException; }

//Class CabinBean (a implement) package com.titan.cabin; import javax.ejb.EntityContext; public class CabinBean implements javax.ejb.EntityBean { public int id, deckLevel, ship, bedCount; public String name; public CabinPK ejbCreate(int id){this.id = id; return null; } public void ejbPostCreate(int id){// Do nothing. Required.} public String getName(){ return name; } public void setName(String str){ name = str; } public int getShip(){ return ship; } public void setShip(int sp) { ship = sp; } public int getBedCount(){ return bedCount; } public void setBedCount(int bc){ bedCount = bc; } public int getDeckLevel(){ return deckLevel;} public void setDeckLevel(int level ){ deckLevel = level; }

public void setEntityContext(EntityContext ctx){ // Not implemented. } public void unsetEntityContext(){ // Not implemented. } public void ejbActivate(){ // Not implemented. } public void ejbPassivate(){ // Not implemented. } public void ejbLoad(){ // Not implemented. } public void ejbStore(){ // Not implemented. } public void ejbRemove(){ // Not implemented. }

//Class Primary Key package com.titan.cabin; public class CabinPK implements java.io.Serializable { public int id; public int hashCode( ){ return id; } public boolean equals(Object obj){ if(obj instanceof CabinPK){ return (id == ((CabinPK)obj).id); } return false; } public String toString(){ return String.valueOf(id); }

//(Deployment Descriptor) This Cabin enterprise bean entity represents a cabin on a cruise ship. CabinBean com.titan.cabin.CabinHome com.titan.cabin.Cabin com.titan.cabin.CabinBean

Container com.titan.cabin.CabinPK False id name deckLevel ship bedCount This role represents everyone who is allowed full access to the cabin bean.

everyone everyone CabinBean * CabinBean * Required

//Exempl of Client public class Client_1 { public static void main(String [] args){ try { Context jndiContext = getInitialContext(); Object obj = jndiContext.lookup("java:env/ejb/CabinHome"); CabinHome home = (CabinHome) javax.rmi.PortableRemoteObject.narrow(obj, CabinHome.class); Cabin cabin_1 = home.create(1); System.out.println("created it!"); cabin_1.setName("Master Suite"); cabin_1.setDeckLevel(1); cabin_1.setShip(1); cabin_1.setBedCount(3); CabinPK pk = new CabinPK(); pk.id = 1; System.out.println("keyed it! ="+ pk);

Cabin cabin_2 = home.findByPrimaryKey(pk); System.out.println("found by key! ="+ cabin_2); System.out.println(cabin_2.getName()); System.out.println(cabin_2.getDeckLevel()); System.out.println(cabin_2.getShip()); System.out.println(cabin_2.getBedCount()); } catch (java.rmi.RemoteException re) {re.printStackTrace();} catch (javax.naming.NamingException ne) {ne.printStackTrace();} catch (javax.ejb.CreateException ce) {ce.printStackTrace();} catch (javax.ejb.FinderException fe) {fe.printStackTrace();} }

//Bean of Session:Travel Agent public interface TravelAgent extends javax.ejb.EJBObject { public void setCruiseID(int cruise) throws RemoteException, FinderException; public int getCruiseID() throws RemoteException, IncompleteConversationalState; public void setCabinID(int cabin) throws RemoteException, FinderException; public int getCabinID() throws RemoteException, IncompleteConversationalState; public int getCustomerID( ) throws RemoteException, IncompleteConversationalState; public Ticket bookPassage(CreditCard card, double price) throws RemoteException,IncompleteConversationalState; public String [] listAvailableCabins(int bedCount) throws RemoteException,IncompleteConversationalState; }