SlideShare a Scribd company logo
1 of 24
 JavaServer Pages (JSP) is a technology for developing
Webpages that supports dynamic content.
 This helps developers insert java code in HTML pages by
making use of special JSP tags, most of which start with <%
and end with %>.
 A JavaServer Pages component is a type of Java servlet that is
designed to fulfill the role of a user interface for a Java web
application.
 Web developers write JSPs as text files that combine HTML or
XHTML code, XML elements, and embedded JSP actions and
commands.
JSP Overview
 Using JSP, you can collect input from users through Webpage
forms, present records from a database or another source,
and create Webpages dynamically.
 JSP tags can be used for a variety of purposes, such as
retrieving information from a database or registering user
preferences, accessing JavaBeans components, passing
control between pages, and sharing information between
requests, pages etc.
JSP Overview
Why Use JSP?
 JavaServer Pages often serve the same purpose as programs
implemented using the Common Gateway Interface (CGI).
But JSP offers several advantages in comparison with the
CGI.
 Performance is significantly better because JSP allows
embedding Dynamic Elements in HTML Pages itself instead
of having separate CGI files.
 JSP are always compiled before they are processed by the
server unlike CGI/Perl which requires the server to load an
interpreter and the target script each time the page is
requested.
The life cycle of a JSP page can be divided into the
following phase:
1.Translation Phase
2.Compilation Phase
3.Initialization Phase
4.Execution Phase
5.Destruction(Cleanup) Phase
Translation phase
1.When a user navigates to a page ending with a .jsp extension in their web
browser, the web browser sends an HTTP request to the webserver.
2.The webserver checks if a compiled version of the JSP page already exists.
3.If the JSP page's compiled version does not exist, the file is sent to the JSP
Servlet engine, which converts it into servlet content (with .java extension).
This process is known as translation.
4.The web container automatically translates the JSP page into a servlet. So
as a developer, you don't have to worry about converting it manually.
Compilation Phase
1. In this compilation phase, the Translated .java file of the servlet is
compiled into the Java servlet .class file.
2. In case the JSP page was not compiled previously or at any point in
time, then the JSP page is compiled by the JSP engine.
Initialization Phase
1. In this Initialization phase: Here, the container will:
2. Load the equivalent servlet class.
3. Create instance.
4. Call the jspInit() method for initializing the servlet instance.
5. This initialization is done only once with the servlet's init method
where database connection, opening files, and creating lookup tables
are done.
Execution Phase
1. This Execution phase is responsible for all JSP interactions and
logic executions for all requests until the JSP gets destroyed.
2. As the requested JSP page is loaded and initiated, the JSP engine
has to invoke the jspService() method.
3. This method is invoked as per the requests, responsible for
generating responses for the associated requests, and responsible for
all HTTP methods.
Destruction(Cleanup) Phase
1. This destruction phase is invoked when the JSP has to be cleaned
up from use by the container.
2. The jspDestroy() method is invoked. You can incorporate and write
cleanup codes inside this method for releasing database
connections or closing any file.
JSP syntax
Syntax available in JSP are following:
1.Declaration Tag :-It is used to declare variables.
Syntax:- <%! Dec var %>
Example:- <%! int var=10; %>
2.Java Scriplets :- It allows us to add any number of
JAVA code, variables and expressions.
Syntax:- <% java code %>
3.JSP Expression :- It evaluates and convert the
expression to a string.
Syntax:- <%= expression %>
Example:- <% num1 = num1+num2 %>
4.JAVA Comments :- It contains the text that is added for
information which has to be ignored.
Syntax:- <% -- JSP Comments %>
Elements of JSP in detail:
The elements of JSP have been described below −
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.
Following is the 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.
Following is the syntax for JSP Declarations −
<%! declaration; [ declaration; ]+ ... %>
You can write the XML equivalent of the above syntax as follows −
<jsp:declaration> code fragment </jsp:declaration>
Following is an example for JSP Declarations −
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>
JSP Expression
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.
Following is the syntax of JSP Expression −
<%= expression %>
You can write the XML equivalent of the above syntax as follows −
<jsp:expression> expression </jsp:expression>
<html>
<head>
<title>
A Comment Test
</title>
</head>
<body>
<p>Today's date: <%= (new
java.util.Date()).toLocaleString()%></p>
</body>
</html>
Following example shows a JSP Expression −
demo.jsp
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=ISO-8859-1">
<title>Hello World - JSP tutorial</title>
</head>
<body>
<%= "Hello World!" %>
</body> </html>
Advantages of using JSP
•It does not require advanced knowledge of JAVA
•It is capable of handling exceptions
•Easy to use and learn
•It can tags which are easy to use and understand
•Implicit objects are there which reduces the length of code
•It is suitable for both JAVA and non JAVA programmer
Disadvantages of using JSP
•Difficult to debug for errors.
•First time access leads to wastage of time
•It’s output is HTML which lacks features.
Following steps are involved in the JSP life cycle:
 Translation of JSP page to Servlet
 Compilation of JSP page(Compilation of JSP into test.java)
 Classloading (test.java to test.class)
 Instantiation(Object of the generated Servlet is created)
 Initialization(jspInit() method is invoked by the container)
 Request processing(_jspService()is invoked by the container)
 JSP Cleanup (jspDestroy() method is invoked by the
container)
JSP - Directives
Directives provide directions and instructions to the container,
telling it how to handle certain aspects of the JSP processing.
A JSP directive affects the overall structure of the servlet class. It
usually has the following form −
<%@ directive attribute = "value" %>
Directives can have a number of attributes which you can list
down as key-value pairs and separated by commas.
The blanks between the @ symbol and the directive name, and
between the last attribute and the closing %>, are optional.
No Directive & Description
1
<%@ page ... %> Ex: <%@ page attribute = "value" %>
Defines page-dependent attributes, such as scripting language,
error page, and buffering requirements.
2
<%@ include ... %> Ex: <%@ include file = "relative url" >
Includes a file during the translation phase.
3
<%@ taglib ... %> Ex: <%@ taglib uri="uri" prefix = "prefixOfTag" >
Declares a tag library, containing custom actions, used in the page
There are three types of directive tag −
JSP | Implicit Objects – request and
response
 Implicit objects are a set of Java objects that the JSP
