SlideShare a Scribd company logo
1 of 78
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).
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 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 %>  
Example of JSP scriptlet tagExample of JSP scriptlet tag
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 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 Scriptlet tag vs DeclarationJSP Scriptlet tag vs Declaration
tagtag
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
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
Example of <jsp:include> actionExample of <jsp:include> action
tagtag
<jsp:forward> action tag<jsp:forward> action tag
<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: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
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: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: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.
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.
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.
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.
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

What's hot

Java Servlets
Java ServletsJava Servlets
Java ServletsNitin Pai
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jspAnkit Minocha
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technologyTanmoy Barman
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)Talha Ocakçı
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jspJafar Nesargi
 
Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletFahmi Jafar
 
Java servlets
Java servletsJava servlets
Java servletslopjuan
 
Java Servlets
Java ServletsJava Servlets
Java ServletsEmprovise
 
Core web application development
Core web application developmentCore web application development
Core web application developmentBahaa Farouk
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postvamsi krishna
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsKaml Sah
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server PageVipin Yadav
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programmingKumar
 

What's hot (20)

Java Servlets
Java ServletsJava Servlets
Java Servlets
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
 
java Servlet technology
java Servlet technologyjava Servlet technology
java Servlet technology
 
JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)JAVA EE DEVELOPMENT (JSP and Servlets)
JAVA EE DEVELOPMENT (JSP and Servlets)
 
Chapter 3 servlet & jsp
Chapter 3 servlet & jspChapter 3 servlet & jsp
Chapter 3 servlet & jsp
 
Knowledge Sharing : Java Servlet
Knowledge Sharing : Java ServletKnowledge Sharing : Java Servlet
Knowledge Sharing : Java Servlet
 
Java servlets
Java servletsJava servlets
Java servlets
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Core web application development
Core web application developmentCore web application development
Core web application development
 
Jsp sasidhar
Jsp sasidharJsp sasidhar
Jsp sasidhar
 
Java Servlets & JSP
Java Servlets & JSPJava Servlets & JSP
Java Servlets & JSP
 
JAVA Servlets
JAVA ServletsJAVA Servlets
JAVA Servlets
 
Servlet
Servlet Servlet
Servlet
 
Java servlets
Java servletsJava servlets
Java servlets
 
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,postServletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
Servlets
ServletsServlets
Servlets
 
Java Servlet
Java Servlet Java Servlet
Java Servlet
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
java servlet and servlet programming
java servlet and servlet programmingjava servlet and servlet programming
java servlet and servlet programming
 

Similar to JSP Basic Syntax and Tags

JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESYoga Raja
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGESKalpana T
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)Manisha Keim
 
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Ayes Chinmay
 
Jsp interview questions by java training center
Jsp interview questions by java training centerJsp interview questions by java training center
Jsp interview questions by java training centerMaheshit Jtc
 
Java Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicJava Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicIMC Institute
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...MathivananP4
 

Similar to JSP Basic Syntax and Tags (20)

Jsp Introduction Tutorial
Jsp Introduction TutorialJsp Introduction Tutorial
Jsp Introduction Tutorial
 
Introduction to JSP.pptx
Introduction to JSP.pptxIntroduction to JSP.pptx
Introduction to JSP.pptx
 
JSP overview
JSP overviewJSP overview
JSP overview
 
Jsp
JspJsp
Jsp
 
JSP.pptx
JSP.pptxJSP.pptx
JSP.pptx
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Unit 4 web technology uptu
Unit 4 web technology uptuUnit 4 web technology uptu
Unit 4 web technology uptu
 
Unit 4 1 web technology uptu
Unit 4 1 web technology uptuUnit 4 1 web technology uptu
Unit 4 1 web technology uptu
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
JAVA SERVER PAGES
JAVA SERVER PAGESJAVA SERVER PAGES
JAVA SERVER PAGES
 
Jsp
JspJsp
Jsp
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
J2EE jsp_01
J2EE jsp_01J2EE jsp_01
J2EE jsp_01
 
JSP Technology I
JSP Technology IJSP Technology I
JSP Technology I
 
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
 
Jsp interview questions by java training center
Jsp interview questions by java training centerJsp interview questions by java training center
Jsp interview questions by java training center
 
Java Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP BasicJava Web Programming [4/9] : JSP Basic
Java Web Programming [4/9] : JSP Basic
 
Spatial approximate string search Doc
Spatial approximate string search DocSpatial approximate string search Doc
Spatial approximate string search Doc
 
JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...JavaScript, often abbreviated as JS, is a programming language and core techn...
JavaScript, often abbreviated as JS, is a programming language and core techn...
 
Java server pages
Java server pagesJava server pages
Java server pages
 

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

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

Machine Learning by Rj
Machine Learning by RjMachine Learning by Rj
Machine Learning by Rj
 
Servlet unit 2
Servlet unit 2 Servlet unit 2
Servlet unit 2
 
Research paper on python by Rj
Research paper on python by RjResearch paper on python by Rj
Research paper on python by Rj
 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in python by Rj
 
Motion capture by Rj
Motion capture by RjMotion capture by Rj
Motion capture by Rj
 
Research paper on big data and hadoop
Research paper on big data and hadoopResearch paper on big data and hadoop
Research paper on big data and hadoop
 
Text processing by Rj
Text processing by RjText processing by Rj
Text processing by Rj
 
Python and data analytics
Python and data analyticsPython and data analytics
Python and data analytics
 
Multithreading by rj
Multithreading by rjMultithreading by rj
Multithreading by rj
 
Django by rj
Django by rjDjango by rj
Django by rj
 
Database programming
Database programmingDatabase programming
Database programming
 
CGI by rj
CGI by rjCGI by rj
CGI by rj
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Seminar on Project Management by Rj
Seminar on Project Management by RjSeminar on Project Management by Rj
Seminar on Project Management by Rj
 
Spring by rj
Spring by rjSpring by rj
Spring by rj
 
Python by Rj
Python by RjPython by Rj
Python by Rj
 
Leadership & Motivation
Leadership & MotivationLeadership & Motivation
Leadership & Motivation
 
Event handling
Event handlingEvent handling
Event handling
 
Layout manager
Layout managerLayout manager
Layout manager
 
Java Applet
Java AppletJava Applet
Java Applet
 

Recently uploaded

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 

Recently uploaded (20)

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 

JSP Basic Syntax and Tags

  • 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).
  • 9.
  • 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.
  • 12.
  • 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 %>  
  • 20.
  • 21. Example of JSP scriptlet tagExample of JSP scriptlet tag
  • 22.
  • 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.
  • 24.
  • 25.
  • 26.
  • 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
  • 28.
  • 29. JSP Scriptlet tag vs DeclarationJSP Scriptlet tag vs Declaration tagtag
  • 30.
  • 31.
  • 32.
  • 33.
  • 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
  • 35.
  • 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
  • 39.
  • 40. Example of <jsp:include> actionExample of <jsp:include> action tagtag
  • 41.
  • 42.
  • 43.
  • 44.
  • 46.
  • 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
  • 49.
  • 50.
  • 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
  • 52.
  • 53.
  • 54.
  • 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
  • 57.
  • 58.
  • 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
  • 60.
  • 61.
  • 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.
  • 68.
  • 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.
  • 70.
  • 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.
  • 72.
  • 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.
  • 74.
  • 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.