SlideShare une entreprise Scribd logo
1  sur  31
Java Server Pages
(JSP)
PRESENTED BY:
Manisha Keim
Introduction
 JSP (Java Server Pages) is server side technology to create
dynamic java web application.
 Allows Java programming code to be embedded in the HTML
pages.
 JSP contains an extension of .jsp
 After execution of a JSP page a plain HTML is produced and
displayed in the client's Web browser.
 JSP can be thought as an extension to servlet technology
because it provides features to easily create user views.
HTTP request
HTTP response
WEB
SERVER
JSP page
JSP container
compiles to
a servlet
URL
request
JavaBean
Library
DB
properties,
call methods
HTTP page
response
BROWSE
R
DATABASE
SERVER
JSP Architecture
Java Server Pages are part of a 3-tier architecture. A server will act as a mediator between
client browser and database.
JSP Architecture
 The following steps explain how the web server creates the web page using JSP:
 As with a normal page, your browser sends an HTTP request to the web server.
 The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This
is done by using the URL or JSP page which ends with .jsp instead of .html.
 The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is very
simple in which all template text is converted to println( ) statements and all JSP elements are converted to
Java code that implements the corresponding dynamic behavior of the page.
 The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet
engine.
 A part of the web server called the servlet engine loads the Servlet class and executes it. During execution,
the servlet produces an output in HTML format, which the servlet engine passes to the web server inside
an HTTP response.
 The web server forwards the HTTP response to your browser in terms of static HTML content.
 Finally web browser handles the dynamically generated HTML page inside the HTTP response exactly as if
it were a static page.
Loading
Instantiation
Initialization
RequestDestroy
Translation
Compilation
JSP
LIFECYCLE
Web Server
hello.jsp hello_jsp.java
Step: 1
Step: 2
hello_jsp.class
Step: 3Step: 4Step: 5
jspInit() Create
Step: 6
jspService()
Step: 7
jspDestroy()
Web Container
JSP Life Cycle
 Web Container translates JSP code into a servlet class
source(.java) file, then compiles that into a java servlet class.
In the third step, the servlet class bytecode is loaded using
classloader. The Container then creates an instance of that
servlet class.
 The initialized servlet can now service request. For each
request the Web Container call the jspService() method.
 When the Container removes the servlet instance from
service, it calls the jspDestroy() method to perform any
required clean up.
Web Container
 Translation – JSP pages doesn’t look like normal java classes, actually JSP container parse the
JSP pages and translate them to generate corresponding servlet source code. If JSP file name is
hello.jsp, usually its named as hello_jsp.java.
 Compilation – If the translation is successful, then container compiles the generated servlet
source file to generate class file.
 Class Loading – Once JSP is compiled as servlet class, its lifecycle is similar to servlet and it
gets loaded into memory.
 Instance Creation – After JSP class is loaded into memory, its object is instantiated by the
container.
 Initialization – The JSP class is then initialized and it transforms from a normal class to servlet.
 Request Processing – For every client request, a new thread is spawned with ServletRequest
and ServletResponse to process and generate the HTML response.
 Destroy – Last phase of JSP life cycle where it’s unloaded into memory.
JSP Life Cycle Phases
JSP Lifecycle Methods
Once a JSP page is translated to a servlet, the container invokes the following life
cycle methods on the servlet :
• jspInit() : This method is invoked at the time when the servlet is initialized.
• jspService() : This method is invoked when request for the JSP page is received.
• jspDestroy() : This method is invoked before the servlet is removes from the
service.
What happens to a JSP page when it is
translated into Servlet
• EXPRESSION enclosed in <%= and %> markers
A expression is used to insert the result of a Java expression directly into the
output.
• SCRIPTLET enclosed in <% and %> markers:
A scriptlet can contain any number of JAVA language statements, variable or
method declarations, or expressions that are valid in the page scripting language.
<%
String message = “Hello World”;
out.println (message);
%>
The time is : <%= new java.util.Date() %>
JSP Elements
• DIRECTIVES syntax - <%@ directive attribute = "value" %>
A JSP directive gives special information about the JSP page to the JSP Engine.
• There are three main types of directive :-
 page : processing information for this page
 include : files to be included
 taglib : tag library to be used in the page
