Introduction to J2EE
(J2EE FT)
Day 2
Agenda
Day 2
– Action Elements
– Directive Elements
– JDBC in JSP
– Introduction to J2EE
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 2
Technologies Ltd Version 1.00
Action Elements
Action elements are tags that affect the runtime behavior of a JSP
Action elements are also known as Standard Actions
Some common standard actions are
–
–
–
–
–
–
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 3
Technologies Ltd Version 1.00
Forwarding a Request
Forwarding is used when two or more JSPs share some work
One JSP can do some processing and forward the request to another JSP for
further processing
This enables modular design
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 4
Technologies Ltd Version 1.00
Forwarding a request
When a JSP forwards the request to another JSP, the request and response
objects are shared
If some data is to be shared between these JSPs, it can be shared as a
request attribute
The methods used for setting and getting the attributes are very similar to
those available for session and application
– void setAttribute(String name, Object value)
– Object getAttribute(String name)
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 5
Technologies Ltd Version 1.00
Standard Actions - jsp:forward
The tag is used to forward a request to another page
The control will be given to the target page
The syntax of the tag is as follows
We can pass parameters to the forwarded page using tag
The syntax of using the jsp:param tag is as follows
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 6
Technologies Ltd Version 1.00
Standard Actions - jsp:forward
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 7
Technologies Ltd Version 1.00
Standard Actions - jsp:include
The tag is used to include the contents of another file in a
JSP
The contents of the included file will be pasted as a part of the JSP
The contents can be static (HTML Page) or dynamic (Another JSP)
The contents of a page can thus be separated into more manageable
elements
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 8
Technologies Ltd Version 1.00
Standard Actions - jsp:include
The inclusion of the page is happening at runtime
So, if the included file is modified, the next request will receive the
modified content
Just as in the case of request forwarding, the request and response
objects are shared here and data can be shared using request
attributes
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 9
Technologies Ltd Version 1.00
Standard Actions - jsp:include
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 10
Technologies Ltd Version 1.00
Standard Actions - jsp:useBean, jsp:setProperty,
jsp:getProperty
Minimizing Java code in a JSP will enable even a web designer to maintain it
To separate presentation from code, we can encapsulate the logic in a
JavaBean
JSP can instantiate a JavaBean using the tag, set the bean
properties using the tag and get the bean properties using
the tag
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 11
Technologies Ltd Version 1.00
The WishBean
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 12
Technologies Ltd Version 1.00
Standard Actions - jsp:useBean
The tag for instantiating the WishBean is as follows
The above tag is equivalent to the following Java code
mypackage.WishBean myWishBean = new mypackage.WishBean();
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 13
Technologies Ltd Version 1.00
Standard Actions - jsp:useBean
The important attributes of tag are
– id
– class
– scope
The id attribute
– Specifies the name of the Bean object
The class attribute
– Specifies the fully qualified name of the Bean class
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 14
Technologies Ltd Version 1.00
Standard Actions - jsp:useBean
The scope attribute
– Specifies the scope of the Bean object as page, request, session or
application
The page scope
– Available only for this request and only in this page
– By default, the scope will be page
The request scope
– Available only for this request
– Available to other forwarded and included JSPs
The session scope
– Available to the current session
The application scope
– Available to any JSP in the same application
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 15
Technologies Ltd Version 1.00
Standard Actions - jsp:setProperty
The tag can be used to set the Bean properties
The
The above tag is equivalent to the following Java code
myWishBean.setWish(“Welcome”);
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 16
Technologies Ltd Version 1.00
Standard Actions - jsp:setProperty
The attributes are
– name
– property
– param
– value
The name attribute
– Specifies the id of the Bean object
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 17
Technologies Ltd Version 1.00
Standard Actions - jsp:setProperty
The property attribute
– Specifies the name of the bean property that is to be set
– If this attribute is *, all the request parameters will be assigned to bean properties
based on matching name
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 18
Technologies Ltd Version 1.00
Standard Actions - jsp:setProperty
The param attribute
– Specifies the name of the request parameter whose value is to be put in to the bean
property
– If this value is not specified, the value of the request parameter whose name is
same as that of the bean property will be assigned to the bean property
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 19
Technologies Ltd Version 1.00
Standard Actions - jsp:setProperty
The value attribute
– Specifies the value to be assigned to the bean property
– A tag cannot have both param and value attributes together
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 20
Technologies Ltd Version 1.00
Standard Actions - jsp:getProperty
The tag can be used to get the value of a bean property
The can be used to get the property wish of the WishBean
as follows
The above tag is equivalent to the following Java code
myWishBean.getWish();
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 21
Technologies Ltd Version 1.00
Standard Actions - jsp:getProperty
The attributes are
– name
– property
The name attribute
– Specifies the id of the Bean object
The property attribute
– Specifies the name of the bean property to get
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 22
Technologies Ltd Version 1.00
Standard Actions - jsp:useBean, jsp:setProperty,
jsp:getProperty
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 23
Technologies Ltd Version 1.00
Standard Actions - jsp:useBean, jsp:setProperty,
jsp:getProperty
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 24
Technologies Ltd Version 1.00
Custom Actions
In JSP, the programmer can create her own customized tags to encapsulate
code from presentation
These tags are categorized as Custom Actions
The syntax of using custom actions is as follows
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 25
Technologies Ltd Version 1.00
Custom Actions
Each custom tag will have an implementation class where the actual
Java code resides
When the JSP Container comes across a custom tag, the code in the
implementation class is executed
However, the code will be hidden from the JSP page
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 26
Technologies Ltd Version 1.00
Directive Elements
Directive elements provide information to the JSP container about the page
JSP can contain three types of directives
– page
– include
– taglib
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 27
Technologies Ltd Version 1.00
The page Directive
The page directive has the following form
Some of the important attributes are
– import
– session
– contentType
– errorPage
– isErrorPage
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 28
Technologies Ltd Version 1.00
The page Directive - import
The import attribute
– Example
– Just like a normal Java program, the Java code embedded in a JSP
page should import all the classes and interfaces used in the code
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 29
Technologies Ltd Version 1.00
The page Directive - session
The session attribute
– Example
– By default, the generated Servlet creates an object called session
– Setting session = “false” prevents the creation of this object
– The implicit object, session, is available only if this value is not set
to false
– The value of this attribute can be set to false if the Servlet is not
tracking the session
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 30
Technologies Ltd Version 1.00
The page Directive - contentType
The contentType attribute
– Example
– The browser can accept many file types - HTML, Plain Text, JPG
Images, XML etc
– To ensure proper display of the file, the file type has to be specified
using a standard called MIME or multipurpose internet mail
extension
– The default value is “text/html”
– Some other examples are “image/gif” and “text/xml”
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 31
Technologies Ltd Version 1.00
The page Directive - errorPage
The errorPage attribute
– Example
– In case of any error, the user will be forwarded to Error.jsp
– The exception object will be set as an attribute in the request object
so that the Error.jsp can also access the exception object
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 32
Technologies Ltd Version 1.00
The page Directive - isErrorPage
The isErrorPage attribute
– Example
– Error pages like Error.jsp in the previous example should contain
this tag
– The presence of this tag creates a new Throwable object called
exception in the generated Servlet
– The exception generated in the original page and passed as an
attribute of the request will be assigned to this
– So, exception is an implicit object that we can use only in error
pages
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 33
Technologies Ltd Version 1.00
The page Directive - errorPage and isErrorPage
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 34
Technologies Ltd Version 1.00
The include Directive
The include directive can be used to include the contents of some other
file in a JSP
The contents of the included file will be pasted as a part of the JSP
The contents can be static (HTML Page) or dynamic (Another JSP)
The contents of a page can thus be separated into more manageable
elements
Unlike jsp:include, include directive acts at compile time and any
change in the included file will NOT be reflected in the including file
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 35
Technologies Ltd Version 1.00
The include Directive
Many dynamic pages contain common static parts in them, mostly header
and footer
The common static parts can be stored as HTML files that can be included in
a JSP
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 36
Technologies Ltd Version 1.00
The taglib Directive
The taglib directive is associated with custom tags
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 37
Technologies Ltd Version 1.00
JDBC
Most of the web applications require a database connection
JSP can make JDBC connections and access a database
Assume a JSP that has to display the names of all employees from the emp
table of Oracle
– In which method will you open the connection?
– In which method will you execute the query?
– In which method will you close the connection?
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 38
Technologies Ltd Version 1.00
JDBC
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 39
Technologies Ltd Version 1.00
Transactions
A simple web application can follow the previous model
In some web applications, we may want to do some atomic
transactions
On success, we want to commit or else rollback
try{
connection.setAutoCommit(false);
//Transactions
connection.commit();
}
catch(Exception exception){
connection.rollback();
}
The model in the previous example will not work here
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 40
Technologies Ltd Version 1.00
JDBC - Transaction
In the above example, for each request the JSP is opening a new Connection
and closing it once the use is over
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 41
Technologies Ltd Version 1.00
JDBC
As JSP is a technology for developing the presentation layer, it is not a good
practice to include a lot of Java code in a JSP
It is better to include all the JDBC code in a separate class and invoke the
methods of that class in the JSP
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 42
Technologies Ltd Version 1.00
Connection Pooling
In the previous examples, we require separate Connection objects for each
client which are created, opened, used and closed
Since opening a connection for each client is time consuming, it is better to
have a pool of open connections from which we can pick a free connection
object for each client
This will increase the performance, as a new Connection object is not created
and opened for each request, and at the same time, a different Connection
object is available for each client
This technique is called Connection Pooling
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 43
Technologies Ltd Version 1.00
J2EE
Characteristics of Enterprise Applications
Enterprise applications solve business problems
This involves safe storage, retrieval and manipulation of business data
They may have multiple interfaces
– Web interface for customers
– GUI application running on the desktop for the employees
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 45
Technologies Ltd Version 1.00
Characteristics of Enterprise Applications
Enterprise Applications may communicate with remote systems
They may have to co-ordinate data in multiple stores
They should follow a set of business rules
If any part of the system fails, the enterprise will start losing money and if the
business grows, the application needs to grow with it
These factors make an enterprise application quite complex
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 46
Technologies Ltd Version 1.00
Components of Enterprise Applications
An enterprise application will be made up of
– Presentation Logic
– Business Logic
– Data Access Logic
– System Services
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 47
Technologies Ltd Version 1.00
Evolution of Enterprise Applications - Single Tier
Dumb Terminal
Dumb Terminal
Mainframe
Dumb Terminal Business logic, Presentation
logic and Data access logic
reside on the single layer
Advantages
– No client side management is required
– Data consistency is easy to achieve
Disadvantages
– Difficult to maintain and reuse
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 48
Technologies Ltd Version 1.00
Evolution of Enterprise Applications - Two Tier
DataBase
Data Layer
Business Logic &
Presentation Logic
Advantages
– DB product independence
Disadvantages
– Difficult to maintain and update
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 49
Technologies Ltd Version 1.00
Evolution of Enterprise Applications - Three Tier
DataBase
Data Layer
Presentation Logic Server
Business Logic
Advantages
– Business logic can change more easily
Disadvantages
– Complexity introduced in the middle tier
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 50
Technologies Ltd Version 1.00
Evolution of Enterprise Applications - n Tier
DataBase
Data Layer
Thin Client
Server Server
Presentation Logic Business Logic
Advantages
– More loosely coupled
– More reusable
– Zero client management
Disadvantages
– Complexity in the middle tier
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 51
Technologies Ltd Version 1.00
Role of Application Servers in Enterprise
Applications
To reduce the complexity in the middle tier, enterprise programmers build their
applications on top of systems called Application Servers
Application Servers provide the system services to address the non functional
requirements like scalability, performance etc and reduces the complexity of
the middle tier
Application developers can concentrate on the functional requirements by
concentrating on the business logic
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 52
Technologies Ltd Version 1.00
Role of Application Servers in Enterprise
Applications
Application Servers typically uses the “Component and Container” model
A Component will be a piece of code developed by the application developer
– Takes care of the functional requirements
A Container is a program that provides an environment for the Component to
run
– Takes care of the non-functional requirements like scalability and performance by
providing the system services
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 53
Technologies Ltd Version 1.00
Application Servers - Proprietary
Proprietary Application Servers provide system services in a well defined but
proprietary manner
– Example - Tuxedo, .NET
The application developers develop programs according to the specification of
the application server
Dependence on a particular vendor is the drawback of this approach
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 54
Technologies Ltd Version 1.00
J2EE - Open Standard for Application Servers
J2EE or Java 2 Enterprise Edition is an open standard
J2EE Application Servers provide system services in a well defined, open,
industry standard
– JBoss
– BEA WebLogic
– IBM WebSphere
The application developers develop programs according to the J2EE
specification and not according to the application server
A J2EE application developed according to J2EE standard can be deployed in
any J2EE Application Server making it vendor independent
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 55
Technologies Ltd Version 1.00
Containers and Components
J2EE uses the Component-Container model
J2EE containers take care of
– Concurrency
– Security
– Availability
– Scalability
– Persistence
– Lifecycle Management
– Management
J2EE Application developers develop components that will take care of
– Presentation
– Business Logic
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 56
Technologies Ltd Version 1.00
J2EE - APIs, Containers and Components
Some important APIs in J2EE are
– JDBC (Already discussed in Java/Advanced Java)
– RMI (Already discussed in Java/Advanced Java)
– JNDI (Java Naming and Directory Interface)
– JMS (Java Messaging Services)
Containers and Components
– Applet (Already discussed in Java/Advanced Java)
– Application (Already discussed in Java/Advanced Java)
– Servlets (Already discussed in this course)
– JSP (Already discussed in this course)
– EJB (Enterprise Java Beans)
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 57
Technologies Ltd Version 1.00
Summary
Action elements like jsp:forward and jsp:include can be used to change the
runtime behavior of a JSP
The jsp:useBean, jsp:getProperty and jsp:setProperty tags can be used to use
JavaBeans in a JSP
The directive elements are used to provide information about the JSP to the
container
Using JDBC, JSP can connect to databases
J2EE is an open standard for developing, deploying and managing enterprise
applications
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 58
Technologies Ltd Version 1.00
Thank You!
ER/CORP/CRS/ED43A/003
Copyright © 2005, Infosys 59
Technologies Ltd Version 1.00