SlideShare a Scribd company logo
UNIT - 3UNIT - 3
• JSP Basic Syntax,
•HTMLText, HTML comments,
•TemplateText,
•JSP Comment, JSPExpression,
•JSP Scriptlet,
•JSP Declaration, JSP Directives,
•JSP Action, JSP Expression Language Element,
•CustomTag (Custom Action),
•EscapedTemplateText, Using JSP Scripting
JSP (JavaServer Page)JSP (JavaServer Page)
JSP is a server side technology that does all the
processing at server.
It is used for creating dynamic web applications,
using java as programming language.
JSP’s allow us to separate the dynamic content of webpage
from static presentation content.
A JSP page consists of HTML tags and JSP tags.
HTML tags are used to create static page content and JSP
tags are used to add dynamic content to web pages.
JSP pages are compiled into a Java Servlet by a JSP
translator.
A JSP page consists of HTML tags and JSP tags. The jsp
pages are easier to maintain than servlet because we can
separate designing and development.
It provides some additional features such as Expression
Language, Custom Tag etc.
Basically, any html file can be converted to
JSP file by just changing the file extension
from “.html” to “.jsp”, it would run just fine.
JSP tag is … <% … %>
What differentiates JSP from HTML is the
ability to use java code inside HTML.
In JSP, you can embed Java code in HTML
using JSP tags. for e.g. run the code below,
every time you run this, it would display the
current time.
That is what makes this code dynamic.
Save it with .jsp & put inside a folderSave it with .jsp & put inside a folder
and put that folder in webapp dirand put that folder in webapp dir
<HTML>
<BODY>
Hello BeginnersBook
Readers!
Current time is: <%=
new java.util.Date()
%>
</BODY>
</HTML>
<html>
<body>
<% out.print(2*5); %>
</body>
</html>
Directory structure of JSPDirectory structure of JSP
Jsp vs htmlJsp vs html
HTML JSP
1
Html is given by w3c (World Wide
Web Consortium).
JSP is given by SunMicro System.
2 Html generated static web pages. JSP generated dynamic web pages.
3
It do not allow to place java code
inside Html pages.
JSP allows to place java code inside JSP
pages.
4 It is Client side technology It is a Server side technology.
5
Need Html Interpreter to execute
these code.
Need JSP container to execute jsp
code.
6
It does not allow to place custom
tag or third party tag.
It allow to place custom tag or third
party tag.
JSP SERVLET
1
JSP is a webpage scripting language that
can generate dynamic content.
Servlets are Java programs that are already
compiled which also creates dynamic web
content.
2
JSP run slower compared to Servlet as it
takes compilation time to convert into
Java Servlets.
Servlets run faster compared to JSP.
3
It’s easier to code in JSP than in Java
Servlets.
Its little much code to write here.
4 In MVC, jsp act as a view. In MVC, servlet act as a controller.
5
JSP are generally preferred when there is
not much processing of data required.
servlets are best for use when there is more
processing and manipulation involved.
6
The advantage of JSP programming over
servlets is that we can build custom tags
which can directly call Java beans.
There is no such custom tag facility in
servlets.
7
We can achieve functionality of JSP at
client side by running JavaScript at client
side.
There are no such methods for servlets.
8 JSP is java in html. Servlet is html in java
Life Cycle of JSPLife Cycle of JSP
JSP pages follow these phases:
Translation of JSP Page
Compilation of JSP Page
Classloading (class file is loaded by the
classloader)
Instantiation (Object of the Generated
Servlet is created).
Initialization ( jspInit() method is invoked
by the container).
Reqeust processing ( _jspService() method
is invoked by the container).
Destroy ( jspDestroy() method is invoked
by the container).
Jsp in Servlet by Rj
As depicted in the above diagram, JSP page is
translated into servlet by the help of JSP
translator.
The JSP translator is a part of webserver that
is responsible to translate the JSP page into
servlet.
Afterthat Servlet page is compiled by the
compiler and gets converted into the class
file.
Moreover, all the processes that happens in
servlet is performed on JSP later like
initialization, committing response to the
browser and destroy.
JSP Page Translation:In the first step, the web
container translates the JSP file into a Java source
file that contains a servlet class definition. The web
container validates the correctness of JSP pages and
tag files.
JSP Page Compilation:In the second step, the
web container compiles the servlet source code
into a Java class file.
JSP Page Class Loading:In the third step, the
servlet class byte code is loaded into the web
container’s JVM software using class loader.
JSP Page Servlet Instance:In the fourth step,
the web container creates an instance of the
servlet class.
Jsp in Servlet by Rj
JSP Compilation:JSP Compilation:
When a browser asks for a JSP, the JSP
engine first checks to see whether it
needs to compile the page
The compilation process involves three
steps:
◦ Parsing the JSP.
◦ Turning the JSP into a servlet.
◦ Compiling the servlet.
JSP Initialization:JSP Initialization:
When a container loads a JSP it invokes
the jspInit() method before servicing any
requests
JSP Execution:JSP Execution:
This phase of the JSP life cycle represents
all interactions with requests until the JSP
is destroyed.
JSP Cleanup:JSP Cleanup:
The destruction phase of the JSP life cycle
represents when a JSP is being removed from
use by a container.
The jspDestroy() method is the JSP equivalent
of the destroy method for servlets
JSP Scriptlet tagJSP Scriptlet tag
(Scripting elements)(Scripting elements)
In JSP, java code can be written inside the
jsp page using the scriptlet tag
JSP Scripting elementsJSP Scripting elements
The scripting elements provides the
ability to insert java code inside the jsp.
There are three types of scripting
elements:
1. scriptlet tag <% … %>
2. expression tag <%= … %>
3. declaration tag <%! … %>
JSP scriptlet tagJSP scriptlet tag
A scriptlet tag is used to execute java
source code in JSP.
Syntax is as follows:
◦ <%  java source code %>  
Jsp in Servlet by Rj
Example of JSP scriptlet tagExample of JSP scriptlet tag
Jsp in Servlet by Rj
JSP expression tagJSP expression tag
The code placed within JSP expression
tag is written to the output stream of the
response
So you need not write out.print() to
write data.
It is mainly used to print the values of
variable or method.
Jsp in Servlet by Rj
Jsp in Servlet by Rj
Jsp in Servlet by Rj
JSP Declaration TagJSP Declaration Tag
The JSP declaration tag is used to
declare fields and methods.
The code written inside the jsp
declaration tag is placed outside the
service() method of auto generated
servlet
Jsp in Servlet by Rj
JSP Scriptlet tag vs DeclarationJSP Scriptlet tag vs Declaration
tagtag
Jsp in Servlet by Rj
Jsp in Servlet by Rj
Jsp in Servlet by Rj
Jsp in Servlet by Rj
JSP Action TagsJSP Action Tags
There are many JSP action tags or
elements. Each JSP action tag is used to
perform some specific tasks.
The action tags are used to control the
flow between pages and to use Java Bean
Jsp in Servlet by Rj
Using Standard Actions TagsUsing Standard Actions Tags
1. <jsp:plugin>
2. <jsp:forward>
3. <jsp:include>
<jsp:include> action Tag<jsp:include> action Tag
The jsp:include action tag is used to
include the content of another resource
it may be jsp, html or servlet.
The jsp include action tag includes the
resource at request time so it is better
for dynamic pages because there might
be changes in future.
The jsp:include tag can be used to include
static as well as dynamic pages
Advantage
◦ Code reusability :
◦ We can use a page many times such as
including header and footer pages in all pages.
So it saves a lot of time
Jsp in Servlet by Rj
Example of <jsp:include> actionExample of <jsp:include> action
tagtag
Jsp in Servlet by Rj
Jsp in Servlet by Rj
Jsp in Servlet by Rj
Jsp in Servlet by Rj
<jsp:forward> action tag<jsp:forward> action tag
Jsp in Servlet by Rj
<jsp:plugin> Action tag<jsp:plugin> Action tag
The plugin action is used to insert Java
components into a JSP page.
It determines the type of browser and inserts
the <object> or <embed> tags as needed
The <jsp:plugin> action tag is used to embed
applet in the jsp file.
The <jsp:plugin> action tag downloads plugin at
client side to execute an applet or bean
When a jsp wants to display response in
a graphics format in a browser, then a jsp
page will integrates with an applet
Jsp in Servlet by Rj
Jsp in Servlet by Rj
<jsp:param><jsp:param>
<jsp:param> tag is used to represent
parameter value during jsp forward or include
action this should be the sub tag of
<jsp:forward> or <jsp:include>
When an include or forward element is
invoked, the original request object is provided
to the target page.
If you wish to provide additional data to that
page, you can append parameters to the
request object by using the jsp:param element
Jsp in Servlet by Rj
Jsp in Servlet by Rj
Jsp in Servlet by Rj
JavaBeansJavaBeans
In computing based on the JavaPlatform, 
◦ JavaBeans are classes that encapsulate
many objects into a single object (the bean).
◦ They are serializable, have a zero-argument
constructor, and allow access to properties
using getter and setter methods
 The name "Bean" was given to encompass this
