JavaServer Pages Pocket Reference_ Server-Side Java Development [Bergsten 2001-08-05].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
,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
,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
,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
,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
Plik z chomika:
musli_com
Inne pliki z tego folderu:
Big Java Late Objects [Horstmann 2012-02-01].pdf
(167477 KB)
Data Structures_ Abstraction and Design using Java (2nd ed.) [Koffman & Wolfgang 2010-01-26].pdf
(190252 KB)
Big Java Early Objects (5th ed.) [Horstmann 2013-01-04].pdf
(145099 KB)
Data Abstraction and Problem Solving with Java_ Walls and Mirrors (3rd ed.) [Prichard & Carrano 2010-10-30] (photocopier quality).pdf
(110506 KB)
A Little Java, a Few Patterns [Felleisen & Friedman 1997-12-19].pdf
(14847 KB)
Inne foldery tego chomika:
3D Design - Programming
ActionScript
Actionscript - Flash - Flex - Air
Ada
ADO
Zgłoś jeśli
naruszono regulamin