Java Server Pages by Jon Pearce JSP Documents

Java Server Pages by Jon Pearce

JSP Documents • JSP docs are XHTML Documents containing: – Fixed-Template Data: FTD • HTML Components • XML markup – JSP Components: <JSP>

demo. jsp <? xml version = 1. 0? > <!DOCTYPE html PUBLIC ". . . /xhtml 1 -strict. dtd"> <html xmlns = "http: //www. w 3. org/1999/xhtml"> <head> <title> Demo </title> </head> <body> FTD <JSP>. . . </body> </html>

JSP Components <JSP> • Scriptlets <%, <%--, <%=, <%!. . . %> • Directives <%@ directive %> • Actions <jsp: action>. . . </jsp: action> <my. NS: action>. . . </my. NS: action>

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

JSP Compilation

Scriptlets • • <% (partial) statement %> <%-- comment --%> <%! declaration %> <%= expression %>

Examples of Scripting Components • <%! int counter = 0; %> • counter = <%= counter++ %> • <% if (counter > 5) { counter = 0; } %> HTML <% else { counter++; } %> ALTERNATIVE HTML • <%-- don't read this --%>

Implicit Objects • Application Scope – application • Session Scope – session • Request Scope – request • Page Scope – response, out, page. Context

Example <% String name = request. get. Parameter("name"); if (name != null) { %> <p> Hello <%= name %> </p> <% } else { %> <p> Hello John Doe </p> <% } %>

javax. servlet. jsp

The JSP to Servlet Translation public class My. JSP implements Http. Jsp. Page { //. . . public void _jsp. Service(Http. Servlet. Request request, Http. Servlet. Response response) throws IOException, Servlet. Exception { Jsp. Factory factory = Jsp. Factory. get. Default. Factory(); Page. Context page. Context = factory. get. Page. Context(. . . ); Http. Session session = page. Context. get. Session(); Jsp. Writer out = page. Context. get. Out(); Object page = this; try { // body of translated JSP goes here. . . } catch (Exception e) { out. clear(); page. Context. handle. Page. Exception(e); } finally { out. close(); factory. release. Page. Context(page. Context); } } }

Examples • clock. jsp <%= new java. util. Date() %> • script. Test. jsp Pallindrome Tester • bean. Test. jsp various counters • person. View. jsp

Standard Actions • • <jsp: action>. . . </jsp: action> include forward plugin param use. Bean set. Property get. Property

Directives • <%@ include. . . %> • <%@ taglib. . . %> • <%@ page. . . %> <%@ page error. Page = "foo. jsp" %> <%@ page session = "true" %> <%@ page language = "java" %> <%@ page extends = "java. awt. Frame" %> <%@ page import = "java. util. *" %>

Include Directive • <%@ include file = "banner. html" %>

Include Directive vs. Action • <jsp: include page = "foo. jsp" flush = "true" /> – foo. html included after each change • <%@ include file = "foo. jsp" %> – foo. html included once at compile time • Static (html) or dynamic (jsp) files can be included

forward • <jsp: forward page = "foo. jsp"> <jsp: param name = "date" value = "<%= new Date() %>" /> </jsp: forward>

use. Bean • <jsp: use. Bean id="cart" scope="session" class = "Cart. Bean" /> • Accessing Bean Properties <%= cart. get. Total() %> <jsp: get. Property name = "cart" property = "total" /> • Setting Bean Properties <% cart. set. Total(200); %> <jsp: set. Property name = "cart" property = "total" value = "200" />

Example <jsp: use. Bean id = "helper" scope = "session" class = "View. Helper" /> <% String command = request. get. Parameter("cmmd"); String content = helper. execute(command); %> <p> result = <%= content %> </p>

Custom Tag Libraries
- Slides: 21