standard, which aims to
create reusable software components for Java
JavaBeans is
◦ an object-oriented programming interface from Sun
Microsystems
◦ that lets you build re-useable applications or program
building blocks called components
◦ that can be deployed in a network on any major
operating system platform
Jsp in Servlet by Rj
Jsp in Servlet by Rj
jsp:useBean action tagjsp:useBean action tag
The jsp:useBean action tag is used to
locate or instantiate a bean class.
If bean object of the Bean class is already
created, it doesn't create the bean
depending on the scope.
But if object of bean is not created, it
instantiates the bean
Jsp in Servlet by Rj
Jsp in Servlet by Rj
jsp:setPropertyjsp:setProperty
and jsp:getProperty action tagsand jsp:getProperty action tags
The jsp:setProperty action tag sets a
property value or values in a bean using
the setter method
The jsp:getProperty action tag returns
the value of the property.
<jsp:setProperty name="bean" property="u
sername" value=“abc" />  
Jsp expression language elementJsp expression language element
JSP Expression Language (EL) makes it
possible to easily access application data
stored in JavaBeans components.
JSP EL allows you to create expressions
both (a) arithmetic and (b) logical.
Within a JSP EL expression, you can use
integers, floating point numbers,
strings, the built-in constants true and
false for boolean values, and null.
Syntax for Expression LanguageSyntax for Expression Language
(EL)(EL)
${ expression }  
Here, expr specifies the expression
itself.
The most common operators in JSP EL
are . and [].
These two operators allow you to access
various attributes of Java Beans and built-
in JSP objects.
Custom Tag (Custom Action)
 Custom tags are user-defined tags. They reduces the
