0% found this document useful (0 votes)
9 views59 pages

Java Server Pages-JIT

The document provides a comprehensive overview of Java Server Pages (JSP), including its objectives, elements, lifecycle, advantages, and architecture. It emphasizes the importance of JSP in creating dynamic web content and outlines the use of implicit objects and Java Beans within JSP. Additionally, it contains proprietary information from Infosys Technologies Ltd., indicating that the document is confidential and meant for training purposes only.

Uploaded by

kavithalms24
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views59 pages

Java Server Pages-JIT

The document provides a comprehensive overview of Java Server Pages (JSP), including its objectives, elements, lifecycle, advantages, and architecture. It emphasizes the importance of JSP in creating dynamic web content and outlines the use of implicit objects and Java Beans within JSP. Additionally, it contains proprietary information from Infosys Technologies Ltd., indicating that the document is confidential and meant for training purposes only.

Uploaded by

kavithalms24
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Server Pages

Education , Training & Assessment


“© 2008 Infosys Technologies Ltd. This document contains valuable confidential and proprietary information of Infosys. Such confidential
and proprietary information includes, amongst others, proprietary intellectual property which can be legally protected and
commercialized. Such information is furnished herein for training purposes only. Except with the express prior written permission of
Infosys, this document and the information contained herein may not be published, disclosed, or used for any other purpose.”

Confidential © 2008, Infosys


ER/CORP/CRS/ ERJEEML104 /003
Technologies Ltd.
Confidential Information

• This Document is confidential to Infosys Technologies Limited. This document contains information and data
that Infosys considers confidential and proprietary (“Confidential Information”).

• Confidential Information includes, but is not limited to, the following:


– Corporate and Infrastructure information about Infosys;
– Infosys’ project management and quality processes;
– Project experiences provided included as illustrative case studies.

• Any disclosure of Confidential Information to, or use of it by a third party, will be damaging to Infosys.

• Ownership of all Infosys Confidential Information, no matter in what media it resides, remains with Infosys.

• Confidential information in this document shall not ©


beInfosys Technologies
disclosed, or used – in whole or in part – for
Ltd.
duplicated
any purpose without specific written permission of an authorized representative of Infosys.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential
anytime, anywhere!
Course Objectives
• Introduction to JSP
• Elements of JSP
• Implicit Objects
• Using Java Beans in JSP
• Introduction to Custom Tags
• Introduction to EL and JSTL

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 3
anytime, anywhere!
Introduction To JSP
• It defines the technology to create web pages with
dynamic content using Java as the programming
language.
• It is an extension to Servlet API

© Infosys Technologies Ltd.

We enable you to leverage


4 knowledge
© 2008, Infosys Technologies Ltd. Confidential 4
anytime, anywhere!
A JSP Example
<%-- This is a JSP Comment --%> View Source Output
<HTML> <HTML>
<HEAD> <HEAD>
<TITLE>Long Message <TITLE>Long Message JSP</TITLE>
JSP</TITLE> </HEAD>
</HEAD> <BODY>
<BODY> Hello World
<%[Link]("Hello World");%> </BODY>
</BODY> </HTML>
</HTML> © Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 5
anytime, anywhere!
Servlets Vs JSP
• Servlet
• Bits of HTML embedded in Java code
• Suitable for coding the business layer of an enterprise
application
• Created and maintained by Java programmers
• JSP
• Bits of Java code embedded in HTML
• Suitable for coding the presentation
© Infosys Technologieslayer
Ltd. of an
enterprise application
• Created and maintained by web designers

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 6
anytime, anywhere!
Life Cycle Of JSP
• A jsp file is like an html file with bits of Java code .
• It has an extension as .jsp
• The steps in the life cycle of a jsp page are:
Translation
Compilation
Loading
© Infosys Technologies Ltd.
Instantiation
Initialization
Request Processing
Destruction

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 7
anytime, anywhere!
Life Cycle Of JSP [Contd…]
• The web container translates the jsp page into a Servlet
class and compiles the class. Translation into a servlet
class will be done only when the web container finds the
servlet class is older than the jsp page.
• Then the container
Loads the JSP page's servlet class
Instantiates an instance of the servlet class
Initializes the servlet instance by calling the jspInit
method © Infosys Technologies Ltd.
• Invokes the _jspService method, passing a request and
response object.
• Invokes the jspDestroy method if the container needs to
remove the instance of the servlet class.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 8
anytime, anywhere!
Advantages Of JSP
• The following advantages are available in JSP as well:
Built in session features, multithreading, exception
handling, Database connectivity
• Enables the separation of content generation from
content presentation.
• Web designers can concentrate on how to present the
information.
• Web Application Programmers can concentrate on how
© Infosys Technologies Ltd.
to process/ build the information.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 9
anytime, anywhere!
Architecture Of A JSP Application
• Model 1 Architecture
JSP
Request Database

