JSPBasics
JSPBasics
JSP Basics
LEVEL PRACTITIONER
About the Author
2
Icons Used
Hands on
Questions Tools Exercise
Best
Demonstrati Practices
Worksho
on & Industry
p
Standards
3
Objectives
4
What is a JSP ?
JSP files are HTML files with special tags that contain Java source code
that provide dynamic content.
5
Sample JSP page
6
Servlet Vs JSP
In Servlets html is written inside java code using print statements. In jsp
java code is embedded inside HTML
JSP are easier for creating HTML content but servlets are easier for
writing the java code.
7
JSP Life Cycle Phases
Translatio
n&
Compilati
on
The JSP will be
Instantiati
translated into
Servlet Java file on
JSP is destroyed
and compiled to by the web
The web container
servlet class by Initializati container when
creates an
the web container. the application is
instance of the on
servlet class. uninstalled.
The web container
instantiates the Service
servlet and makes
it ready for
servicing the This is the phase
request. during which the
Destroy
jsp services the
user requests
8
Jsp Life Cycle methods
jspInit() - The web container calls the jspInit() to initialize the servlet instance
generated. It is invoked before servicing the client request and invoke only once for
a servlet instance.
_jspservice() - The container calls the jspservice() for each user request, passing it
the request and the response objects.
jspDestroy() - The container calls this when it decides take the instance out of
service. It is the last method called in the servlet instance.
9
JSP Life Cycle with a demo
WEB CONTAINER
When Tim Logs in the
Response sent back container invokes
CTSMypay.jsp
to Jack only the service
Web container method.
Response sent back
translates the CTSMypay_jsp.java to Tim.
JSP into a servlet service Web container
(Servlet) When application is
Java file invokes the uninstalled the
Web container _jspservice() for destroy() method of
compiles the CTSMypay_jsp.class Jacks request. the JSP will be
servlet Java into initialization invoked
a class file.
Web container instantiation
Web container destroy
invokes jspInit()
and instantiates initializes the
the class. servlet
10
Lend a Hand : JSP Jump Start
Here we will create a web application with a simple jsp page which prints
a message Welcome to JSP .
This Demo is meant for understanding the following things
1. How to deploy a JSP application?
2. What happens to the JSP when it is deployed?
3. How to call the jsp page from the browser?
Note : We will not be covering the details of the jsp components which will
be covered in the coming slides . Demo just meant to see a basic view of a
JSP page.
11
Lend a Hand : Lets Start
Development
12
Lend a Hand : Step 1 : Create a
Dynamic Web Project
13
Lend a Hand : Step 2 : Create
index.jsp
14
Lend a Hand : Step 2 : Create
index.jsp (Cont)
16
Lend a Hand : Step 3 Deploy
and Run
Right click the application and run it using the option
Run As Run on Server
Call index.jsp from the browser
http://localhost:5000/JSPDemo/index.jsp
NOTE: Ensure the port number and context are correct.
17
What happened to the Index JSP?
The JSP which you had created would have been converted to a servlet file and
compiled as class for servicing users request.
You can find this file in the web server folder where the applications is deployed. The
web server creates a temporary folder for extracting these files.
Lets see how our generated java file of index.jsp looks like.
18
What happened to the Index JSP?
19
Time To Reflect
20
Advance Java