possibility of scriptlet tag and separates the business logic
from the JSP page.
 The same business logic can be used many times by the use of
custom tag.
 Advantages of Custom Tags
 Eliminates the need of scriptlet tag The custom tags
eliminates the need of scriptlet tag which is considered bad
programming approach in JSP.
 Separation of business logic from JSP The custom tags
separate the the business logic from the JSP page so that it
may be easy to maintain.
 Re-usability The custom tags makes the possibility to reuse
the same business logic again and again.
To create a custom tag we need three
things:
1) Tag handler class: In this class we
specify what our custom tag will do when
it is used in a JSP page.
2) TLD file: Tag descriptor file where
we will specify our tag name, tag handler
class and tag attributes.
3) JSP page: A JSP page where we will
be using our custom tag.
Jsp in Servlet by Rj
Tag handler class:
A tag handler class should implement
Tag/IterationTag/ BodyTag interface or it
can also extend
TagSupport/BodyTagSupport/SimpleTagSu
pport class.
All the classes that support custom tags are
present inside javax.servlet.jsp.tagext.
In the below we are extending the class
SimpleTagSupport.
Jsp in Servlet by Rj
TLD File
This file should present at the location: Project
Name/WebContent/WEB-INF/ and it
should have a .tld extension.
Note:
<name> tag: custom tag name.
In this example we have given it as MyMsg
<tag-class> tag: Fully qualified class name. Our
tag handler class Details.java is in package
beginnersbook.com
so we have given the value as
beginnersbook.com.Details.
Jsp in Servlet by Rj
Using custom tag in JSP
 Above we have created a custom tag named MyMsg. Here