Browser

Response

• Disadvantages: Beans

Maintainability, scalability and reusability


• Advantages:
© Infosys Technologies Ltd.

Easy to build

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 10
anytime, anywhere!
Architecture Of A JSP Application [Contd…]
• Model 2 Architecture

Servlets
Request

Browser

Database
Response
JSP Beans

• Advantages: © Infosys Technologies Ltd.

Maintainability, scalability and reusability

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 11
anytime, anywhere!
Java Server Pages
Module 2 – Elements of JSP

Confidential © 2008, Infosys


ER/CORP/CRS/EC32/003
Technologies Ltd.
Learning Outcomes
• After completion of the module you will be able to
understand and work with various elements of a JSP
page.

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 13
anytime, anywhere!
Elements Of A JSP Page
• Directives
• Scriptlets
• Actions
• Expressions
• Comments
• Declarations
• Template Text
© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 14
anytime, anywhere!
Directives
• A JSP directive affects the overall structure of the servlet
class. It usually has the following form:
<%@ directive name [attribute name=“value” attribute
name=“value” ........]%>

• The directives in JSP include:


– page directive
– include directive © Infosys Technologies Ltd.

– taglib directive

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 15
anytime, anywhere!
Directives [Contd…]
• Page directive :
<%@ page [attribute name=“value” attribute
name=“value”……..]%>
The various attributes are :
import extends
session info
isErrorPage language
errorPage autoFlush
© Infosys Technologies Ltd.

contentType buffer
isThreadSafe

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 16
anytime, anywhere!
Directives [Contd…]
• Example
<%@page import=“[Link].*”%>
<%@Page import=“[Link].*”%>
or
<%@page import=“[Link].* ,[Link].*”%>
© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 17
anytime, anywhere!
Directives [Contd…]
• Include directive :
<%@include file=“file name” %>
- This directive helps to statically include the contents of
the included resource at that location.
• taglib directive :
<%@taglib uri= “ taglibURI ” prefix= “ tag prefix”%>
- where Uri is the uniform resource identifier and prefix
is a unique string that is used to identify the custom tags
later in the page. © Infosys Technologies Ltd.

Example:
<%@taglib Uri= “WEB- INF/[Link]” prefix= “abb”/ >
< abb: process element> …

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 18
anytime, anywhere!
Scriptlets
• It is a set of java statements enclosed within <% and %>
tags.
• The jsp container moves the contents of the scriptlet into
the _jspService() method of the generated servlet.
• As the Service method is invoked for each request, the
scriptlet code present inside the _jspService method also
gets executed.
© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 19
anytime, anywhere!
Actions
• These are request time processing instructions to the jsp
engine.
• The various action elements in JSP are:
<jsp: include>
<jsp: forward>
<jsp:param>
<jsp:useBean>
<jsp:setProperty> © Infosys Technologies Ltd.
<jsp:getProperty
<jsp:plugin>

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 20
anytime, anywhere!
Expressions
• Expression is a shorthand notation for a scriptlet that
outputs a value in the response stream back to the client .

• When expression is evaluated the result is converted to


string and displayed.

• An expression is written as <%= expression %>


Example: < H1> The value©is <%=
Infosys i %>
Technologies Ltd. < /H1>

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 21
anytime, anywhere!
Comments
There are two ways to include comments:
• <% -- comments --%>
This statement will be ignored by the jsp container when
translating jsp page into servlet class.
• <! -- comments -- !>
As this is the html syntax the jsp container will keep it as
it is in the output html. The browser while parsing html
discards it in the output screen but the source code still
© Infosys Technologies Ltd.
contains it.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 22
anytime, anywhere!
Declarations
• Is a block of java code to define class wide variables,
methods and classes.
• Declarations are initialized when JSP is initialized and
have class scope
• While translating jsp page the jsp container keeps this
code outside the service method. So they become class
level variables or methods.
• A declaration is written as © Infosys Technologies Ltd.
<%! Declaration %>
Example : <%! int i= 4; %>

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 23
anytime, anywhere!
Template Text
This will include all HTML tags and text.
Example:
The total amount is : <B> <=a+b> </B>

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 24
anytime, anywhere!
Java Server Pages
Module 3 – Implicit Objects

