SlideShare a Scribd company logo
Overview of JSP Technology
Introduction
⚫ It stands for Java Server Pages.
⚫ It is a server side technology.
⚫ It is used for creating web application.
⚫ It is used to create dynamic web content.
⚫ In this JSP tags are used to insert JAVA code into HTML
pages.
⚫ It is an advanced version of Servlet Technology.
⚫ It is a Web based technology helps us to create dynamic
and platform independent web pages.
⚫ In this, Java code can be inserted in HTML/ XML pages or
both.
⚫ JSP is first converted into servlet by JSP container before
processing the client’s request.
Advantageous than Servlet:
• They are easy to maintain.
⚫ No recompilation or redeployment is required.
⚫ JSP has access to entire API of JAVA .
⚫ JSP are extended version of Servlet.
Features of JSP
⚫ Coding in JSP is easy :- As it is just adding JAVA code to
HTML/XML.
⚫ Reduction in the length of Code :- In JSP we use action tags,
custom tags etc.
⚫ Connection to Database is easier :-It is easier to connect
website to database and allows to read or write data easily to
the database.
⚫ Make Interactive websites :- In this we can create dynamic web
pages which helps user to interact in real time environment.
⚫ Portable, Powerful, flexible and easy to maintain :- as these
are browser and server independent.
Architecture of JSP Page
⚫ The web server needs a JSP engine, i.e, a container to process JSP pages.
⚫ The JSP container is responsible for intercepting requests for JSP pages.
⚫ A JSP container works with the Web server to provide the runtime
environment and other services a JSP needs.
⚫ Following diagram shows the position of JSP container and JSP files in a
Web application.
Introduction  - Java Server Programming (JSP)
Understanding the JSP Page Lifecycle
⚫The JSP pages follow these phases:
● Compilation
● Initialization
● Execution
● Cleanup
⚫ jspInit(), _jspService() and jspDestroy() are the life cycle
methods of JSP.
Compilation
⚫ The compilation process involves three steps −
● Parsing the JSP.
● Turning the JSP into a servlet.
● Compiling the servlet.
Initialization
public void jspInit(){
// Initialization code...
}
Execution
void _jspService(HttpServletRequest request,
HttpServletResponse response)
{ // Service handling code...}
Cleanup
public void jspDestroy()
{ // Your cleanup code goes here.}
⚫ <%-- JSP comment --%>
⚫ <HTML> <HEAD>
⚫ <TITLE>MESSAGE</TITLE>
⚫ </HEAD> <BODY>
⚫ <%out.print("Hello, Sample JSP code");%>
⚫ </BODY>
⚫ </HTML>
The above JSP generates the following output:
Hello, Sample JSP code.
⚫ Explanation of above code
1) The line <%–JSP Comment–%> represents the JSP element called JSP Comment, While adding
comments to a JSP page you can use this tag, we will discuss this in detail in coming posts.
Note: JSP Comments must starts with a tag <%– and ends with –%>
⚫ 2) Head, Title and Body tags are HTML tags – They are HTML tags, frequently used for static
web pages. Whatever content they have is delivered to client(Web browser) as such.
⚫ 3) <%out.print(“ Hello, Sample JSP code ”);%> is a JSP element, which is known as Scriptlet.
Scriptlets can contain Java codes. syntax of scriptlet is: <%Executable java code%>. As the
code in Scriptlets is java statement, they must end with a semicolon(;). out.print(“ Hello, Sample
JSP code ”) is a java statement, which prints“ Hello, Sample JSP code”.
⚫ JSP is used for creating dynamic webpages. Dynamic webpages are usually a mix of static &
dynamic content.
⚫ The static content can have text-based formats such as HTML, XML etc and the dynamic
content is generated by JSP tags using java code inside HTML .
Elements of JSP
The JSP elements are used to create JSP page. They are divides
into three categories:
JSP Scripting Elements
JSP Directive Elements
JSP Standard Action Elements
Scripting Elements of JSP
JSP Scripting Elements are used for writing the Java Code inside the JSP page. There
are different types of scripting elements these elements are used for various
purposes.
The scripting elements of JSP are:
The Scriptlet
⚫ A scriptlet can contain any number of JAVA language statements, variable or
method declarations, or expressions that are valid in the page scripting language.
⚫ syntax of Scriptlet −
<% code fragment %>
⚫ You can write the XML equivalent of the above syntax as follows −
<jsp:scriptlet> code fragment </jsp:scriptlet>
<html> <head><title>Hello World</title></head>
<body> Hello World!
<br/>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body> </html>
JSP Declarations
⚫ A declaration declares one or more variables or methods
that you can use in Java code later in the JSP file.
⚫ You must declare the variable or method before you use it in
the JSP file.
⚫ syntax for JSP Declarations −
<%! declaration; [ declaration; ]+ ... %>
You can write the XML equivalent of the above syntax as
follows −
<jsp:declaration> code fragment </jsp:declaration>
example for JSP Declarations −
⚫ <%! int i = 0; %>
⚫ <%! int a, b, c; %>
⚫ <%! Circle a = new Circle(2.0); %>
JSP Expression
A JSP expression element contains a scripting language expression that is
evaluated, converted to a String, and inserted where the expression appears
in the JSP file.
⚫ The expression element can contain any expression that is valid according to
the Java Language Specification but you cannot use a semicolon to end an
expression.
syntax of JSP Expression −
<%= expression %>
You can write the XML equivalent of the above syntax as follows −
<jsp:expression> expression </jsp:expression>
JSP Expression −
<html> <head><title>A Comment Test</title></head>
<body>
<p>Today's date: <%= (new java.util.Date()).toLocaleString()%></p>
</body> </html>
The above code will generate the following result −
Today's date: 11-Sep-2010 21:24:25
JSP Comments
⚫ JSP comment marks text or statements that the JSP container
should ignore. A JSP comment is useful when you want to hide
or "comment out", a part of your JSP page.
syntax of the JSP comments −
<%-- This is JSP comment --%>
example of the JSP Comments −
<html> <head><title>A Comment Test</title></head> <body>
<h2>A Test of Comments</h2>
<%-- This comment will not be visible in the page source --%>
</body> </html>
The above code will generate the following result −
A Test of Comments
Introduction  - Java Server Programming (JSP)
JSP directives
⚫ JSP directives are the messages to JSP container. They
provide global information about an entire JSP page.
⚫ JSP directives are used to give special instruction to a
container for translation of JSP to servlet code.
⚫ In JSP, directive is described in <%@ %> tags.
Syntax of Directive:
<%@ directive attribute=“value" %>
There are three types of directives:
⚫ Page directive
⚫ Include directive
⚫ Taglib directive
JSP Page directive
Syntax of Page directive:
<%@ page attribute="value" %>
⚫ It provides attributes that get applied to entire JSP
page.
⚫ JSP page directive is used to define the properties
applying the JSP page, such as the size of the allocated
buffer, imported packages and classes/interfaces,
defining what type of page it is etc.
⚫ It is used to provide instructions to a container that
pertains to current JSP page.
Attributes of JSP page directive
⚫import
⚫contentType
⚫buffer
⚫Language
⚫errorPage
⚫isErrorPage
Import Attribute
This tells the container what packages/classes are needed to be imported into
the program.
Syntax:
<%@page import = "value"%>
Example :
<%-- JSP code to demonstrate how to use page directive to import a package
--%>
<%@page import = "java.util.Date"%>
<%Date d = new Date();%>
<%=d%>
contentType Attribute
This defines the format of data that is being exchanged between the client and
the server. It does the same thing as the setContentType method in servlet
used to.
Syntax:
<%@page contentType="value“ %>
<%-- JSP code to demonstrate how to use page directive to set the type of
content --%>
<%@page contentType = "text/html" %>
<% = "This is sparta" %>
buffer Attribute
⚫ The buffer attribute of page directive is used to specify
behavior of JSP container with respect to sending the output
to an intermediate buffer, before it is sent to Http response.
⚫ The default size of buffer is 8KB. This means that the JSP
container will send the HTTP response in chunks of 8 KBytes.
⚫ The feature can be disabled by setting the buffer attribute to
none. In this case, JSP container does not perform any
buffering, and sends contents to the client as they are
generated.
<%@page buffer=”none”%>
The buffer attribute of page directive may also be used to
change the buffer size.
The example below sets the buffer size to 4 KBytes.
<%@page buffer=”4kb”%>
language Attribute
⚫The language attribute indicates the
programming language used in scripting the JSP
page.
⚫For example, because you usually use Java as the
scripting language, your language option looks
like this
<%@ page language = "java" %>
errorPage and isErrorPage Attribute
errorPage Attribute
In JSP, the errorPage attribute of page directive is used to specify a web page which will be
displayed as an error page for the current JSP page i.e. if there is an error while executing the
current page).
Syntax
<%@ page errorPage= "path-to-another-jsp-page" %>
isErrorPage Attribute
In JSP, the isErrorPage attribute of page directive is used to specify whether the current page
could be displayed as an error page for another JSP page. We could set the value of isErrorPage
to either true or false.
Syntax
<%@ page isErrorPage= "true|false" %>
isErrorPage = "true“
⚫ When isErrorPage is set to true, the current page is displayed in case an error appears in
another JSP page.
<%@ page isErrorPage= "true" %>
isErrorPage = "false“
⚫ By default, the isErrorPage attribute value is false, hence we don't have to
set isErrorPage to false, in case if we don't want the current page to be displayed as an error
appears for any JSP page.
Include Directive
⚫ The include directive is used to include a file during the
translation phase.
⚫ This directive tells the container to merge the content of other
external files with the current JSP during the translation phase.
⚫ You may code include directives anywhere in your JSP page.
⚫ The include directive is used to include the contents of any
resource it may be jsp file, html file or text file.
⚫ The include directive includes the original content of the included
resource at page translation time (the jsp page is translated only
once so it will be better to include static resource).
⚫ Advantage of Include directive
Code Reusability
Syntax of include directive
<%@ include file="resourceName" %>
Include Directive Example
index.jsp
<html> <head> <title>Main JSP Page</title> </head>
<body>
<%@ include file="file1.jsp" %>
Main JSP Page: Content between two include directives.
<%@ include file="file2.jsp" %>
</body> </html>
file1.jsp
<p align="center">
This is my File1.jsp and I will include it in index.jsp using include directive
</p>
file2.jsp
<p align="center"> This is File2.jsp </p>
Introduction  - Java Server Programming (JSP)
Taglib Directive
⚫ The taglib directive is used to define tag library that the current JSP
page uses.
⚫ A JSP page might include several tag library.
⚫ JavaServer Pages Standard Tag Library (JSTL), is a collection of useful
JSP tags, which provides many commonly used core functionalities.
⚫ It has support for many general, structural tasks such as iteration
and conditionals, readymade tags for manipulating XML
documents, internationalization tags, and for performing SQL
operations.
Syntax of taglib directive is:
<%@ taglib prefix="prefixOfTag" uri="uriOfTagLibrary" %>
The prefix is used to distinguish the custom tag from other libary
custom tag. Prefix is prepended to the custom tag name. Every
custom tag must have a prefix.
⚫ The URI is the unique name for Tag Library.
<%@ taglib uri="http://www.sample.com/mycustomlib" prefix="demotag" %>
<html>
<body>
<demotag:welcome/>
</body>
</html>
JSP Actions
⚫JSP actions use the construct in XML syntax
to control the behavior of the servlet engine.
⚫We can dynamically insert a file, reuse the
beans components, forward user to another
page, etc. through JSP Actions like include
and forward.
⚫Unlike directives, actions are re-evaluated
each time the page is accessed.
Syntax:
<jsp:action_name attribute="value" />
standard Action Tags
There are 11 types of Standard Action
Tags as following:
⚫jsp:include
⚫jsp:forward
jsp:include
⚫ It also used to insert a jsp file into another file, just like include
directive.
⚫ It is added during request processing phase
Syntax of jsp:include
<jsp:include page="page URL" flush="true/false">
<%@ page language="java" contentType="text/html"%>
<html>
<head>
<title>Date JSP</title>
</head>
<body>
<jsp:include page="date.jsp" flush="true" />
</body>
</html>
Date.jsp
<%@ page language="java" contentType="text/html"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<p>
Today's date: <%= {new java.util.Date()).toLocaleString()%>
</p>
</body>
</html>
jsp:forward:
⚫ It is used to forward the request to another jsp or any static page.
⚫ Here the request can be forwarded with no parameters or with
parameters.
Syntax:
<jsp:forward page="value">
Here value represents where the request has to be forwarded.
Action.jsp
<%@ page language="java" contentType="text/html"%>
<html>
<head>
<title>Guru Action JSP1</title>
</head>
<body>
<jsp:forward page="jsp_action.jsp" />
</body>
</html>