we will be using it.
 Note: taglib directive should have the TLD file path in uri
field.
 Above we have created the message.tld file so we have
given the path of that file.
 Choose any prefix and specify it in taglib directive’s prefix
field.
 Here we have specified it as myprefix.
 Custom tag is called like this: <prefix:tagName/>.
 Our prefix is myprefix and tag name is MyMsg so we have
called it as <myprefix:MyMsg/> in the below JSP page.
Jsp in Servlet by Rj
Predefined Variables in JSPPredefined Variables in JSP
 These variables can be used with servlets and Java beans. Following are
the list of predefined variables.
 request:
This variable specifies the data included in a http request. This variable
takes value from the clients' browser to pass it over to the
server.reponse:
This variable specifies the data included in the http response. It is used
with cookies and also in http headers.
 out:
This variable specifies the output stream otherwise known as
printwriter in a page context.
 session:
This variable specifies the data associated with httpsession object with
a specific session of a user. The main purpose of this object is to use
the session information to maintain multiple page requests.
application:
This variable is used to share data with all
application pages.
Config:
This variable refers the java servlet configuration
value.
pagecontext:
This variable is used to store the environment for
the page like page attributes, access to the
request, response and session objects.
page:
This variable is used to store the instance of the
generated java servlet.
Scope of JSP ObjectsScope of JSP Objects
 The availability of a JSP object for use from a particular place of the
application is defined as the scope of that JSP object.
 Every object created in a JSP page will have a scope. Object scope in
JSP is segregated into four parts and they are page, request, session and
application.
 page
‘page’ scope means, the JSP object can be accessed only from within
the same page where it was created.
 The default scope for JSP objects created using <jsp:useBean> tag is
page.
 JSP implicit objects out, exception, response, pageContext, config and
page have ‘page’ scope.
 request
A JSP object created using the ‘request’ scope can be accessed from
any pages that serves that request.
 More than one page can serve a single request. The JSP object will be
bound to the request object.
 Implicit object request has the ‘request’ scope.
 session
‘session’ scope means, the JSP object is accessible from pages that
belong to the same session from where it was created.
 The JSP object that is created using the session scope is bound to
the session object.
 Implicit object session has the ‘session’ scope.
 application
A JSP object created using the ‘application’ scope can be accessed
from any pages across the application.
 The JSP object is bound to the application object.
 Implicit object application has the ‘application’ scope.

More Related Content

PPTX
Hibernate ppt
PDF
Problem Characteristics in Artificial Intelligence
PDF
Java 8 Lambda Expressions
PPT
Abstract class in java
PPTX
Java/Servlet/JSP/JDBC
PPT
Exception Handling in JAVA
PPSX
Frequent itemset mining methods
PPTX
Fuzzy set
Hibernate ppt
Problem Characteristics in Artificial Intelligence
Java 8 Lambda Expressions
Abstract class in java
Java/Servlet/JSP/JDBC
Exception Handling in JAVA
Frequent itemset mining methods
Fuzzy set

What's hot (20)

PPT
Applet life cycle
PPTX
Java RMI
PDF
Java 8 Stream API. A different way to process collections.
PDF
Java exception handling ppt
PPTX
Access specifiers(modifiers) in java
PPTX
Dempster shafer theory
PPTX
PDF
Polymorphism In Java
PPSX
Data Types & Variables in JAVA
PDF
Java Thread Synchronization
PPTX
GUI programming
PPTX
Polymorphism in java
PPTX
Introduction to EJB
PPTX
java interface and packages
PPT
Chapter 02 php basic syntax
PDF
Java I/o streams
PPTX
Java Strings
PPTX
for loop in java
PPTX
Chapter 3 servlet & jsp
PPSX
JDBC: java DataBase connectivity
Applet life cycle
Java RMI
Java 8 Stream API. A different way to process collections.
Java exception handling ppt
Access specifiers(modifiers) in java
Dempster shafer theory
Polymorphism In Java
Data Types & Variables in JAVA
Java Thread Synchronization
GUI programming
Polymorphism in java
Introduction to EJB
java interface and packages
Chapter 02 php basic syntax
Java I/o streams
Java Strings
for loop in java
Chapter 3 servlet & jsp
JDBC: java DataBase connectivity
Ad