Confidential © 2008, Infosys


ER/CORP/CRS/EC32/003
Technologies Ltd.
Learning Outcomes
• After completion of the module you will be able to
understand and work with Implicit Objects.

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 26
anytime, anywhere!
Implicit Objects
• The JSP Container creates these objects to provide an
environment for scriptlets and expressions to operate.
• These objects will be declared by the generated Servlet
class and hence the statements we write in JSP using
these variables will get a meaning once they are pasted
in the Servlet code.

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 27
anytime, anywhere!
Implicit Objects [Contd..]
request
response
pageContext
session
application
out
config
© Infosys Technologies Ltd.
page
exception

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 28
anytime, anywhere!
Implicit Objects [Contd..]
• request
Is an instance of [Link]
This object is passed as parameter to _jspService
method

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 29
anytime, anywhere!
Implicit Objects [Contd..]
• response
- Is instance of the HttpServletResponse class.
- It encapsulates the response that is sent back to the
client
- This object is passed as parameter to _jspService
method.
• pageContext
- Is an instance of [Link]
© Infosys Technologies Ltd. class
- It provides access to several page attributes
- This object is used by servlet engine to manage error
pages, parameters of the forwarding and including pages
also.
We enable you to leverage knowledge
© 2008, Infosys Technologies Ltd. Confidential 30
anytime, anywhere!
Implicit Objects [Contd..]
• session
– is an instance of HttpSession
– is valid only for the session of the requesting client
• application
– is instance of class ServletContext obtained through
getServletConfig().getServletContext().
– The application object contains methods that provide
information about the context© Infosys
in which JSP
Technologies Ltd. is executing.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 31
anytime, anywhere!
Implicit Objects [Contd..]
• out
– is instance of [Link] .
– used to send output to client

• config
– represents Servlet configuration
– is instance of [Link]
© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 32
anytime, anywhere!
Implicit Objects [Contd..]
• page
is instance of [Link]
– it refers to the current servlet instance “this”

• exception
– is instance of [Link]
– refers to exception being generated in the page
during execution. © Infosys Technologies Ltd.

– The following tag is used to forward the user to


[Link] in case of any error
<%@ page errorPage = “[Link]” %>

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 33
anytime, anywhere!
Java Server Pages
Module 4 – Using Java Beans in JSP

Confidential © 2008, Infosys


ER/CORP/CRS/EC32/003
Technologies Ltd.
Learning Outcomes
• After completion of the module you will be able to
understand how to use Java Beans in JSP.

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 35
anytime, anywhere!
Using java Beans in JSP
• Minimizing Java code in a JSP will enable even a web
designer to maintain it
• It provides the flexibility to change the presentation logic
without affecting the data logic in bean component.
• JSP can instantiate a Java Bean using the
<jsp:useBean> tag, set the bean properties using the
<jsp:setProperty> tag and get the bean properties using
the <jsp:getProperty> tag
© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 36
anytime, anywhere!
Using java Beans in JSP [Steps]
• To use java Beans in JSP
– create a Bean class
– place it in the web server
– create the jsp page
– call the bean from the jsp page

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 37
anytime, anywhere!
Java Server Pages
Module 5– Introduction to EL and JSTL

Confidential © 2008, Infosys


ER/CORP/CRS/EC32/003
Technologies Ltd.
Learning Outcomes
• This module will give you a brief idea on EL and JSTL.

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 39
anytime, anywhere!
Introduction to Expression Language
• EL is designed specially for the web application
environment.
• It has become a part of jsp from JSP2.0
• The syntax is:
${expression}

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 40
anytime, anywhere!
Introduction to Expression Language [Contd…]
• Like any language EL can also include Arithmetic,
relational and logical operators.
EX:
${a+b}
${a<b}
• It can also be used to access the attributes of any
objects.
EX: © Infosys Technologies Ltd.

${[Link]}

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 41
anytime, anywhere!
Introduction to Expression Language [Contd…]
• Similar to implicit objects the EL contains pre defined
variables as:
pageContext
pageScope
requestScope
sessionScope
applicationScope
param © Infosys Technologies Ltd.

