Thursday, April 28, 2011

few more JSP SYNTAX AND SEMANTICS


Directives
Directives are messages to a JSP container. They do not send output to a client,
but are used to define page attributes, which custom tag libraries use and which
other pages include. All directives use the following syntax.
<%@ directive {attribute="value"}* %>
Directives may optionally have extra whitespace after the <%@ and before the
%>. There are three different JSP directives for use on a page page, taglib, and
include.

<%@ page %>
The  page directive provides page-specific information to a JSP container. This
information includes settings such as the type of content the JSP is to produce,
the default scripting language of the page, and code libraries to import for use.
Multiple page directives may be used on a single JSP or pages included via JSP as
long as no specific attribute, except import, occurs more than once. Attributes for
the page directive are as follows.
language The language attribute defines the scripting language to be used by
scriptlets, expressions, and declarations occurring in the JSP. The only defined
and required scripting language value for this attribute is java. Different con-
tainers may provide additional language support; however,it is uncommon to see
a JSP use a language other than Java for scripting elements.
In this book it is always assumed that the language appearing in scripting ele-
ments is Java (as that is currently the only defined language). Translation of a
scripting element using Java code fragments into real Java code is easily done by
any developer with previous Java experience. Understanding how and why a
scriptlet example works is assumed to be intuitive because it is identical to under-
standing the Java equivalent.
extends The extends attribute value is a fully qualified Java programming lan-
guage class name that names the superclass of the class to which this JSP is trans-
formed. The extends attribute is analogous to the extends keyword used when
authoring a Java class. This attribute should be used sparingly as it prevents a
container from using a pre-built optimized class.
import The  import attribute describes the types that are available for use in
scripting elements. The value is the same as in an import declaration in the Java
programming language, with multiple packages listed with either a fully qualified
Java programming language-type names or a package name followed by .*,
denoting all the public types declared in that package.
The default import list is java.lang.*, javax.servlet.*, javax.servlet.
jsp.*, and  javax.servlet.http.*. These packages can be assumed to be
available by default with every JSP. The import attribute is currently only defined
for use when the value of the language directive is java.
session The session attribute indicates that the page requires participation in
an HTTP session. If true, then the implicit scripting variable session references
the current/new session for the page. If false, then the page does not participate
in a session and the session implicit scripting variable is unavailable, and any

reference to it within the body of the JSP page is illegal and results in a fatal trans-
lation error. The default value of the session attribute is true.
buffer The  buffer attribute specifies the buffering model for the initial out
implicit scripting variable to handle content output from the page. If the
attribute’s value is none, then there is no buffering and output is written directly
through to the appropriate ServletResponse PrintWriter. Valid values for the
attribute are sizes specified in kilobytes, with the kb suffix being mandatory. If a
buffer size is specified, then output is buffered with a buffer size of at least the
specified, value. Depending upon the value of the autoFlush attribute, the con-
tents of this buffer are either automatically flushed or an exception is thrown
when overflow would occur. The default value of the buffer attribute is 8kb.
autoFlush The autoFlush attribute specifies whether buffered output should
be flushed automatically when the buffer is filled, or whether an exception
should be raised to indicate buffer overflow.A value of true indicates automatic
buffer flushing and a value of false throws an exception. The default value of
the  autoFlush attribute is true. It is illegal to set the autoFlush attribute to
false when the value of the buffer attribute is none.
isThreadSafe The isThreadSafe attribute indicates the level of thread safety
implemented in the page. If the value is false, then the JSP container shall dis-
patch multiple outstanding client requests, one at a time, in the order they were
received, to the page implementation for processing by having the generated
Servlet implement SingleThreadModel. If the attribute’s value is true, then the
JSP container may choose to dispatch multiple client requests to the page simul-
taneously. The default value of the isThreadSafe attribute is true.
isErrorPage The  isErrorPage attribute indicates if the current JSP page is
intended to be an error page for other JSP. If true, then the implicit scripting
variable  exception is defined, and its value is a reference to the offending
Throwable object. If the  isErrorPage attribute value is false, then the
exception implicit variable is unavailable, and any reference to it within the body
of the JSP page is illegal and will result in a fatal translation error. The default
value of the isErrorPage attribute is false.
errorPage The errorPage attribute defines a relative URL to a resource in the
Web Application to which any Java programming language Throwable object
thrown but not caught by the page implementation is forwarded for error pro-
cessing. The Throwable object is transferred to the error page by binding the object
reference to request scope with the name javax.servlet.jsp.jspException.

If the value of the autoFlush attribute is true, and if the contents of the
initial JspWriter have been flushed to the ServletResponse output stream, then
any subsequent attempt to dispatch an uncaught exception from the offending
page to an errorPage may fail.
contentType The contentType attribute defines the character encoding for the
JSP page, and for the response of the JSP page and the MIME type for the
response of the JSP page. The default value of the contentType attribute is
text/html with ISO-8859-1 character encoding for regular JSP syntax and UTF-
8 encoding for JSP in XML syntax.
pageEncoding The pageEncoding attribute defines the character encoding for
the JSP.The default value of the pageEncodingattribute is ISO-8859-1for regular
JSP and UTF-8 for JSP in XML syntax.
isScriptingEnabled The  isScriptingEnabled attribute determines if
scripting elements are allowed for use. The default value (true) enables scriptlets,
expressions, and declarations. If the attribute’s value is set to false, a translation-
time error will be raised if the JSP uses any scriptlets, expressions (non-EL), or
declarations. This attribute is helpful for creating ‘scriptless’ JSP and can also be
set using the web.xml scripting-enabled element.
isELEnabled The isELEnabled attribute determines if JSP EL expressions used
in the JSP are to be evaluated. The default value of the attribute is true, meaning
that expressions, ${...}, are evaluated as dictated by the JSP specification. If the
attribute is set to false, then expressions are not evaluated but rather treated as
static text.
The page directive is by default set to accommodate the most common use of
JSP: to make dynamic HTML pages. When creating a simple JSP, it is rarely
needed to specify any of the page directive attributes except in cases where extra
code libraries are needed for scripting elements or when producing XML
content, which is happening more and more.


No comments:

Post a Comment