<%@page import="java.util.Date" %>
<%
Date x = new java.util.Date();
out.println (x);
%>
Page is used in importing
external classes and providing
information about the page
<%@include file="externalContent.jsp" %>
include is used to
include the code
written in another file.
taglib is used to allow users use tags they
defined themselves using "custom tags"
• DECLARATION enclosed in <%! and %> markers
This tag allows the developer to declare variables or methods.
• Code placed in this must end in a semicolon(;).
• Declarations do not generate output, so are used with JSP expressions or
scriptlets.
<%! private int counter = 0 ;
private String getAccount (int accountNo); %>
HTML
JSP – ENVIRONMENT
SETUP
What is JDK? JRE?
 JRE: Java Runtime Environment. It is basically the Java Virtual
Machine where your Java programs run on.
 JDK: It's the full featured Software Development Kit for Java,
including JRE, and the compilers and tools to create and compile
programs.
 The JDK is a superset of the JRE, and contains everything that is in
the JRE.
 Sometimes, even though you are not planning to do any Java
Development on a computer, you still need the JDK installed.
Because application server will convert JSP into Servlets and
use JDK to compile the servlets.
Java – Environment Setup
QUESTIONS
A. Translate the JSP into a servlet.
B. Compile servlet source code.
C. Call _jspService()
D. Instantiate the servlet class.
E. Call jspInit()
F. Call jspDestroy()
Qus 1. Which JSP Lifecycle step is out of order?
A. <% page import=“java.util.Date” %>
B. <%@ page import=“java.util.Date” @%>
C. <%@ page import=“java.util.Date” %>
D. <% import java.util.Date %>
Qus 2. Which is an example of the syntax used to
import a class in a JSP?
To Display:
"Good Morning", if it is between 3:00am and 12:00pm
"Good Afternoon", if it is between 12:00pm and 6:00pm
"Good Evening", if it is after 6:00 pm
Qus 3.
References
 http://www.studytonight.com/jsp/lifecycle-of-jsp.php
 https://www.ntu.edu.sg/home/ehchua/programming/java/JSPB
yExample.html
 http://met.guc.edu.eg/OnlineTutorials/JSP%20-
%20Servlets/A%20simple%20JSP%20example.aspx
 Head First JavaScript Programming: A Brain-Friendly Guide
 https://www.tutorialspoint.com/java/java_environment_setup.h
tm
S
THANK YOU ☺

Contenu connexe

Tendances (20)

Jsp element
Jsp elementJsp element
Jsp element
 
Java swing
Java swingJava swing
Java swing
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Server Controls of ASP.Net
Server Controls of ASP.NetServer Controls of ASP.Net
Server Controls of ASP.Net
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Jsp chapter 1
Jsp chapter 1Jsp chapter 1
Jsp chapter 1
 
Event handling
Event handlingEvent handling
Event handling
 
Ajax
AjaxAjax
Ajax
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Tracing in ASp.Net
Tracing in ASp.NetTracing in ASp.Net
Tracing in ASp.Net
 
Jdbc architecture and driver types ppt
Jdbc architecture and driver types pptJdbc architecture and driver types ppt
Jdbc architecture and driver types ppt
 
servlet in java
servlet in javaservlet in java
servlet in java
 
Java Beans
Java BeansJava Beans
Java Beans
 
Remote Method Invocation (RMI)
Remote Method Invocation (RMI)Remote Method Invocation (RMI)
Remote Method Invocation (RMI)
 
Introduction to ASP.NET
Introduction to ASP.NETIntroduction to ASP.NET
Introduction to ASP.NET
 
Ajax ppt
Ajax pptAjax ppt
Ajax ppt
 

Similaire à Java Server Pages(jsp)