paramValues
initParam
cookie

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 42
anytime, anywhere!
Introduction to JSTL
• JSTL was introduced to allow JSP programmers to
program using tags rather than Java code.
• JSTL can do nearly everything that regular JSP scriptlet
code can do.
• The primary advantage behind JSTL is readability

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 43
anytime, anywhere!
The JSTL Tag Libraries
• Core Tag Library— It includes looping, expression
evaluation, and basic input and output.

• Formatting/Internationalization Tag Library— Contains


tags that are used to parse data.

• Database Tag Library— Contains tags that can be used


to access SQL databases. © Infosys Technologies Ltd.

• XML Tag Library— Contains tags that can be used to


access XML elements.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 44
anytime, anywhere!
CoreTag Library
• Variable Tag
<c:set>

• Conditional Tag
<c:if>
<c:choose>
<c:when>
© Infosys Technologies Ltd.
<c:otherwise>

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 45
anytime, anywhere!
CoreTag Library
• Iterator Tag
<c:forEach>

• URL Tag:
<c:url>
<c:redirect>

• Miscellaneous Tag © Infosys Technologies Ltd.

<c:out>

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 46
anytime, anywhere!
Formatting/Internationalization Tag Library
• Setting locale
<fmt:setLocale>
<fmt:requestEncoding>
• Messaging
<fmt:bundle>
<fmt:message> with <fmt:param> subtag
<fmt:setBundle>
• Number and Date formatting© Infosys Technologies Ltd.
<fmt:formatNumber>, <fmt:parseNumber>
<fmt:formatDate>, <fmt:parseDate>
<fmt:setTimeZone>, <fmt:timeZone >

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 47
anytime, anywhere!
Database Tag Library

• Querying Database:

<sql:query>

• Updating Database:

<sql:update>

<sql: transaction>
© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 48
anytime, anywhere!
Database Tag Library
• Data Source Tag:

<sql:setDataSource>

• Others:

<sql:param>
© Infosys Technologies Ltd.
<sql:dateParam>

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 49
anytime, anywhere!
XML Tag Library
• Flow control
<x:choose>, <x:when>, <x:if>, <x:otherwise>
• Iteration
<x:forEach>
• General purpose
<x:out>
<x:set>
• Parsing and Transformation© Infosys Technologies Ltd.
<x:parse>
<x:transform> with <x:param> subtags

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 50
anytime, anywhere!
Java Server Pages
Module 6 – Introduction to Custom Tags

Confidential © 2008, Infosys


ER/CORP/CRS/EC32/003
Technologies Ltd.
Learning Outcomes
• After completion of the module you will be able to
understand how to use Custom Tags in JSP.

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 52
anytime, anywhere!
Custom tags
• It helps to maintain reusable code in JSP page
• It speeds up web application development
• It helps in easy maintenance of web application
• When a JSP page containing custom tag is translated
into a Servlet , the tag is converted to operations on an
object called tag handler

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 53
anytime, anywhere!
Custom tags [Contd..]
• A custom tag library consists of one or more Java
Classes called as Tag Handlers and an XML tag library
description file also termed as tag library. The tag library
dictates the new tag names and valid attributes for those
tags.
• A class which has to be a tag handler needs to
implement Tag interface or IterationTag interface
or BodyTag interface or it can also extend TagSupport
class or BodyTagSupport class.© Infosys Technologies Ltd.

• All the classes that support custom tags are present


inside a package called [Link]

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 54
anytime, anywhere!
Custom tags [Contd..]
• JSP technology defines two types of tag handlers:
simple and classic.
• Simple tag handlers can be used only for tags that do
not use scripting elements in attribute values or the tag
body.
• Classic tag handlers must be used if scripting elements
are required.
© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 55
anytime, anywhere!
Simple tag handler
• To create a Simple tag handler:
-Create the Tag Handler Class that extends
SimpleTagSupport
-Implement the doTag() method
-Create the Tag Library Descriptor File
-Create the JSP file

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 56
anytime, anywhere!
Summary
• Elements of JSP
• Implicit Objects
• Using Java Beans in JSP
• Custom Tags in JSP

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 57
anytime, anywhere!
References
• Phil Hanna, The Complete Reference JSP 2.0, TMH
Publication, 2001.
• James L. Weaver, Beginning J2ee1.4 , Shroff
publications, 2004.
• Servlets and Java Server Pages found online at
[Link]

© Infosys Technologies Ltd.

We enable you to leverage knowledge


© 2008, Infosys Technologies Ltd. Confidential 58
anytime, anywhere!
Thank You

Confidential © 2008, Infosys


ER/CORP/CRS/ EC32/003
Technologies Ltd.

You might also like