More Related Content

Similar to Introduction - Java Server Programming (JSP) (20)

PPTX
JSP AND XML USING JAVA WITH GET AND POST METHODS
bharathiv53
 
PPTX
JSP Directives
ShahDhruv21
 
PPTX
JSP - Java Server Page
Vipin Yadav
 
DOCX
Java server pages
Abhishek Kesharwani
 
PPTX
JAVA SERVER PAGES
Kalpana T
 
PPTX
WT Unit-Vuufvmjn dissimilating Dunkirk k
asta9578
 
PPTX
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Ayes Chinmay
 
PPTX
Web programming-Introduction to JSP.pptx
mcjaya2024
 
PPT
Atul & shubha goswami jsp
Atul Giri
 
DOCX
Unit 4 1 web technology uptu
Abhishek Kesharwani
 
DOCX
Unit 4 web technology uptu
Abhishek Kesharwani
 
PPT
Jsp ppt
Vikas Jagtap
 
PDF
JSP Components and Directives.pdf
Arumugam90
 
PPTX
Jsp basic
Jaya Kumari
 
PPS
Jsp element
kamal kotecha
 
PPTX
Module 3.pptx.............................
Betty333100
 
PPTX
SCWCD : Java server pages CHAP : 9
Ben Abdallah Helmi
 