Similar to Jsp in Servlet by Rj (20)

PPTX
Module 3.pptx.............................
DOCX
Java server pages
PPTX
Jsp basic
DOCX
Unit 4 1 web technology uptu
DOCX
Unit 4 web technology uptu
PPTX
JAVA SERVER PAGES
PPTX
PPTX
JSP.pptx
PPTX
JSP - Java Server Page
PPTX
Web programming-Introduction to JSP.pptx
PPTX
Jsp Introduction Tutorial
PPTX
SCWCD : Java server pages CHAP : 9
PPTX
Java Server Pages(jsp)
PDF
PPTX
JSP- JAVA SERVER PAGES
PPT
JSP 1.pptdfdfdfdsfdsfdsfdsfdsgdgdgdgdgdd
PPTX
WT Unit-Vuufvmjn dissimilating Dunkirk k
PPTX
JavaScript, often abbreviated as JS, is a programming language and core techn...
PPTX
Introduction to JSP.pptx
PPT
Session 5 : intro to jsp - Giáo trình Bách Khoa Aptech
Module 3.pptx.............................
Java server pages
Jsp basic
Unit 4 1 web technology uptu
Unit 4 web technology uptu
JAVA SERVER PAGES
JSP.pptx
JSP - Java Server Page
Web programming-Introduction to JSP.pptx
Jsp Introduction Tutorial
SCWCD : Java server pages CHAP : 9
Java Server Pages(jsp)
JSP- JAVA SERVER PAGES
JSP 1.pptdfdfdfdsfdsfdsfdsfdsgdgdgdgdgdd
WT Unit-Vuufvmjn dissimilating Dunkirk k
JavaScript, often abbreviated as JS, is a programming language and core techn...
Introduction to JSP.pptx
Session 5 : intro to jsp - Giáo trình Bách Khoa Aptech
Ad

More from Shree M.L.Kakadiya MCA mahila college, Amreli (20)

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPT
Teaching material agriculture food technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
cuic standard and advanced reporting.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Cloud computing and distributed systems.
PDF
KodekX | Application Modernization Development
Approach and Philosophy of On baking technology
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Advanced methodologies resolving dimensionality complications for autism neur...
Digital-Transformation-Roadmap-for-Companies.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
NewMind AI Weekly Chronicles - August'25 Week I
Teaching material agriculture food technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Spectral efficient network and resource selection model in 5G networks
Diabetes mellitus diagnosis method based random forest with bat algorithm
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
cuic standard and advanced reporting.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Cloud computing and distributed systems.
KodekX | Application Modernization Development

