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>
[Link]
<?xml version = 1.0?>
<!DOCTYPE html PUBLIC ".../[Link]">
<html xmlns = "[Link]
<head> <title> Demo </title> </head>
<body>
FTD <JSP> FTD <JSP> FTD <JSP>...
</body>
</html>
JSP Components <JSP>
Scriptlets
<%, <%--, <%=, <%! ... %>
Directives
<%@ directive %>
Actions
<jsp: action> ... </jsp:action>
<myNS:action> ... </myNS: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
client container
[Link]
compile [Link] : MyJSP
request
HTML
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, pageContext
Example
<%
String name = [Link]("name");
if (name != null) {
%>
<p> Hello <%= name %> </p>
<%
} else {
%>
<p> Hello John Doe </p>
<% } %>
[Link]
<<Interface>>
Servlet
init()
service()
destroy()
<<Interface>>
JSPPage
jspDestroy()
jspInit() contains implicit
objects + actions
PageContext
<<Interface>>
HttpJspPage getAttribute()
setAttribute()
_jspService() 1 1
include()
container
generated from MyJSP
JSP
The JSP to Servlet Translation
public class MyJSP implements HttpJspPage {
// ...
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
JspFactory factory = [Link]();
PageContext pageContext = [Link](...);
HttpSession session = [Link]();
JspWriter out = [Link]();
Object page = this;
try {
// body of translated JSP goes here ...
} catch (Exception e) {
[Link]();
[Link](e);
} finally {
[Link]();
[Link](pageContext);
}
}
}
Examples
[Link]
<%= new [Link]() %>
[Link]
Pallindrome Tester
[Link]
various counters
[Link]
Standard Actions
<jsp:action> ... </jsp:action>
include
forward
plugin
param
useBean
setProperty
getProperty
Directives
<%@ include ... %>
<%@ taglib ... %>
<%@ page ... %>
<%@ page errorPage = "[Link]" %>
<%@ page session = "true" %>
<%@ page language = "java" %>
<%@ page extends = "[Link]" %>
<%@ page import = "[Link].*" %>
Include Directive
<%@ include file = "[Link]" %>
Include Directive vs. Action
<jsp:include page = "[Link]" flush = "true" />
[Link] included after each change
<%@ include file = "[Link]" %>
[Link] included once at compile time
Static (html) or dynamic (jsp) files can be included
forward
<jsp:forward page = "[Link]">
<jsp:param
name = "date"
value = "<%= new Date() %>"
/>
</jsp:forward>
useBean
<jsp:useBean id="cart" scope="session" class = "CartBean" />
Accessing Bean Properties
<%= [Link]() %>
<jsp:getProperty name = "cart" property = "total" />
Setting Bean Properties
<% [Link](200); %>
<jsp:setProperty name = "cart" property = "total" value = "200" />
Example
<jsp:useBean id = "helper" scope = "session"
class = "ViewHelper" />
<% String command =
[Link]("cmmd");
String content = [Link](command);
%>
<p> result = <%= content %> </p>
Custom Tag Libraries