Similaire à Java Server Pages(jsp) (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 overview
JSP overviewJSP overview
JSP overview
 
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
 
J2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - ComponentsJ2EE - JSP-Servlet- Container - Components
J2EE - JSP-Servlet- Container - Components
 
Java server pages
Java server pagesJava server pages
Java server pages
 
Jsp in Servlet by Rj
Jsp in Servlet by RjJsp in Servlet by Rj
Jsp in Servlet by Rj
 
Jsp tutorial
Jsp tutorialJsp tutorial
Jsp tutorial
 
JSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGESJSP- JAVA SERVER PAGES
JSP- JAVA SERVER PAGES
 
Arpita industrial trainingppt
Arpita industrial trainingpptArpita industrial trainingppt
Arpita industrial trainingppt
 
Jsp
JspJsp
Jsp
 
JSP Components and Directives.pdf
JSP Components and Directives.pdfJSP Components and Directives.pdf
JSP Components and Directives.pdf
 
Atul & shubha goswami jsp
Atul & shubha goswami jspAtul & shubha goswami jsp
Atul & shubha goswami jsp
 
Java JSP.pptx
Java JSP.pptxJava JSP.pptx
Java JSP.pptx
 
Jsp and Servlets
Jsp and ServletsJsp and Servlets
Jsp and Servlets
 
AJppt.pptx
AJppt.pptxAJppt.pptx
AJppt.pptx
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
Jsp
JspJsp
Jsp
 
Jsp
JspJsp
Jsp
 

Plus de Manisha Keim

Plus de Manisha Keim (20)

National Rail Museum
National Rail MuseumNational Rail Museum
National Rail Museum
 
Electricity
ElectricityElectricity
Electricity
 
Health, Hygiene and Cleanliness
Health, Hygiene and CleanlinessHealth, Hygiene and Cleanliness
Health, Hygiene and Cleanliness
 
Transport Layer Numericals
Transport Layer NumericalsTransport Layer Numericals
Transport Layer Numericals
 
Physical Layer Questions
Physical Layer QuestionsPhysical Layer Questions
Physical Layer Questions
 
Network Layer Numericals
Network Layer NumericalsNetwork Layer Numericals
Network Layer Numericals
 
Data Link Layer Numericals
Data Link Layer NumericalsData Link Layer Numericals
Data Link Layer Numericals
 
Circle
CircleCircle
Circle
 
Rational Numbers
Rational NumbersRational Numbers
Rational Numbers
 
Data Handling
Data HandlingData Handling
Data Handling
 
Environment for Class IV
Environment for Class IVEnvironment for Class IV
Environment for Class IV
 
Tsunami
TsunamiTsunami
Tsunami
 
Concept of c data types
Concept of c data typesConcept of c data types
Concept of c data types
 
Kabir
KabirKabir
Kabir
 
Abc Of Friendship
Abc Of FriendshipAbc Of Friendship
Abc Of Friendship
 
History of India
History of IndiaHistory of India
History of India
 
Moments
MomentsMoments
Moments
 
Coordinate Geometry
Coordinate GeometryCoordinate Geometry
Coordinate Geometry
 
Computers
ComputersComputers
Computers
 
Powepoint
PowepointPowepoint
Powepoint
 

Dernier

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
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
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
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
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
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
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 

Dernier (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
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
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
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
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
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
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 

Java Server Pages(jsp)

  • 2. Introduction  JSP (Java Server Pages) is server side technology to create dynamic java web application.  Allows Java programming code to be embedded in the HTML pages.  JSP contains an extension of .jsp  After execution of a JSP page a plain HTML is produced and displayed in the client's Web browser.  JSP can be thought as an extension to servlet technology because it provides features to easily create user views.
  • 3. HTTP request HTTP response WEB SERVER JSP page JSP container compiles to a servlet URL request JavaBean Library DB properties, call methods HTTP page response BROWSE R DATABASE SERVER JSP Architecture Java Server Pages are part of a 3-tier architecture. A server will act as a mediator between client browser and database.
  • 4. JSP Architecture  The following steps explain how the web server creates the web page using JSP:  As with a normal page, your browser sends an HTTP request to the web server.  The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This is done by using the URL or JSP page which ends with .jsp instead of .html.  The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is very simple in which all template text is converted to println( ) statements and all JSP elements are converted to Java code that implements the corresponding dynamic behavior of the page.  The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet engine.  A part of the web server called the servlet engine loads the Servlet class and executes it. During execution, the servlet produces an output in HTML format, which the servlet engine passes to the web server inside an HTTP response.  The web server forwards the HTTP response to your browser in terms of static HTML content.  Finally web browser handles the dynamically generated HTML page inside the HTTP response exactly as if it were a static page.
  • 6. Web Server hello.jsp hello_jsp.java Step: 1 Step: 2 hello_jsp.class Step: 3Step: 4Step: 5 jspInit() Create Step: 6 jspService() Step: 7 jspDestroy() Web Container JSP Life Cycle
  • 7.  Web Container translates JSP code into a servlet class source(.java) file, then compiles that into a java servlet class. In the third step, the servlet class bytecode is loaded using classloader. The Container then creates an instance of that servlet class.  The initialized servlet can now service request. For each request the Web Container call the jspService() method.  When the Container removes the servlet instance from service, it calls the jspDestroy() method to perform any required clean up. Web Container
  • 8.  Translation – JSP pages doesn’t look like normal java classes, actually JSP container parse the JSP pages and translate them to generate corresponding servlet source code. If JSP file name is hello.jsp, usually its named as hello_jsp.java.  Compilation – If the translation is successful, then container compiles the generated servlet source file to generate class file.  Class Loading – Once JSP is compiled as servlet class, its lifecycle is similar to servlet and it gets loaded into memory.  Instance Creation – After JSP class is loaded into memory, its object is instantiated by the container.  Initialization – The JSP class is then initialized and it transforms from a normal class to servlet.  Request Processing – For every client request, a new thread is spawned with ServletRequest and ServletResponse to process and generate the HTML response.  Destroy – Last phase of JSP life cycle where it’s unloaded into memory. JSP Life Cycle Phases
  • 9. JSP Lifecycle Methods Once a JSP page is translated to a servlet, the container invokes the following life cycle methods on the servlet : • jspInit() : This method is invoked at the time when the servlet is initialized. • jspService() : This method is invoked when request for the JSP page is received. • jspDestroy() : This method is invoked before the servlet is removes from the service.
  • 10. What happens to a JSP page when it is translated into Servlet
  • 11. • EXPRESSION enclosed in <%= and %> markers A expression is used to insert the result of a Java expression directly into the output. • SCRIPTLET enclosed in <% and %> markers: A scriptlet can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language. <% String message = “Hello World”; out.println (message); %> The time is : <%= new java.util.Date() %> JSP Elements
  • 12. • DIRECTIVES syntax - <%@ directive attribute = "value" %> A JSP directive gives special information about the JSP page to the JSP Engine. • There are three main types of directive :-  page : processing information for this page  include : files to be included  taglib : tag library to be used in the page <%@page import="java.util.Date" %> <% Date x = new java.util.Date(); out.println (x); %> Page is used in importing external classes and providing information about the page <%@include file="externalContent.jsp" %> include is used to include the code written in another file. taglib is used to allow users use tags they defined themselves using "custom tags"
  • 13. • DECLARATION enclosed in <%! and %> markers This tag allows the developer to declare variables or methods. • Code placed in this must end in a semicolon(;). • Declarations do not generate output, so are used with JSP expressions or scriptlets. <%! private int counter = 0 ; private String getAccount (int accountNo); %>
  • 14. HTML
  • 16.
  • 17. What is JDK? JRE?  JRE: Java Runtime Environment. It is basically the Java Virtual Machine where your Java programs run on.  JDK: It's the full featured Software Development Kit for Java, including JRE, and the compilers and tools to create and compile programs.  The JDK is a superset of the JRE, and contains everything that is in the JRE.  Sometimes, even though you are not planning to do any Java Development on a computer, you still need the JDK installed. Because application server will convert JSP into Servlets and use JDK to compile the servlets.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 24.
  • 25.
  • 27. A. Translate the JSP into a servlet. B. Compile servlet source code. C. Call _jspService() D. Instantiate the servlet class. E. Call jspInit() F. Call jspDestroy() Qus 1. Which JSP Lifecycle step is out of order?
  • 28. A. <% page import=“java.util.Date” %> B. <%@ page import=“java.util.Date” @%> C. <%@ page import=“java.util.Date” %> D. <% import java.util.Date %> Qus 2. Which is an example of the syntax used to import a class in a JSP?
  • 29. To Display: "Good Morning", if it is between 3:00am and 12:00pm "Good Afternoon", if it is between 12:00pm and 6:00pm "Good Evening", if it is after 6:00 pm Qus 3.
  • 30. References  http://www.studytonight.com/jsp/lifecycle-of-jsp.php  https://www.ntu.edu.sg/home/ehchua/programming/java/JSPB yExample.html  http://met.guc.edu.eg/OnlineTutorials/JSP%20- %20Servlets/A%20simple%20JSP%20example.aspx  Head First JavaScript Programming: A Brain-Friendly Guide  https://www.tutorialspoint.com/java/java_environment_setup.h tm

Notes de l'éditeur

  1. When you develop a webpage, you use HTML to describe what the page should look like and how it should behave.
  2. The _jspService() method can never be called before jspInit().