of 41
8/3/2019 43282103-JSP
1/41
1
Java Server Pages (JSP)- Sharad [email protected]
mailto:[email protected]:[email protected]8/3/2019 43282103-JSP
2/41
2
Servlets & JSPs
Agenda
Introduction JSP Elements Implicit Objects
Tag Libraries JSP Actions Error Handling Demo
Java Server Pages (JSP)
8/3/2019 43282103-JSP
3/41
3
Introduction What is JSP
JSP Stands for Java Server Pages
Presents dynamic content to users
Handles the presentation logic in an MVC architecture
Con
tainer
servlet
JSP
request
response
Helper
Objects
(business logic)
(presentation logic)
Busine
ssTier
8/3/2019 43282103-JSP
4/41
4
Introduction JSP v/s Servlet
Servlets JSP
Handles dynamic data
Handles business logic Handles presentation logic
Lifecylce methods
init() : can be overridden
service() : can be overridden
destroy() : can be overridden
Lifecylce methods
jspInit() : can be overridden
_jspService() : cannot be overridden
jspDestroy() : can be overridden
Html within javaout.println();out.println(Time is + new Date());
out.println();
Java within html
Time is
Runs within a Web Container
How is JSP different / similar to Servlet?
8/3/2019 43282103-JSP
5/41
5
Introduction JSP is a Servlet
In the end, a JSP is just a Servlet
0010 00011100 10010001 0011
Import javax.servlet.
HttpServlet.*JSPServlet
MyJsp.jsp MyJsp_jsp.java MyJsp_jsp.class MyJsp_jsp
Servlet
writes
Is translated to Compiles toIs loaded andInitialized as
8/3/2019 43282103-JSP
6/41
8/3/2019 43282103-JSP
7/41
7
JSP Elements Intro
JSP Declarations :: Code that goes outside the service method
JSP Scriptlets :: Code that goes within the service method
JSP Expressions :: Code inside expressions is evaluated
JSP Directives :: Commands given to the JSP engine
Need to write some Java in your HTML?
Want to make your HTML more dynamic?
8/3/2019 43282103-JSP
8/41
8
JSP Elements - Declarations
What are declarations? Whatever goes inside the tags is called a declaration
Everything you write in a declarations goes outside the service method
Treat them as instance methods and instance variables
What do you declare in declarations? You can declare methods and variables in declarations
Why do you need declarations?
You have any variable that needs to be shared across different requests.
You have repeated common code in you jsp, declare it in a method.
Example
8/3/2019 43282103-JSP
9/41
9
JSP Elements - Declarations
Request 2
Request 3
Request 1
Service1 Service2 Service3
Declarations
8/3/2019 43282103-JSP
10/41
10
JSP Elements - Scriptlets
What are declarations? Whatever goes inside the tags is called a scriptlet
Everything you write in a scriptlets goes in the service method
Treat variables in scriptlets as method/local variables
What do you put in scriptlets? Business logic in JSPs are put in scriptlets. Set of java statements
Why do you need scriptlets?
Need to perform some small business logic (if logic is complex, do in java)
Need to perform some basic validations
Example
8/3/2019 43282103-JSP
11/41
11
JSP Elements - Expressions
What are expressions? Whatever goes inside the tags is called an expression
Code inside expressions is evaluated, and output is displayed
Whatever is put inside expressions should evaluate to a value
What do you put in scriptlets?
Variables or methods that return some values
Why do you need scriptlets?
Need to print some text onto the page
Example
8/3/2019 43282103-JSP
12/41
12
JSP Elements - Example
JSP Elements Example
Welcome . You are user number
Expressions
Declarations
Scriptlets
8/3/2019 43282103-JSP
13/41
13
JSP Elements - Directives
What are directives? Whatever goes inside the tags is called a directive
Directives gives pre-processing commands to the JSP Engine.
Everything in directives are processed before JSP is translated to Servlet
What do you put in directives?
Processing commands to the JSP Engine.
Why do you need directives?
To incorporate certain additional features into the JSP
Modifying the Servlet behaviour generated from the JSP
Include other html/jsp files in the JSP.
Provide tag libraries
Example
8/3/2019 43282103-JSP
14/41
14
Directives page directive
Any number of the pre-defined attributes can be added to the page directive
8/3/2019 43282103-JSP
15/41
15
Directives include directive
Including other page content into your JSP
Includes files at translation time
If included file is modified, the main jsp needs to be recompiled
Main page
Contact Details
Hello
header.jsp
footer.jspmain.jsp
8/3/2019 43282103-JSP
16/41
16
Directives taglib directive
Providing output based on common custom logic
Components involved in tag libraries
Tag handler class
Common custom logic and HTML generation
Descriptor configuration fileDirectives properties mapping information
taglib directive
Used in JSPs to use the tag handler functionality
8/3/2019 43282103-JSP
17/41
17
taglib directive high-level overview
..
< sharmanj:txtBoxlength=10 name=userName/>
..
test.jsp
sharmanjTagssharmanj-taglib.tld
web.xml
txtBoxTxtBoxTagEMPTYnametrue.
public class TxtBoxTag extends TagSupport {public int doStartTag() {.
}}
TxtBoxTag.java
sharmanj-taglib.tld
8/3/2019 43282103-JSP
18/41
18
Implicit Objects - Introduction
Set of predefined objects readily available for use.
out: JspWriter
request: HttpRequest
response: HttpResponse
session: HttpSession
config: ServletConfig
application: ServletContex
page: JSP page
pageContext: Special object
8/3/2019 43282103-JSP
19/41
19
Implicit Objects - Example
JSP Implicit objects Example
8/3/2019 43282103-JSP
20/41
20
Implicit Objects out
An instance of JspWriter
Writes statements to the page
The java way to display text on the webpage
Displaying dynamic data
8/3/2019 43282103-JSP
21/41
21
Implicit Objects request
Instance of HttpServletRequest
Use the attributes stored in request object for display /basicvalidations in JSP
Servlet JSP
HelperObject(a = 10)
Uses helperobject
Forwards request to JSP
a = 10
request.setAttribute(a, 10)Servlet JSP
8/3/2019 43282103-JSP
22/41
22
Implicit Objects response
Instance of HttpServletResponse
Send the response back to the client (HTML in most of thecases)
JSP1
Send response to client
JSP1Redirect to JSP2
JSP2Send response to client
response.sendRedirect(url);
8/3/2019 43282103-JSP
23/41
23
Implicit Objects session
Instance of HttpSession
Retrieve and set the session attributes in JSP
Remember the client across multiple requests
Web App CLIENT
Request 1
Request 1
Response1
Response2
SESSION
8/3/2019 43282103-JSP
24/41
24
Implicit Objects config and application
config - Instance of ServletConfig
Application instance of ServletContext
JSP 1
Servlet Context
Servlet Config Servlet ConfigServlet Config Servlet Config
JSP 1JSP 1JSP 1
8/3/2019 43282103-JSP
25/41
25
Implicit Objects page
Main page
Contact Details
Hello
header.jsp
footer.jsp
main.jsp
8/3/2019 43282103-JSP
26/41
26
Implicit Objects pageContext
request
page
application
session
Leastvisibletomo
stvisible
pageContext
8/3/2019 43282103-JSP
27/41
27
JSP Actions
Special tags which provides special features
The container handles these tags in a special way
Addresses certain common functionalities
Achieve functionality with less code, and more standard way
8/3/2019 43282103-JSP
28/41
28
JSP Actions -
Definition:Includes a page at the given location in the main page
Syntax:
8/3/2019 43282103-JSP
29/41
29
JSP Actions Include Action v/s Include Directive
Include Directive Include Action
Translation time Run time
Copies the included file References to the included file
For static content For dynamic content
Cannot pass parameters Can pass parameters
8/3/2019 43282103-JSP
30/41
30
JSP Actions -
8/3/2019 43282103-JSP
31/41
31
JSP Actions -
Definition:Pass parameters to the included/forwarded page
Syntax:
8/3/2019 43282103-JSP
32/41
32
JSP Actions -
Servlet JSP
bean
How can I usethat bean?
getId();setId(..)getName()setName()
request.setAttribute(userBean, userBean)
8/3/2019 43282103-JSP
33/41
33
JSP Actions -
Definition:Instantiate a bean class, use bean properties, and set property values
Syntax:
UserBean ub = new UserBean();
8/3/2019 43282103-JSP
34/41
34
JSP Actions -
Definition:Gets property values of a Bean
Syntax:
8/3/2019 43282103-JSP
35/41
35
JSP Actions -
Definition:Sets property values in a Bean
Syntax:
8/3/2019 43282103-JSP
36/41
36
JSP Actions useBean
8/3/2019 43282103-JSP
37/41
37
Error Handling Types of errors
Errors in java code in scriptlets
handle errors in try/catch block
may occur at runtime
Errors in HTML
adhere to the HTML rules
can be caught during testing
8/3/2019 43282103-JSP
38/41
38
Error Handling
Why is error handling important in JSPs?
8/3/2019 43282103-JSP
39/41
39
Error Handling Error page
Error pages come to the rescue
My Error PageWe are sorry, the page you aretrying to access is currently notavailable. Please try after some time
8/3/2019 43282103-JSP
40/41
40
Error Handling
Specify error page in your main page
8/3/2019 43282103-JSP
41/41
Thanks