O'Reilly - JavaServer Pages Pocket Reference.pdf

(1075 KB) Pobierz
,jsppr.9600
,jsppr.9600 Page 1 Friday, September 7, 2001 2:51 PM
JavaServer Pages Pocket
Reference
The JavaServer Pages™ (JSP) specification is built on top of
the Java™ servlet specification and is intended to provide for
better separation of the presentation (e.g., HTML markup)
and business logic (e.g., database operations) parts of web
applications. JSP is supported by all major web and applica-
tion servers. A partial listing of JSP-compliant products is
available at Sun Microsystems’ JSP web page:
http://java.sun.com/products/jsp/
A JSP page is a web page that contains both static content,
such as HTML, and JSP elements for generating the parts
that differ with each request, as shown in Figure 1. The
default filename extension for a JSP page is .jsp .
Everything in the page that’s not a JSP element is called tem-
plate text . Template text can be in any format, including
HTML, WML, XML, and even plain text. Since HTML is by
far the most common web page language in use today, most
of the descriptions and examples in this text are HTML-
based. You should be aware, though, that JSP has no depen-
dency on HTML. Template text is not interpreted at all; it’s
passed straight through to the browser. JSP is therefore well-
suited to serve any markup language.
When a JSP page request is processed, the static template
text and the dynamic content generated by the JSP ele-
ments are merged, and the result is sent as the response to
the client.
1
374511198.183.png 374511198.194.png 374511198.205.png 374511198.216.png 374511198.001.png 374511198.012.png 374511198.023.png 374511198.034.png 374511198.045.png 374511198.056.png 374511198.067.png 374511198.078.png 374511198.089.png 374511198.100.png 374511198.111.png 374511198.122.png 374511198.133.png 374511198.144.png 374511198.147.png 374511198.148.png 374511198.149.png 374511198.150.png 374511198.151.png 374511198.152.png 374511198.153.png 374511198.154.png 374511198.155.png 374511198.156.png 374511198.157.png 374511198.158.png 374511198.159.png 374511198.160.png 374511198.161.png 374511198.162.png 374511198.163.png 374511198.164.png 374511198.165.png 374511198.166.png 374511198.167.png 374511198.168.png 374511198.169.png 374511198.170.png 374511198.171.png 374511198.172.png 374511198.173.png 374511198.174.png
,jsppr.9600 Page 2 Friday, September 7, 2001 2:51 PM
<%@ page language="java" contentType="text/html" %>
JSP element
<html>
<body bgcolor="white">
<jsp:useBean
id="userInfo"
class="com.ora.jsp.beans.userinfo.UserInfoBean">
<jsp:setProperty name="userInfo" property="*"/>
</jsp:useBean>
template text
JSP element
The following information was saved:
<ul>
<li>User Name:
template text
<jsp:getProperty name="userInfo"
property="userName"/>
<li>Email Address:
JSP element
template text
<jsp:getProperty name="userInfo"
property="emailAddr"/>
</ul>
</body>
</html>
JSP element
template text
Figure 1. Template text and JSP elements
JSP Processing
Before a JSP page is sent to a browser, the server must pro-
cess all the JSP elements it contains. This processing is per-
formed by a web container , which can be either a native part
of a web server or a separate product attached to the web
server. The web container turns the JSP page into a Java serv-
let, then executes the servlet.
Converting the JSP page into a servlet (known as the JSP page
implementation class ) and compiling the servlet take place in
the translation phase . The web container initiates the transla-
tion phase for a JSP page automatically when the first request
for the page is received. The translation phase takes a bit of
time, of course, so users may notice a slight delay the first
time they request a JSP page. The translation phase can also
2
|
JavaServer Pages Pocket Reference
374511198.175.png 374511198.176.png 374511198.177.png 374511198.178.png 374511198.179.png 374511198.180.png 374511198.181.png 374511198.182.png 374511198.184.png 374511198.185.png 374511198.186.png 374511198.187.png 374511198.188.png 374511198.189.png 374511198.190.png 374511198.191.png 374511198.192.png 374511198.193.png 374511198.195.png 374511198.196.png 374511198.197.png 374511198.198.png 374511198.199.png 374511198.200.png 374511198.201.png 374511198.202.png 374511198.203.png 374511198.204.png 374511198.206.png 374511198.207.png 374511198.208.png 374511198.209.png 374511198.210.png 374511198.211.png 374511198.212.png 374511198.213.png 374511198.214.png 374511198.215.png 374511198.217.png 374511198.218.png 374511198.219.png 374511198.220.png 374511198.221.png 374511198.222.png
,jsppr.9600 Page 3 Friday, September 7, 2001 2:51 PM
be initiated explicitly, to avoid hitting the first user with the
delay. This is referred to as precompilation .
The web container is also responsible for invoking the JSP
page implementation class to process each request and gen-
erate responses. This is called the request processing phase .
The two phases are illustrated in Figure 2.
hello.jsp
Server with
JSP Container
2
Translation
phase
Client
helloServlet.java
1
GET /hello.jsp
3
Generate
HTTP/1.0 200 OK
6
Compile
4
<html>Hello!</html>
5
Request
processing
phase
helloServlet.class
Figure 2. JSP page translation and processing phases
As long as the JSP page remains unchanged, the translation
phase is skipped. When the page is modified, it goes through
the translation phase again.
Let’s lookat a simple example. In the tradition of program-
ming books, we start with an application that writes “Hello
World” (with a twist—it also shows the current time on the
server):
<html>
<head>
<title>Hello World</title>
</head>
<body>
<h1>Hello World</h1>
It's <%= new java.util.Date().toString() %> and all
is well.
</body>
</html>
This JSP page produces the result shown in Figure 3.
JSP Processing
|
3
374511198.223.png 374511198.224.png 374511198.225.png 374511198.226.png 374511198.002.png 374511198.003.png 374511198.004.png 374511198.005.png 374511198.006.png 374511198.007.png 374511198.008.png 374511198.009.png 374511198.010.png 374511198.011.png 374511198.013.png 374511198.014.png 374511198.015.png 374511198.016.png 374511198.017.png 374511198.018.png 374511198.019.png 374511198.020.png 374511198.021.png 374511198.022.png 374511198.024.png 374511198.025.png 374511198.026.png 374511198.027.png 374511198.028.png 374511198.029.png 374511198.030.png 374511198.031.png 374511198.032.png 374511198.033.png 374511198.035.png 374511198.036.png 374511198.037.png 374511198.038.png 374511198.039.png 374511198.040.png 374511198.041.png 374511198.042.png 374511198.043.png 374511198.044.png 374511198.046.png 374511198.047.png 374511198.048.png
,jsppr.9600 Page 4 Friday, September 7, 2001 2:51 PM
Figure 3. The output from the Hello World page
This is as simple as it gets. The code represented by the JSP
element (which we have highlighted in bold in the code) is
executed, and the result is combined with the regular HTML
in the page. In this case the JSP element is a scripting ele-
ment with Java code for writing the current date and time.
There are three types of JSP elements: directives, actions, and
scripting elements. The following sections describe the ele-
ments of each type.
Directive Elements
Directive elements specify information about the page itself;
information that doesn’t differ between requests for the page.
Examples are the scripting language used in the page,
whether or not session tracking is required, and the name of
the page that will be used to report any errors.
The general directive syntax is:
<%@ directiveName attr1 =" value1 " attr2 =" value2 " %>
You can use single quotes instead of double quotes around
the attribute values. The directive name and all attribute
names are case-sensitive.
Include Directive
The include directive includes a file, merging its content with
the including page before the combined result is converted to
4
|
JavaServer Pages Pocket Reference
374511198.049.png 374511198.050.png 374511198.051.png 374511198.052.png 374511198.053.png 374511198.054.png 374511198.055.png 374511198.057.png 374511198.058.png 374511198.059.png 374511198.060.png 374511198.061.png 374511198.062.png 374511198.063.png 374511198.064.png 374511198.065.png 374511198.066.png 374511198.068.png 374511198.069.png 374511198.070.png 374511198.071.png 374511198.072.png 374511198.073.png 374511198.074.png 374511198.075.png 374511198.076.png 374511198.077.png 374511198.079.png 374511198.080.png 374511198.081.png 374511198.082.png 374511198.083.png 374511198.084.png 374511198.085.png 374511198.086.png 374511198.087.png 374511198.088.png 374511198.090.png 374511198.091.png 374511198.092.png 374511198.093.png 374511198.094.png 374511198.095.png 374511198.096.png 374511198.097.png
,jsppr.9600 Page 5 Friday, September 7, 2001 2:51 PM
a JSP page implementation class. It supports the attribute
described in Table 1.
Table 1. Attributes for the include directive
Name
Default
Description
file
No default
A page- or context-relative URI path for the file
to include.
A single page can contain multiple include directives.
Together, the including page and all included pages form a
JSP translation unit .
Example:
<%@ include file="header.html" %>
Page Directive
The page directive defines page-dependent attributes, such as
scripting language, error page, and buffering requirements. It
supports the attributes described in Table 2.
Table 2. Attributes for the page directive
Name Default Description
autoFlush true Set to true if the page buffer should be flushed
automatically when it’s full or to false if an
exception should be thrown when it’s full.
buffer
8kb Specifies the buffer size for the page. The value
must be expressed as the size in kilobytes
followed by kb , or be the keyword none to
disable buffering.
contentType text/
html
The MIME type for the response generated by
the page, and optionally the charset for the
source page (e.g., text/
html;charset=Shift_JIS ).
errorPage No
default
A page- or context-relative URI path to which
the JSP page will forward users if an exception is
thrown by code in the page.
Directive Elements
|
5
374511198.098.png 374511198.099.png 374511198.101.png 374511198.102.png 374511198.103.png 374511198.104.png 374511198.105.png 374511198.106.png 374511198.107.png 374511198.108.png 374511198.109.png 374511198.110.png 374511198.112.png 374511198.113.png 374511198.114.png 374511198.115.png 374511198.116.png 374511198.117.png 374511198.118.png 374511198.119.png 374511198.120.png 374511198.121.png 374511198.123.png 374511198.124.png 374511198.125.png 374511198.126.png 374511198.127.png 374511198.128.png 374511198.129.png 374511198.130.png 374511198.131.png 374511198.132.png 374511198.134.png 374511198.135.png 374511198.136.png 374511198.137.png 374511198.138.png 374511198.139.png 374511198.140.png 374511198.141.png 374511198.142.png 374511198.143.png 374511198.145.png 374511198.146.png
Zgłoś jeśli naruszono regulamin