Container makes available to developers on each page.
 These objects may be accessed as built-in variables via
scripting elements and can also be accessed
programmatically by JavaBeans and Servlets.
 JSP provide you Total 9 implicit objects which are as follows:
1.request: This is the object of HttpServletRequest class
associated with the request.
2.response: This is the object of HttpServletResponse class
associated with the response to the client.
3. config: This is the object of ServletConfig class associated
with the page.
4. application: This is the object of ServletContext class
associated with the application context.
5. session: This is the object of HttpSession class associated
with the request.
6. page context: This is the object of PageContext class that
encapsulates the use of server-specific features. This object
can be used to find, get or remove an attribute.
7. page object: The manner we use the keyword this for current
object, page object is used to refer to the current translated
servlet class.
8. exception: The exception object represents all errors and
exceptions which is accessed by the respective jsp. The
exception implicit object is of type java.lang.Throwable.
9. out: This is the PrintWriter object where methods like print
and println help for displaying the content to the client.

More Related Content

Similar to JSP.pptx (20)

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
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Spatial approximate string search Doc
Spatial approximate string search DocSpatial approximate string search Doc
Spatial approximate string search Doc
 
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
 
Jsp tutorial
Jsp tutorialJsp tutorial
Jsp tutorial
 
Jsp Introduction Tutorial
Jsp Introduction TutorialJsp Introduction Tutorial
Jsp Introduction Tutorial
 
WEB TECHNOLOGIES JSP
WEB TECHNOLOGIES  JSPWEB TECHNOLOGIES  JSP
WEB TECHNOLOGIES JSP
 
3.jsp tutorial
3.jsp tutorial3.jsp tutorial
3.jsp tutorial
 
Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course Introduction to the Servlet / JSP course
Introduction to the Servlet / JSP course
 
