Title: Java Server Pages
1Java Server PagesA technology for serving
dynamic web content
Internet Applications, Lecture 4
What is JSP ? Elements of a JSP
document JavaBeans and JSP TagLibs, JSTL JSP and
Java Servlets JSP v2.0 Summary
- Stanislaw Osinski, stachoo_at_man.poznan.pl
2Agenda
- What is JSP ?
- Elements of a JSP document
- JavaBeans and JSP
- Exception handling
- Custom Tags, JSP Standard Tag Library
- JSP and Java Servlets
- JSP v2.0 new features
- Summary
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
3What is JSP ?
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
The JavaServer Pages technology provides the
means for textual specification of the creation
of a dynamic response to a request (JSP
Spec. v1.2)
4What is JSP ?
JSP is a textual specification of the creation of
a dynamic response to a request (JSP Spec. v1.2)
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
5What is JSP ?
JSP is a textual specification of the creation of
a dynamic response to a request (JSP Spec. v1.2)
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
ltHTMLgt ltTITLEgtTitlelt/TITLEgt ltBODYgt ltPgt
Today is lt new java.util.Date().toSt
ring() gt lt/Pgt lt/BODYgt lt/HTMLgt
6Elements of a JSP document
lt-- An example JSP page --gt lt_at_page
importjava.util." contentType"text/ht
ml" session"true" gt ltHTMLgt
ltTITLEgtTitlelt/TITLEgt ltBODYgt ltPgt Today
is lt! String date gt lt
date new Date().toString() gt
lt date gt lt/Pgt ltjspinclude
page"logo.jsp"/gt lt/BODYgt lt/HTMLgt
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
7Implicit Objects
- In every JSP document a number of implicit
objects can be accessed - application (javax.servlet.ServletContext)
- config (javax.servlet.ServletConfig)
- out (javax.servlet.jsp.JspWriter)
- request (javax.servlet.ServletRequest)
- response (java.servlet.ServletResponse)
- session (javax.servlet.http.HttpSession)
- exception (java.lang.Throwable)
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
8JavaBeans in JSP (1)
package org.stachoodev.examples. / / public
class Login / / private String userId,
password / / public void
setUserId(String userId) this.userId
userId / / public void
setPassword(String password) this.password
password / / public boolean
isLoginCorrect() return userId.equals(passw
ord)
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
9JavaBeans in JSP (2)
lt_at_page import"org.stachoodev.examples."
contentType"text/html" gt ltHTMLgt
ltTITLEgtLoginlt/TITLEgt ltBODYgt ltPgt lt
Login login new Login()
login.setUserId(request.getParameter("userId))
login.setPassword(request.getParameter("pa
ssword)) gt lt if
(login.isLoginCorrect()) gt Login
successful. lt else gt Login
failed. lt gt lt/Pgt lt/BODYgt lt/HTMLgt
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
10JavaBeans in JSP (3)
lt_at_page import"org.stachoodev.examples."
contentType"text/html" gt ltjspuseBean
id"login" scope"page" class"Login"/gt ltHTMLgt
ltTITLEgtLoginlt/TITLEgt ltBODYgt ltPgt lt
login.setUserId(request.getParameter("userId
")) login.setPassword(request.getParamet
er("password")) gt lt if
(login.isLoginCorrect()) gt Login
successful. lt else gt Login
failed. lt gt lt/Pgt lt/BODYgt lt/HTMLgt
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
11JavaBean instance scope
- The scope parameter of the jspuseBean action
determines the "visibility" of a JavaBean
instance - page the bean is visible only within the JSP
page containing its declaration - request the bean is visible in any JSP page
processing the same request - session the instance is accessible across the
current user session - application any JSP page can access the
JavaBean instance
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
12JavaBeans in JSP (4)
lt_at_page import"org.stachoodev.examples."
contentType"text/html" gt ltjspuseBean
id"login" scope"page" class"Login"
/gt ltjspsetProperty name"login"
property"userId" value"lt
request.getParameter("userId") gt"
/gt ltjspsetProperty name"login"
property"passowrd" value"lt
request.getParameter("password") gt" /gt ltHTMLgt
ltTITLEgtLoginlt/TITLEgt ltBODYgt ltPgt lt if
(login.isLoginCorrect()) gt Login
successful. lt else gt Login
failed. lt gt lt/Pgt lt/BODYgt lt/HTMLgt
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
13JavaBeans in JSP (5)
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
lt_at_page import"org.stachoodev.examples."
contentType"text/html" gt ltjspuseBean
id"login" scope"page" class"Login"
/gt ltjspsetProperty name"login" property""
/gt ltHTMLgt ltTITLEgtLoginlt/TITLEgt ltBODYgt
ltPgt lt if (login.isLoginCorrect()) gt
Login successful. lt else gt
Login failed. lt gt lt/Pgt
lt/BODYgt lt/HTMLgt
14Exception handling
lt_at_page contentType"text/html"
errorPage"error.jsp" gt ltHTMLgt ltTITLEgtTest
pagelt/TITLEgt ltBODYgt ltPgt lt
((String)null).length() gt lt/Pgt
lt/BODYgt lt/HTMLgt
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
15Including and forwarding
lt_at_page import"org.stachoodev.examples."
contentType"text/html" gt ltjspuseBean
id"login" scope"page" class"Login"
/gt ltjspsetProperty name"login" property""
/gt lt_at_include file"page_header.html" gt
lt if (login.isLoginCorrect()) gt
ltjspforward page"welcome.jsp" /gt lt
else gt ltjspinclude
page"forgotten.jsp" /gt lt
gt lt_at_include file"page_footer.html" gt
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
16Custom Tags (1)
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
lt_at_page import"org.stachoodev.examples."
contentType"text/html" gt lt_at_taglib
prefix"util" uri"util.tld"
gt lt_at_include file"page_header.html" gt
ltutillogin request"lt request gt"
/gt lt_at_include file"page_footer.html" gt
17Custom Tags (2)
- To implement a Custom JSP Tag
- Create the tag handler class (doStartTag(),
doEndTag(), ...) - Create the class describing the variables
accesible within the Custom Tag (optional) - Create the Tag Library Descriptor (TLD file)
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
18JSP Standard Tag Library
- A standardized library of JSP Custom Tags
- conditions, loops, enumerations
- URL transformations
- content localization
- formatting of numbers and dates
- SQL queries
- XML processing
- XSLT transformations
- validation of JSP documents
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
19JSP and Java Servlets
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
lt_at_page import"java.util." gt ltHTMLgt
ltTITLEgtTitlelt/TITLEgt ltBODYgt ltPgt
Today is lt new Date().toString() gt
lt/Pgt lt/BODYgt lt/HTMLgt
20JSP and Java Servlets
lt_at_page importjava.util." gt ltHTMLgt
ltTITLEgtJSP testlt/TITLEgt ltBODYgt ltPgt
lt! String date gt lt date
new Date().toString() gt
Today is lt date gt lt/Pgt
lt/BODYgt lt/HTMLgt
package org.apache.jsp import
java.util. import javax.servlet. //
... public class testjsp extends HttpJspBase
// begin file"/test.jsp"from(8,9)to(8,2
3) String date // end public void
_jspService(...) // ... session
pageContext.getSession() // HTML
file"/test.jsp"from(2,36)to(8,6)
out.write("\r\n\r\nltHTMLgt\r\nltTITLEgtJSP...")
// end // begin file"/test.jsp"from(9,8)
to(11,6) date new Date().toString()
// end // begin file"/test.jsp"from(14
,9)to(14,15) out.print( date ) //
end
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
21JSP Specification v2.0
- A lot of changes and improvements
- Expression Language (EL) scriptless JSP
- Tag Files
- based on Servlet Specification v2.4
- requires Java 2 Platform v1.4
- to be implemented in Tomcat 5.0
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
22Summary
- JSP vs. Java Servlets
- better, though still imperfect, code-content
separation - Tag Libraries - extensibility and component reuse
- faster development (less typing )
- development tools available
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
23Summary
- JavaServer Pages Home
- http//java.sun.com/jsp/
- JSP Standard Templates Library
- http//java.sun.com/products/jsp/jstl/
- Tomcat Reference Implementation
- http//jakarta.apache.org/tomcat
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
24Java Server PagesA technology for serving
dynamic web content
What is JSP ? Elements of a JSP
document JavaBeans and JSP Exception
handling TagLibs, JSTL JSP and Java Servlets JSP
v2.0 Summary
Thank you for your attention
- Stanislaw Osinski, stachoo_at_man.poznan.pl