Thứ Hai, 24 tháng 2, 2014

Tài liệu Session 2: Using JSP Tags docx

eACCP2003/Dynamic Pages Using JSP/ Session 2/ 5 of 45
What happens under the hood?
User
Request
Web Server/
Servlet Engine
JSP Handler Servlet
(Page Compilation Servlet)
New or
modified file?
Compile into
Servlet
Execute
Servlet
Yes
No
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 6 of 45
Various Elements in a JSP

Directives

page

Defines information that will be globally available for a
JSP

include

Used to insert text and code at JSP translation time

taglib

Defines a mechanism for extending the current set of
JSP tags
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 7 of 45
Different Attributes of the
Page Directive
Attribute Definition
language =
”<scriptingLanguage>”
This attribute tells the server about the language to be
used to complete the JSP file. At present, Java is the only
language that can be used.

extends =
”<className>"
This attribute defines the parent class that the generated
servlet will extend.

import =
”<importList>"
This attribute defines the list of packages that will be
available to this JSP. However, a comma must separate
each package.

session = "true | false" This attribute determines whether the session data will be
available to this page. The default is true.

buffer = "none | <size
in kb>"
This attribute determines whether the output stream is
buffered. The default value is 8 kb.

eACCP2003/Dynamic Pages Using JSP/ Session 2/ 8 of 45
Different Attributes of the
Page Directive – (1)
Attribute Definition
autoFlush = "true |
false"
Determines whether the output buffer will be flushed
automatically, or whether it will raise an exception
when the buffer is full. The default is true.

isThreadSafe = "true
| false"
Specifies whether page can service more than one
request at a time. The default is true.
info = ”<text>" Specifies information about the JSP, can be accessed
by Servlet.getServletInfo().

errorPage =
”<error_URL>"
This attribute represents the relative URL to the JSP
that will handle exceptions.

isErrorPage = "true |
false"
This attribute states whether or not the JSP is an
errorPage. The default is false.

contentType =
”<ctinfo>"
This attribute represents the MIME type and
character set of the response.

eACCP2003/Dynamic Pages Using JSP/ Session 2/ 9 of 45
Using page Attributes - An
example
<%@ page language = "java"
import = "java.rmi.*, java.util.*"
session = "true"
buffer = "12kb"
autoFlush = "true"
info = "page directive jsp"
errorPage = "Error.jsp"
isErrorPage = "false"
isThreadSafe = "false" %>
<HTML> <TITLE> JSP Elements </TITLE>
<HEAD> <H1> JSP Elements </H1> </HEAD>
<BODY> </BODY> </HTML>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 10 of 45
More on Directives

include directive

Syntax
<%@ include file = “relativeURLspec” %>

taglib directive

Syntax
<%@ taglib uri = “tagLibraryURI”

prefix = “tagPrefix” %>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 11 of 45
Some Important Points

While using directives, remember the
following:

Directives are messages to the JSP container, that is, the
JSP engine

Directives do not store any results in the output buffer

Directives are processed when the JSP is initialized
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 12 of 45
JSP Scripting Elements

Expressions

Scriptlets

Declarations
Scripting is a mechanism for embedding
code fragments directly in an HTML page.
The following three scripting elements are
involved in JSP scripting:
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 13 of 45
JSP Scripting Elements – (1)
Expressions are evaluated to produce a
java.lang.String type result.
Evaluated to
produce 4
<HTML>
<BODY> 2 + 2 equals <%= 2 + 2 %>
</BODY>
</HTML>
Example:
Syntax: <%= expression %>
eACCP2003/Dynamic Pages Using JSP/ Session 2/ 14 of 45
JSP Scripting Elements – (2)
Scriptlets contain code that is valid for the
language specified in the language directive. Java
is the only language that is presently available.
Scriptlet
<HTML> <BODY>
<% out.println("HELLO WORLD!!"); %>
</BODY> </HTML>
Example:
Syntax: <% scriptlet_source; %>

Không có nhận xét nào:

Đăng nhận xét