Comp2513 JavaBeans, EJB and J2EE by czz18476



JavaBeans, EJB and J2EE

   Daniel L. Silver, Ph.D.
   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

2001                  Daniel L. Silver           2
       The JSP Operational Model
 1.    HTTP server receives request for .jsp file
 2.    The .jsp file is located (can be in any directory)
 3.    Tomcat is called and passed the .jsp file
 4.    Tomcat invokes a special servlet called pageCompile
       (the JSP engine, comes as part of JSP distribution)
 5.    pageCompile builds a servlet based on JSP code (that
       may include a reference to a JavaBean)
 6.    The servlet .java and .class files are written to disk
       (internal to Tomcat)
 7.    The .class file is processed by the JVM within Tomcat
       like any other servlet
 8.    HTML code is generated by the servlet and returned

2001                       Daniel L. Silver                     3
                Tomcat Java Servlet
                Request Processing
  Browser                                                   6 HelloWorld.class
                1      HTTP             HTML       Tomcat
                                                    App.         7
  Internet             Server                      Server
                                          3        pageCompile 4
                2         ../Store05/WEB-INF/classes/exampleBean/HelloJSP1
   2001                         Daniel L. Silver                        4
        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

2001                Daniel L. Silver            5
                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
 Under JSP – the use of Beans is the preferred method of
  separating static HTML from dynamic Java code and
  business logic

2001                     Daniel L. Silver                      6
              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 />
        <jsp:useBean use_bean_attribute >
               [option scriptlets and tags]
2001                    Daniel L. Silver                7
               JSP useBean Tag
   The basic syntax for referencing a JavaBean
    within a JSP is as follows:
                                        Object name used within
                                        JSP (case sensitive)
          id = "textProvider"
          class = "exampleBean.HelloJSP1"
          scope = “request"                        Name of the object’s
                                                   implementation class
       />                 See next slide

   If an instance of HellpJSP1 does not already
    exist then one is created

2001                        Daniel L. Silver                       8
                 JSP useBean Tag
   Scope          Description

   Page           Object is accessible only by single client from this
                  page until a response is returned

   request        Object is accessible by single client for lifetime of
                  client request (possibly several pages) until a
                  response is returned
   session        Object is accessible by single client from anywhere
                  in the application for lifetime of client session
                  (possibly several requests). The page in which you create the
                  Bean must have a <%@ page %> directive with session=true.

   application    Object is accessible by any client from anywhere
                  in the application for lifetime of the application

2001                           Daniel L. Silver                                   9
         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” />

2001                 Daniel L. Silver              10
             Getting Bean Properties
Other tags allow you to get or set Bean properties (data
  attributes) … getProperty tag:
    JSP code:
      <jsp:getProperty name="textProvider"

    Java Bean (servlet) code:
      public String getTextMessage()
           Date now = new Date();
           return "Hello World! ... It is now " +
         now.toString() + ".";

    2001                         Daniel L. Silver     11
         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;
 2001                 Daniel L. Silver            12
         JSP Bean Tags in Action
   Lets have a look at the useBean and
    getProperty tags in action within
    For JSP source see (use browser’s source view)
   Note that the example demonstrates how a bean
    property value can be obtained using the
    getProperty tag or by an expression:
       <%= textProvider.getTextMessage() %>

2001                   Daniel L. Silver             13
          JSP Bean Tags in Action
Lets have a look at the setProperty tag in action
 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() %>

2001                       Daniel L. Silver                14
          JSP Bean Tags in Action
   Lets have a look at passing parameters from forms
   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() %>

2001                       Daniel L. Silver              15
               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>
    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)                      public void setB(int value)
         {                                                {
            a = value;                                       b = value;
         }                                                }

    2001                            Daniel L. Silver                             16
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

2001                           Daniel L. Silver                     17
       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

2001                         Daniel L. Silver                     18
       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
2001                          Daniel L. Silver                      19
       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)

2001                         Daniel L. Silver                 20
       Reasons for EJB Architecture
   Security
       – There was no set of common APIs for handling
       – 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
2001                      Daniel L. Silver             21
          Hierarchy of EJB Types

          Entity Bean                                    Session Bean
       (persistent object)                              (nonpersistent)

  Container           Bean                        Stateful
   Managed           Managed                                        Stateless
                                                (client session)
  Persistence       Persistence

2001                         Daniel L. Silver                             22
 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

2001                        Daniel L. Silver                   23
   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

2001                      Daniel L. Silver                24
             J2EE Architecture
  Browser                                                      Enterprise
   Client                                                       System

            Web Container                  EJB Container
             Servlets                            EJBs
                         JSPs                       EJBs

            J2EE services                      J2EE services

                   Java 2 Enterprise Edition

                    Java 2 Standard Edition

2001                        Daniel L. Silver                            25
   Conforming vendors:
       –   Jakarta - Tomcat
       –   BEA - Weblogic AS
       –   IBM - WebSphere AS
       –   Sun-AOL alliance - iPlanet AS
       –   Borland – Borland AS
       –   Iona – iPortal AS

2001                       Daniel L. Silver   26
             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
2001                    Daniel L. Silver                27
   To the SUN Java Tutorial on Java

   To the SUN Java Tutorial on JavaBeans

2001                            Daniel L. Silver                 28
       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" />
    2001                 Daniel L. Silver           30
       Database Connectivity via JSP
    Lets have a look at these tags in action via a JSP
     example that returns a request store’s name

    Here is a servlet that does the same thing

    2001                    Daniel L. Silver                 31
               Links to JSP software
   getProperty and method call to a bean that returns the
    current date and time
   JSP form and bean combo that inputs to values A and B
    and calculates C = A + B
 JSP that returns the requested stores name
 Servlet that does the same thing

    2001                      Daniel L. Silver               32

To top