PPTX
Java ppt
VivekPandit33
 
PPTX
Introduction to JSP.pptx
ManishaPatil932723
 
JSP AND XML USING JAVA WITH GET AND POST METHODS
bharathiv53
 
JSP Directives
ShahDhruv21
 
JSP - Java Server Page
Vipin Yadav
 
Java server pages
Abhishek Kesharwani
 
JAVA SERVER PAGES
Kalpana T
 
WT Unit-Vuufvmjn dissimilating Dunkirk k
asta9578
 
Internet and Web Technology (CLASS-14) [JSP] | NIC/NIELIT Web Technology
Ayes Chinmay
 
Web programming-Introduction to JSP.pptx
mcjaya2024
 
Atul & shubha goswami jsp
Atul Giri
 
Unit 4 1 web technology uptu
Abhishek Kesharwani
 
Unit 4 web technology uptu
Abhishek Kesharwani
 
Jsp ppt
Vikas Jagtap
 
JSP Components and Directives.pdf
Arumugam90
 
Jsp basic
Jaya Kumari
 
Jsp element
kamal kotecha
 
Module 3.pptx.............................
Betty333100
 
SCWCD : Java server pages CHAP : 9
Ben Abdallah Helmi
 
Java ppt
VivekPandit33
 
Introduction to JSP.pptx
ManishaPatil932723
 