Jsp 01
Jsp 01Jsp 01
Jsp 01
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
J2EE jsp_01
J2EE jsp_01J2EE jsp_01
J2EE jsp_01
 
JSP Directives
JSP DirectivesJSP Directives
JSP Directives
 
JSP - Java Server Page
JSP - Java Server PageJSP - Java Server Page
JSP - Java Server Page
 
JSP Components and Directives.pdf
JSP Components and Directives.pdfJSP Components and Directives.pdf
JSP Components and Directives.pdf
 
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSINGINTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE  AND JSP PROCESSING
INTRODUCTION TO JSP,JSP LIFE CYCLE, ANATOMY OF JSP PAGE AND JSP PROCESSING
 

Recently uploaded

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 

JSP.pptx

  • 1.  JavaServer Pages (JSP) is a technology for developing Webpages that supports dynamic content.  This helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>.  A JavaServer Pages component is a type of Java servlet that is designed to fulfill the role of a user interface for a Java web application.  Web developers write JSPs as text files that combine HTML or XHTML code, XML elements, and embedded JSP actions and commands. JSP Overview
  • 2.  Using JSP, you can collect input from users through Webpage forms, present records from a database or another source, and create Webpages dynamically.  JSP tags can be used for a variety of purposes, such as retrieving information from a database or registering user preferences, accessing JavaBeans components, passing control between pages, and sharing information between requests, pages etc. JSP Overview
  • 3. Why Use JSP?  JavaServer Pages often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But JSP offers several advantages in comparison with the CGI.  Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself instead of having separate CGI files.  JSP are always compiled before they are processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested.
  • 4. The life cycle of a JSP page can be divided into the following phase: 1.Translation Phase 2.Compilation Phase 3.Initialization Phase 4.Execution Phase 5.Destruction(Cleanup) Phase
  • 5.
  • 6. Translation phase 1.When a user navigates to a page ending with a .jsp extension in their web browser, the web browser sends an HTTP request to the webserver. 2.The webserver checks if a compiled version of the JSP page already exists. 3.If the JSP page's compiled version does not exist, the file is sent to the JSP Servlet engine, which converts it into servlet content (with .java extension). This process is known as translation. 4.The web container automatically translates the JSP page into a servlet. So as a developer, you don't have to worry about converting it manually.
  • 7. Compilation Phase 1. In this compilation phase, the Translated .java file of the servlet is compiled into the Java servlet .class file. 2. In case the JSP page was not compiled previously or at any point in time, then the JSP page is compiled by the JSP engine.
  • 8. Initialization Phase 1. In this Initialization phase: Here, the container will: 2. Load the equivalent servlet class. 3. Create instance. 4. Call the jspInit() method for initializing the servlet instance. 5. This initialization is done only once with the servlet's init method where database connection, opening files, and creating lookup tables are done.
  • 9. Execution Phase 1. This Execution phase is responsible for all JSP interactions and logic executions for all requests until the JSP gets destroyed. 2. As the requested JSP page is loaded and initiated, the JSP engine has to invoke the jspService() method. 3. This method is invoked as per the requests, responsible for generating responses for the associated requests, and responsible for all HTTP methods.
  • 10. Destruction(Cleanup) Phase 1. This destruction phase is invoked when the JSP has to be cleaned up from use by the container. 2. The jspDestroy() method is invoked. You can incorporate and write cleanup codes inside this method for releasing database connections or closing any file.
  • 11. JSP syntax Syntax available in JSP are following: 1.Declaration Tag :-It is used to declare variables. Syntax:- <%! Dec var %> Example:- <%! int var=10; %> 2.Java Scriplets :- It allows us to add any number of JAVA code, variables and expressions. Syntax:- <% java code %> 3.JSP Expression :- It evaluates and convert the expression to a string. Syntax:- <%= expression %> Example:- <% num1 = num1+num2 %> 4.JAVA Comments :- It contains the text that is added for information which has to be ignored. Syntax:- <% -- JSP Comments %>
  • 12. Elements of JSP in detail: The elements of JSP have been described below − 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. Following is the syntax of Scriptlet − <% code fragment %> You can write the XML equivalent of the above syntax as follows − <jsp:scriptlet> code fragment </jsp:scriptlet>
  • 13. <html> <head> <title>Hello World</title> </head> <body> Hello World!<br/> <% out.println("Your IP address is " + request.getRemoteAddr()); %> </body> </html>
  • 14. 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. Following is the syntax for JSP Declarations − <%! declaration; [ declaration; ]+ ... %> You can write the XML equivalent of the above syntax as follows − <jsp:declaration> code fragment </jsp:declaration> Following is an example for JSP Declarations − <%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %>
  • 15. JSP Expression 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. Following is the syntax of JSP Expression − <%= expression %> You can write the XML equivalent of the above syntax as follows − <jsp:expression> expression </jsp:expression>
  • 16. <html> <head> <title> A Comment Test </title> </head> <body> <p>Today's date: <%= (new java.util.Date()).toLocaleString()%></p> </body> </html> Following example shows a JSP Expression −
  • 17. demo.jsp <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Hello World - JSP tutorial</title> </head> <body> <%= "Hello World!" %> </body> </html>
  • 18. Advantages of using JSP •It does not require advanced knowledge of JAVA •It is capable of handling exceptions •Easy to use and learn •It can tags which are easy to use and understand •Implicit objects are there which reduces the length of code •It is suitable for both JAVA and non JAVA programmer Disadvantages of using JSP •Difficult to debug for errors. •First time access leads to wastage of time •It’s output is HTML which lacks features.
  • 19. Following steps are involved in the JSP life cycle:  Translation of JSP page to Servlet  Compilation of JSP page(Compilation of JSP into test.java)  Classloading (test.java to test.class)  Instantiation(Object of the generated Servlet is created)  Initialization(jspInit() method is invoked by the container)  Request processing(_jspService()is invoked by the container)  JSP Cleanup (jspDestroy() method is invoked by the container)
  • 20. JSP - Directives Directives provide directions and instructions to the container, telling it how to handle certain aspects of the JSP processing. A JSP directive affects the overall structure of the servlet class. It usually has the following form − <%@ directive attribute = "value" %> Directives can have a number of attributes which you can list down as key-value pairs and separated by commas. The blanks between the @ symbol and the directive name, and between the last attribute and the closing %>, are optional.
  • 21. No Directive & Description 1 <%@ page ... %> Ex: <%@ page attribute = "value" %> Defines page-dependent attributes, such as scripting language, error page, and buffering requirements. 2 <%@ include ... %> Ex: <%@ include file = "relative url" > Includes a file during the translation phase. 3 <%@ taglib ... %> Ex: <%@ taglib uri="uri" prefix = "prefixOfTag" > Declares a tag library, containing custom actions, used in the page There are three types of directive tag −
  • 22. JSP | Implicit Objects – request and response  Implicit objects are a set of Java objects that the JSP Container makes available to developers on each page.  These objects may be accessed as built-in variables via scripting elements and can also be accessed programmatically by JavaBeans and Servlets.  JSP provide you Total 9 implicit objects which are as follows: 1.request: This is the object of HttpServletRequest class associated with the request. 2.response: This is the object of HttpServletResponse class associated with the response to the client.
  • 23. 3. config: This is the object of ServletConfig class associated with the page. 4. application: This is the object of ServletContext class associated with the application context. 5. session: This is the object of HttpSession class associated with the request. 6. page context: This is the object of PageContext class that encapsulates the use of server-specific features. This object can be used to find, get or remove an attribute.
  • 24. 7. page object: The manner we use the keyword this for current object, page object is used to refer to the current translated servlet class. 8. exception: The exception object represents all errors and exceptions which is accessed by the respective jsp. The exception implicit object is of type java.lang.Throwable. 9. out: This is the PrintWriter object where methods like print and println help for displaying the content to the client.