Title: Java Server Pages
1Java Server Pages
2JSP Documents
- JSP docs are XHTML Documents containing
- Fixed-Template Data FTD
- HTML Components
- XML markup
- JSP Components ltJSPgt
3demo.jsp
lt?xml version 1.0?gtlt!DOCTYPE html PUBLIC
".../xhtml1-strict.dtd"gtlthtml xmlns
"http//www.w3.org/1999/xhtml"gtltheadgt lttitlegt
Demo lt/titlegt lt/headgtltbodygt FTD ltJSPgt FTD
ltJSPgt FTD ltJSPgt...lt/bodygtlt/htmlgt
4JSP Components ltJSPgt
- Scriptlets
- lt, lt--, lt, lt! ... gt
- Directives
- lt_at_ directive gt
- Actions
- ltjsp actiongt ... lt/jspactiongt
- ltmyNSactiongt ... lt/myNSactiongt
5JSP Facts
- The container compiles a JSP to a servlet the
first time it is served. - Use JSPs when ratio of FTD to Dynamic Content is
high - JSPs decouple design from programming
- JSP HTML containing Java
- Servlet Java containing HTML
- JSPs replace views
6JSP Compilation
7Scriptlets
- lt (partial) statement gt
- lt-- comment --gt
- lt! declaration gt
- lt expression gt
8Examples of Scripting Components
- lt! int counter 0 gt
- counter lt counter gt
- lt if (counter gt 5) counter 0 gtHTMLlt
else counter gtALTERNATIVE HTML - lt-- don't read this --gt
9Implicit Objects
- Application Scope
- application
- Session Scope
- session
- Request Scope
- request
- Page Scope
- response, out, page, pageContext
10Example
lt String name request.getParameter("name")
if (name ! null) gt ltpgt Hello lt name
gt lt/pgtlt else gt ltpgt Hello John Doe
lt/pgtlt gt
11javax.servlet.jsp
12The JSP to Servlet Translation
public class MyJSP implements HttpJspPage //
... public void _jspService(HttpServletRequest
request, HttpServletResponse response) throws
IOException, ServletException JspFactory
factory JspFactory.getDefaultFactory()
PageContext pageContext factory.getPageContext
(...) HttpSession session
pageContext.getSession() JspWriter out
pageContext.getOut() Object page this
try // body of translated JSP
goes here ... catch (Exception e)
out.clear() pageContext.handlePageE
xception(e) finally
out.close() factory.releasePageContext(p
ageContext)
13Examples
- clock.jsp
- lt new java.util.Date() gt
- scriptTest.jsp
- Pallindrome Tester
- beanTest.jsp
- various counters
- personView.jsp
14Standard Actions
- ltjspactiongt ... lt/jspactiongt
- include
- forward
- plugin
- param
- useBean
- setProperty
- getProperty
15Directives
- lt_at_ include ... gt
- lt_at_ taglib ... gt
- lt_at_ page ... gt
- lt_at_ page errorPage "foo.jsp" gt
- lt_at_ page session "true" gt
- lt_at_ page language "java" gt
- lt_at_ page extends "java.awt.Frame" gt
- lt_at_ page import "java.util." gt
16Include Directive
- lt_at_ include file "banner.html" gt
17Include Directive vs. Action
- ltjspinclude page "foo.jsp" flush "true" /gt
- foo.html included after each change
- lt_at_ include file "foo.jsp" gt
- foo.html included once at compile time
- Static (html) or dynamic (jsp) files can be
included
18forward
- ltjspforward page "foo.jsp"gt ltjspparam
name "date" value "lt new Date() gt"
/gtlt/jspforwardgt
19useBean
- ltjspuseBean id"cart" scope"session" class
"CartBean" /gt - Accessing Bean Properties
- lt cart.getTotal() gt
- ltjspgetProperty name "cart" property "total"
/gt - Setting Bean Properties
- lt cart.setTotal(200) gt
- ltjspsetProperty name "cart" property "total"
value "200" /gt
20Example
ltjspuseBean id "helper" scope "session"
class "ViewHelper" /gt lt String command
request.getParameter("cmmd")String content
helper.execute(command)gt ltpgt result lt
content gt lt/pgt
21Custom Tag Libraries