Recently uploaded (20)

PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
PDF
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Shinkawa Proposal to meet Vibration API670.pptx
AchmadBashori2
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Thermal runway and thermal stability.pptx
godow93766
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Design Thinking basics for Engineers.pdf
CMR University
 
GitOps_Repo_Structure for begeinner(Scaffolindg)
DanialHabibi2
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
GTU Civil Engineering All Semester Syllabus.pdf
Vimal Bhojani
 
6th International Conference on Machine Learning Techniques and Data Science ...
ijistjournal
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Ad

Introduction - Java Server Programming (JSP)

  • 1. Overview of JSP Technology Introduction ⚫ It stands for Java Server Pages. ⚫ It is a server side technology. ⚫ It is used for creating web application. ⚫ It is used to create dynamic web content. ⚫ In this JSP tags are used to insert JAVA code into HTML pages. ⚫ It is an advanced version of Servlet Technology. ⚫ It is a Web based technology helps us to create dynamic and platform independent web pages. ⚫ In this, Java code can be inserted in HTML/ XML pages or both. ⚫ JSP is first converted into servlet by JSP container before processing the client’s request.
  • 2. Advantageous than Servlet: • They are easy to maintain. ⚫ No recompilation or redeployment is required. ⚫ JSP has access to entire API of JAVA . ⚫ JSP are extended version of Servlet. Features of JSP ⚫ Coding in JSP is easy :- As it is just adding JAVA code to HTML/XML. ⚫ Reduction in the length of Code :- In JSP we use action tags, custom tags etc. ⚫ Connection to Database is easier :-It is easier to connect website to database and allows to read or write data easily to the database. ⚫ Make Interactive websites :- In this we can create dynamic web pages which helps user to interact in real time environment. ⚫ Portable, Powerful, flexible and easy to maintain :- as these are browser and server independent.
  • 3. Architecture of JSP Page ⚫ The web server needs a JSP engine, i.e, a container to process JSP pages. ⚫ The JSP container is responsible for intercepting requests for JSP pages. ⚫ A JSP container works with the Web server to provide the runtime environment and other services a JSP needs. ⚫ Following diagram shows the position of JSP container and JSP files in a Web application.
  • 5. Understanding the JSP Page Lifecycle ⚫The JSP pages follow these phases: ● Compilation ● Initialization ● Execution ● Cleanup ⚫ jspInit(), _jspService() and jspDestroy() are the life cycle methods of JSP.
  • 6. Compilation ⚫ The compilation process involves three steps − ● Parsing the JSP. ● Turning the JSP into a servlet. ● Compiling the servlet. Initialization public void jspInit(){ // Initialization code... } Execution void _jspService(HttpServletRequest request, HttpServletResponse response) { // Service handling code...} Cleanup public void jspDestroy() { // Your cleanup code goes here.}
  • 7. ⚫ <%-- JSP comment --%> ⚫ <HTML> <HEAD> ⚫ <TITLE>MESSAGE</TITLE> ⚫ </HEAD> <BODY> ⚫ <%out.print("Hello, Sample JSP code");%> ⚫ </BODY> ⚫ </HTML> The above JSP generates the following output: Hello, Sample JSP code. ⚫ Explanation of above code 1) The line <%–JSP Comment–%> represents the JSP element called JSP Comment, While adding comments to a JSP page you can use this tag, we will discuss this in detail in coming posts. Note: JSP Comments must starts with a tag <%– and ends with –%> ⚫ 2) Head, Title and Body tags are HTML tags – They are HTML tags, frequently used for static web pages. Whatever content they have is delivered to client(Web browser) as such. ⚫ 3) <%out.print(“ Hello, Sample JSP code ”);%> is a JSP element, which is known as Scriptlet. Scriptlets can contain Java codes. syntax of scriptlet is: <%Executable java code%>. As the code in Scriptlets is java statement, they must end with a semicolon(;). out.print(“ Hello, Sample JSP code ”) is a java statement, which prints“ Hello, Sample JSP code”. ⚫ JSP is used for creating dynamic webpages. Dynamic webpages are usually a mix of static & dynamic content. ⚫ The static content can have text-based formats such as HTML, XML etc and the dynamic content is generated by JSP tags using java code inside HTML .
  • 8. Elements of JSP The JSP elements are used to create JSP page. They are divides into three categories: JSP Scripting Elements JSP Directive Elements JSP Standard Action Elements
  • 9. Scripting Elements of JSP JSP Scripting Elements are used for writing the Java Code inside the JSP page. There are different types of scripting elements these elements are used for various purposes. The scripting elements of JSP are: The Scriptlet ⚫ A scriptlet can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language. ⚫ syntax of Scriptlet − <% code fragment %> ⚫ You can write the XML equivalent of the above syntax as follows − <jsp:scriptlet> code fragment </jsp:scriptlet> <html> <head><title>Hello World</title></head> <body> Hello World! <br/> <% out.println("Your IP address is " + request.getRemoteAddr()); %> </body> </html>
  • 10. JSP Declarations ⚫ A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. ⚫ You must declare the variable or method before you use it in the JSP file. ⚫ syntax for JSP Declarations − <%! declaration; [ declaration; ]+ ... %> You can write the XML equivalent of the above syntax as follows − <jsp:declaration> code fragment </jsp:declaration> example for JSP Declarations − ⚫ <%! int i = 0; %> ⚫ <%! int a, b, c; %> ⚫ <%! Circle a = new Circle(2.0); %>
  • 11. JSP Expression A JSP expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. ⚫ The expression element can contain any expression that is valid according to the Java Language Specification but you cannot use a semicolon to end an expression. syntax of JSP Expression − <%= expression %> You can write the XML equivalent of the above syntax as follows − <jsp:expression> expression </jsp:expression> JSP Expression − <html> <head><title>A Comment Test</title></head> <body> <p>Today's date: <%= (new java.util.Date()).toLocaleString()%></p> </body> </html> The above code will generate the following result − Today's date: 11-Sep-2010 21:24:25
  • 12. JSP Comments ⚫ JSP comment marks text or statements that the JSP container should ignore. A JSP comment is useful when you want to hide or "comment out", a part of your JSP page. syntax of the JSP comments − <%-- This is JSP comment --%> example of the JSP Comments − <html> <head><title>A Comment Test</title></head> <body> <h2>A Test of Comments</h2> <%-- This comment will not be visible in the page source --%> </body> </html> The above code will generate the following result − A Test of Comments
  • 14. JSP directives ⚫ JSP directives are the messages to JSP container. They provide global information about an entire JSP page. ⚫ JSP directives are used to give special instruction to a container for translation of JSP to servlet code. ⚫ In JSP, directive is described in <%@ %> tags. Syntax of Directive: <%@ directive attribute=“value" %> There are three types of directives: ⚫ Page directive ⚫ Include directive ⚫ Taglib directive
  • 15. JSP Page directive Syntax of Page directive: <%@ page attribute="value" %> ⚫ It provides attributes that get applied to entire JSP page. ⚫ JSP page directive is used to define the properties applying the JSP page, such as the size of the allocated buffer, imported packages and classes/interfaces, defining what type of page it is etc. ⚫ It is used to provide instructions to a container that pertains to current JSP page.
  • 16. Attributes of JSP page directive ⚫import ⚫contentType ⚫buffer ⚫Language ⚫errorPage ⚫isErrorPage
  • 17. Import Attribute This tells the container what packages/classes are needed to be imported into the program. Syntax: <%@page import = "value"%> Example : <%-- JSP code to demonstrate how to use page directive to import a package --%> <%@page import = "java.util.Date"%> <%Date d = new Date();%> <%=d%>
  • 18. contentType Attribute This defines the format of data that is being exchanged between the client and the server. It does the same thing as the setContentType method in servlet used to. Syntax: <%@page contentType="value“ %> <%-- JSP code to demonstrate how to use page directive to set the type of content --%> <%@page contentType = "text/html" %> <% = "This is sparta" %>
  • 19. buffer Attribute ⚫ The buffer attribute of page directive is used to specify behavior of JSP container with respect to sending the output to an intermediate buffer, before it is sent to Http response. ⚫ The default size of buffer is 8KB. This means that the JSP container will send the HTTP response in chunks of 8 KBytes. ⚫ The feature can be disabled by setting the buffer attribute to none. In this case, JSP container does not perform any buffering, and sends contents to the client as they are generated. <%@page buffer=”none”%> The buffer attribute of page directive may also be used to change the buffer size. The example below sets the buffer size to 4 KBytes. <%@page buffer=”4kb”%>
  • 20. language Attribute ⚫The language attribute indicates the programming language used in scripting the JSP page. ⚫For example, because you usually use Java as the scripting language, your language option looks like this <%@ page language = "java" %>
  • 21. errorPage and isErrorPage Attribute errorPage Attribute In JSP, the errorPage attribute of page directive is used to specify a web page which will be displayed as an error page for the current JSP page i.e. if there is an error while executing the current page). Syntax <%@ page errorPage= "path-to-another-jsp-page" %> isErrorPage Attribute In JSP, the isErrorPage attribute of page directive is used to specify whether the current page could be displayed as an error page for another JSP page. We could set the value of isErrorPage to either true or false. Syntax <%@ page isErrorPage= "true|false" %> isErrorPage = "true“ ⚫ When isErrorPage is set to true, the current page is displayed in case an error appears in another JSP page. <%@ page isErrorPage= "true" %> isErrorPage = "false“ ⚫ By default, the isErrorPage attribute value is false, hence we don't have to set isErrorPage to false, in case if we don't want the current page to be displayed as an error appears for any JSP page.
  • 22. Include Directive ⚫ The include directive is used to include a file during the translation phase. ⚫ This directive tells the container to merge the content of other external files with the current JSP during the translation phase. ⚫ You may code include directives anywhere in your JSP page. ⚫ The include directive is used to include the contents of any resource it may be jsp file, html file or text file. ⚫ The include directive includes the original content of the included resource at page translation time (the jsp page is translated only once so it will be better to include static resource). ⚫ Advantage of Include directive Code Reusability Syntax of include directive <%@ include file="resourceName" %>
  • 23. Include Directive Example index.jsp <html> <head> <title>Main JSP Page</title> </head> <body> <%@ include file="file1.jsp" %> Main JSP Page: Content between two include directives. <%@ include file="file2.jsp" %> </body> </html> file1.jsp <p align="center"> This is my File1.jsp and I will include it in index.jsp using include directive </p> file2.jsp <p align="center"> This is File2.jsp </p>
  • 25. Taglib Directive ⚫ The taglib directive is used to define tag library that the current JSP page uses. ⚫ A JSP page might include several tag library. ⚫ JavaServer Pages Standard Tag Library (JSTL), is a collection of useful JSP tags, which provides many commonly used core functionalities. ⚫ It has support for many general, structural tasks such as iteration and conditionals, readymade tags for manipulating XML documents, internationalization tags, and for performing SQL operations. Syntax of taglib directive is: <%@ taglib prefix="prefixOfTag" uri="uriOfTagLibrary" %> The prefix is used to distinguish the custom tag from other libary custom tag. Prefix is prepended to the custom tag name. Every custom tag must have a prefix. ⚫ The URI is the unique name for Tag Library.
  • 26. <%@ taglib uri="http://www.sample.com/mycustomlib" prefix="demotag" %> <html> <body> <demotag:welcome/> </body> </html>
  • 27. JSP Actions ⚫JSP actions use the construct in XML syntax to control the behavior of the servlet engine. ⚫We can dynamically insert a file, reuse the beans components, forward user to another page, etc. through JSP Actions like include and forward. ⚫Unlike directives, actions are re-evaluated each time the page is accessed. Syntax: <jsp:action_name attribute="value" />
  • 28. standard Action Tags There are 11 types of Standard Action Tags as following: ⚫jsp:include ⚫jsp:forward
  • 29. jsp:include ⚫ It also used to insert a jsp file into another file, just like include directive. ⚫ It is added during request processing phase Syntax of jsp:include <jsp:include page="page URL" flush="true/false"> <%@ page language="java" contentType="text/html"%> <html> <head> <title>Date JSP</title> </head> <body> <jsp:include page="date.jsp" flush="true" /> </body> </html>
  • 30. Date.jsp <%@ page language="java" contentType="text/html"%> <html> <head> <title>Insert title here</title> </head> <body> <p> Today's date: <%= {new java.util.Date()).toLocaleString()%> </p> </body> </html>
  • 31. jsp:forward: ⚫ It is used to forward the request to another jsp or any static page. ⚫ Here the request can be forwarded with no parameters or with parameters. Syntax: <jsp:forward page="value"> Here value represents where the request has to be forwarded. Action.jsp <%@ page language="java" contentType="text/html"%> <html> <head> <title>Guru Action JSP1</title> </head> <body> <jsp:forward page="jsp_action.jsp" /> </body> </html>