320 likes | 460 Views
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
E N D
Comp2513JavaBeans, 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 Daniel L. Silver
The JSP Operational Model • 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
Tomcat Java ServletRequest Processing http://eagle.acadiau.ca/store05/HelloWorld.jsp Client HelloWorld.java HelloWorld.class Browser 6 8 HTTP Server Tomcat App. Server HTML 1 7 Internet pageCompile 4 3 mod_jserv 5 2 ../Store05/WEB-INF/classes/exampleBean/HelloJSP1 ../Store05/HelloWorld.jsp HelloJSP1.class HelloWorld.jsp Daniel L. Silver
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 Daniel L. Silver
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 self-contained, 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 Daniel L. Silver
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> Daniel L. Silver
JSP useBean Tag • The basic syntax for referencing a JavaBean within a JSP is as follows: <jsp:useBean id = "textProvider" class = "exampleBean.HelloJSP1" scope = “request" /> • If an instance of HellpJSP1 does not already exist then one is created Object name used within JSP (case sensitive) Name of the object’s implementation class See next slide Daniel L. Silver
JSP useBean Tag Daniel L. Silver
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” /> Daniel L. Silver
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() + "."; } Daniel L. Silver
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; } Daniel L. Silver
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() %> Daniel L. Silver
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() %> Daniel L. Silver
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() %> Daniel L. Silver
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 setA(int value) { a = value; } • public void setB(int value) • { • b = value; • } Daniel L. Silver
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 Daniel L. Silver
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 Daniel L. Silver
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 Daniel L. Silver
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) Daniel L. Silver
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 Daniel L. Silver
Hierarchy of EJB Types EJB Entity Bean (persistent object) Session Bean (nonpersistent) Stateful (client session) Stateless Container Managed Persistence Bean Managed Persistence Daniel L. Silver
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 Daniel L. Silver
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 Daniel L. Silver
J2EE Architecture Browser Client Enterprise System Web Container EJB Container Servlets EJBs Servlets EJBs JSPs EJBs JSPs J2EE services J2EE services Java 2 Enterprise Edition Java 2 Standard Edition Daniel L. Silver
J2EE • Conforming vendors: • Jakarta - Tomcat • BEA - Weblogic AS • IBM - WebSphere AS • Sun-AOL alliance - iPlanet AS • Borland – Borland AS • Iona – iPortal AS Daniel L. Silver
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 Daniel L. Silver
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 Daniel L. Silver
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" /> Daniel L. Silver
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 Daniel L. Silver
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 Daniel L. Silver