Java Server Pages - PowerPoint PPT Presentation

About This Presentation
Title:

Java Server Pages

Description:

Java Server Pages by Jon Pearce JSP Documents JSP docs are XHTML Documents containing: Fixed-Template Data: FTD HTML Components XML markup JSP Components: demo ... – PowerPoint PPT presentation

Number of Views:34
Avg rating:3.0/5.0
Slides: 22
Provided by: Pea36
Learn more at: http://www.cs.sjsu.edu
Category:
Tags: java | pages | server

less

Transcript and Presenter's Notes

Title: Java Server Pages


1
Java Server Pages
  • by Jon Pearce

2
JSP Documents
  • JSP docs are XHTML Documents containing
  • Fixed-Template Data FTD
  • HTML Components
  • XML markup
  • JSP Components ltJSPgt

3
demo.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
4
JSP Components ltJSPgt
  • Scriptlets
  • lt, lt--, lt, lt! ... gt
  • Directives
  • lt_at_ directive gt
  • Actions
  • ltjsp actiongt ... lt/jspactiongt
  • ltmyNSactiongt ... lt/myNSactiongt

5
JSP 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

6
JSP Compilation
7
Scriptlets
  • lt (partial) statement gt
  • lt-- comment --gt
  • lt! declaration gt
  • lt expression gt

8
Examples 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

9
Implicit Objects
  • Application Scope
  • application
  • Session Scope
  • session
  • Request Scope
  • request
  • Page Scope
  • response, out, page, pageContext

10
Example
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
11
javax.servlet.jsp
12
The 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)
13
Examples
  • clock.jsp
  • lt new java.util.Date() gt
  • scriptTest.jsp
  • Pallindrome Tester
  • beanTest.jsp
  • various counters
  • personView.jsp

14
Standard Actions
  • ltjspactiongt ... lt/jspactiongt
  • include
  • forward
  • plugin
  • param
  • useBean
  • setProperty
  • getProperty

15
Directives
  • 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

16
Include Directive
  • lt_at_ include file "banner.html" gt

17
Include 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

18
forward
  • ltjspforward page "foo.jsp"gt ltjspparam
    name "date" value "lt new Date() gt"
    /gtlt/jspforwardgt

19
useBean
  • 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

20
Example
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
21
Custom Tag Libraries
Write a Comment
User Comments (0)
About PowerShow.com