Jsp in Servlet by Rj

  • 1. UNIT - 3UNIT - 3 • JSP Basic Syntax, •HTMLText, HTML comments, •TemplateText, •JSP Comment, JSPExpression, •JSP Scriptlet, •JSP Declaration, JSP Directives, •JSP Action, JSP Expression Language Element, •CustomTag (Custom Action), •EscapedTemplateText, Using JSP Scripting
  • 2. JSP (JavaServer Page)JSP (JavaServer Page) JSP is a server side technology that does all the processing at server. It is used for creating dynamic web applications, using java as programming language. JSP’s allow us to separate the dynamic content of webpage from static presentation content. A JSP page consists of HTML tags and JSP tags. HTML tags are used to create static page content and JSP tags are used to add dynamic content to web pages. JSP pages are compiled into a Java Servlet by a JSP translator. A JSP page consists of HTML tags and JSP tags. The jsp pages are easier to maintain than servlet because we can separate designing and development. It provides some additional features such as Expression Language, Custom Tag etc.
  • 3. Basically, any html file can be converted to JSP file by just changing the file extension from “.html” to “.jsp”, it would run just fine. JSP tag is … <% … %> What differentiates JSP from HTML is the ability to use java code inside HTML. In JSP, you can embed Java code in HTML using JSP tags. for e.g. run the code below, every time you run this, it would display the current time. That is what makes this code dynamic.
  • 4. Save it with .jsp & put inside a folderSave it with .jsp & put inside a folder and put that folder in webapp dirand put that folder in webapp dir <HTML> <BODY> Hello BeginnersBook Readers! Current time is: <%= new java.util.Date() %> </BODY> </HTML> <html> <body> <% out.print(2*5); %> </body> </html>
  • 5. Directory structure of JSPDirectory structure of JSP
  • 6. Jsp vs htmlJsp vs html HTML JSP 1 Html is given by w3c (World Wide Web Consortium). JSP is given by SunMicro System. 2 Html generated static web pages. JSP generated dynamic web pages. 3 It do not allow to place java code inside Html pages. JSP allows to place java code inside JSP pages. 4 It is Client side technology It is a Server side technology. 5 Need Html Interpreter to execute these code. Need JSP container to execute jsp code. 6 It does not allow to place custom tag or third party tag. It allow to place custom tag or third party tag.
  • 7. JSP SERVLET 1 JSP is a webpage scripting language that can generate dynamic content. Servlets are Java programs that are already compiled which also creates dynamic web content. 2 JSP run slower compared to Servlet as it takes compilation time to convert into Java Servlets. Servlets run faster compared to JSP. 3 It’s easier to code in JSP than in Java Servlets. Its little much code to write here. 4 In MVC, jsp act as a view. In MVC, servlet act as a controller. 5 JSP are generally preferred when there is not much processing of data required. servlets are best for use when there is more processing and manipulation involved. 6 The advantage of JSP programming over servlets is that we can build custom tags which can directly call Java beans. There is no such custom tag facility in servlets. 7 We can achieve functionality of JSP at client side by running JavaScript at client side. There are no such methods for servlets. 8 JSP is java in html. Servlet is html in java
  • 8. Life Cycle of JSPLife Cycle of JSP JSP pages follow these phases: Translation of JSP Page Compilation of JSP Page Classloading (class file is loaded by the classloader) Instantiation (Object of the Generated Servlet is created). Initialization ( jspInit() method is invoked by the container). Reqeust processing ( _jspService() method is invoked by the container). Destroy ( jspDestroy() method is invoked by the container).
  • 10. As depicted in the above diagram, JSP page is translated into servlet by the help of JSP translator. The JSP translator is a part of webserver that is responsible to translate the JSP page into servlet. Afterthat Servlet page is compiled by the compiler and gets converted into the class file. Moreover, all the processes that happens in servlet is performed on JSP later like initialization, committing response to the browser and destroy.
  • 11. JSP Page Translation:In the first step, the web container translates the JSP file into a Java source file that contains a servlet class definition. The web container validates the correctness of JSP pages and tag files. JSP Page Compilation:In the second step, the web container compiles the servlet source code into a Java class file. JSP Page Class Loading:In the third step, the servlet class byte code is loaded into the web container’s JVM software using class loader. JSP Page Servlet Instance:In the fourth step, the web container creates an instance of the servlet class.
  • 13. JSP Compilation:JSP Compilation: When a browser asks for a JSP, the JSP engine first checks to see whether it needs to compile the page The compilation process involves three steps: ◦ Parsing the JSP. ◦ Turning the JSP into a servlet. ◦ Compiling the servlet.
  • 14. JSP Initialization:JSP Initialization: When a container loads a JSP it invokes the jspInit() method before servicing any requests
  • 15. JSP Execution:JSP Execution: This phase of the JSP life cycle represents all interactions with requests until the JSP is destroyed.
  • 16. JSP Cleanup:JSP Cleanup: The destruction phase of the JSP life cycle represents when a JSP is being removed from use by a container. The jspDestroy() method is the JSP equivalent of the destroy method for servlets
  • 17. JSP Scriptlet tagJSP Scriptlet tag (Scripting elements)(Scripting elements) In JSP, java code can be written inside the jsp page using the scriptlet tag
  • 18. JSP Scripting elementsJSP Scripting elements The scripting elements provides the ability to insert java code inside the jsp. There are three types of scripting elements: 1. scriptlet tag <% … %> 2. expression tag <%= … %> 3. declaration tag <%! … %>
  • 19. JSP scriptlet tagJSP scriptlet tag A scriptlet tag is used to execute java source code in JSP. Syntax is as follows: ◦ <%  java source code %>  
  • 21. Example of JSP scriptlet tagExample of JSP scriptlet tag
  • 23. JSP expression tagJSP expression tag The code placed within JSP expression tag is written to the output stream of the response So you need not write out.print() to write data. It is mainly used to print the values of variable or method.
  • 27. JSP Declaration TagJSP Declaration Tag The JSP declaration tag is used to declare fields and methods. The code written inside the jsp declaration tag is placed outside the service() method of auto generated servlet
  • 29. JSP Scriptlet tag vs DeclarationJSP Scriptlet tag vs Declaration tagtag
  • 34. JSP Action TagsJSP Action Tags There are many JSP action tags or elements. Each JSP action tag is used to perform some specific tasks. The action tags are used to control the flow between pages and to use Java Bean
  • 36. Using Standard Actions TagsUsing Standard Actions Tags 1. <jsp:plugin> 2. <jsp:forward> 3. <jsp:include>
  • 37. <jsp:include> action Tag<jsp:include> action Tag The jsp:include action tag is used to include the content of another resource it may be jsp, html or servlet. The jsp include action tag includes the resource at request time so it is better for dynamic pages because there might be changes in future. The jsp:include tag can be used to include static as well as dynamic pages
  • 38. Advantage ◦ Code reusability : ◦ We can use a page many times such as including header and footer pages in all pages. So it saves a lot of time
  • 40. Example of <jsp:include> actionExample of <jsp:include> action tagtag
  • 47. <jsp:plugin> Action tag<jsp:plugin> Action tag The plugin action is used to insert Java components into a JSP page. It determines the type of browser and inserts the <object> or <embed> tags as needed The <jsp:plugin> action tag is used to embed applet in the jsp file. The <jsp:plugin> action tag downloads plugin at client side to execute an applet or bean
  • 48. When a jsp wants to display response in a graphics format in a browser, then a jsp page will integrates with an applet
  • 51. <jsp:param><jsp:param> <jsp:param> tag is used to represent parameter value during jsp forward or include action this should be the sub tag of <jsp:forward> or <jsp:include> When an include or forward element is invoked, the original request object is provided to the target page. If you wish to provide additional data to that page, you can append parameters to the request object by using the jsp:param element
  • 55. JavaBeansJavaBeans In computing based on the JavaPlatform,  ◦ JavaBeans are classes that encapsulate many objects into a single object (the bean). ◦ They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods  The name "Bean" was given to encompass this standard, which aims to create reusable software components for Java
  • 56. JavaBeans is ◦ an object-oriented programming interface from Sun Microsystems ◦ that lets you build re-useable applications or program building blocks called components ◦ that can be deployed in a network on any major operating system platform
  • 59. jsp:useBean action tagjsp:useBean action tag The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of the Bean class is already created, it doesn't create the bean depending on the scope. But if object of bean is not created, it instantiates the bean
  • 62. jsp:setPropertyjsp:setProperty and jsp:getProperty action tagsand jsp:getProperty action tags The jsp:setProperty action tag sets a property value or values in a bean using the setter method The jsp:getProperty action tag returns the value of the property.
  • 64. Jsp expression language elementJsp expression language element JSP Expression Language (EL) makes it possible to easily access application data stored in JavaBeans components. JSP EL allows you to create expressions both (a) arithmetic and (b) logical. Within a JSP EL expression, you can use integers, floating point numbers, strings, the built-in constants true and false for boolean values, and null.
  • 65. Syntax for Expression LanguageSyntax for Expression Language (EL)(EL) ${ expression }   Here, expr specifies the expression itself. The most common operators in JSP EL are . and []. These two operators allow you to access various attributes of Java Beans and built- in JSP objects.
  • 66. Custom Tag (Custom Action)  Custom tags are user-defined tags. They reduces the possibility of scriptlet tag and separates the business logic from the JSP page.  The same business logic can be used many times by the use of custom tag.  Advantages of Custom Tags  Eliminates the need of scriptlet tag The custom tags eliminates the need of scriptlet tag which is considered bad programming approach in JSP.  Separation of business logic from JSP The custom tags separate the the business logic from the JSP page so that it may be easy to maintain.  Re-usability The custom tags makes the possibility to reuse the same business logic again and again.
  • 67. To create a custom tag we need three things: 1) Tag handler class: In this class we specify what our custom tag will do when it is used in a JSP page. 2) TLD file: Tag descriptor file where we will specify our tag name, tag handler class and tag attributes. 3) JSP page: A JSP page where we will be using our custom tag.
  • 69. Tag handler class: A tag handler class should implement Tag/IterationTag/ BodyTag interface or it can also extend TagSupport/BodyTagSupport/SimpleTagSu pport class. All the classes that support custom tags are present inside javax.servlet.jsp.tagext. In the below we are extending the class SimpleTagSupport.
  • 71. TLD File This file should present at the location: Project Name/WebContent/WEB-INF/ and it should have a .tld extension. Note: <name> tag: custom tag name. In this example we have given it as MyMsg <tag-class> tag: Fully qualified class name. Our tag handler class Details.java is in package beginnersbook.com so we have given the value as beginnersbook.com.Details.
  • 73. Using custom tag in JSP  Above we have created a custom tag named MyMsg. Here we will be using it.  Note: taglib directive should have the TLD file path in uri field.  Above we have created the message.tld file so we have given the path of that file.  Choose any prefix and specify it in taglib directive’s prefix field.  Here we have specified it as myprefix.  Custom tag is called like this: <prefix:tagName/>.  Our prefix is myprefix and tag name is MyMsg so we have called it as <myprefix:MyMsg/> in the below JSP page.
  • 75. Predefined Variables in JSPPredefined Variables in JSP  These variables can be used with servlets and Java beans. Following are the list of predefined variables.  request: This variable specifies the data included in a http request. This variable takes value from the clients' browser to pass it over to the server.reponse: This variable specifies the data included in the http response. It is used with cookies and also in http headers.  out: This variable specifies the output stream otherwise known as printwriter in a page context.  session: This variable specifies the data associated with httpsession object with a specific session of a user. The main purpose of this object is to use the session information to maintain multiple page requests.
  • 76. application: This variable is used to share data with all application pages. Config: This variable refers the java servlet configuration value. pagecontext: This variable is used to store the environment for the page like page attributes, access to the request, response and session objects. page: This variable is used to store the instance of the generated java servlet.
  • 77. Scope of JSP ObjectsScope of JSP Objects  The availability of a JSP object for use from a particular place of the application is defined as the scope of that JSP object.  Every object created in a JSP page will have a scope. Object scope in JSP is segregated into four parts and they are page, request, session and application.  page ‘page’ scope means, the JSP object can be accessed only from within the same page where it was created.  The default scope for JSP objects created using <jsp:useBean> tag is page.  JSP implicit objects out, exception, response, pageContext, config and page have ‘page’ scope.  request A JSP object created using the ‘request’ scope can be accessed from any pages that serves that request.  More than one page can serve a single request. The JSP object will be bound to the request object.  Implicit object request has the ‘request’ scope.
  • 78.  session ‘session’ scope means, the JSP object is accessible from pages that belong to the same session from where it was created.  The JSP object that is created using the session scope is bound to the session object.  Implicit object session has the ‘session’ scope.  application A JSP object created using the ‘application’ scope can be accessed from any pages across the application.  The JSP object is bound to the application object.  Implicit object application has the ‘application’ scope.