Scripting elements Directives
JSP directives do not directly produce any visible output but instead
tell the engine what to do with the rest of the JSP page. They are always
enclosed within the <%@
%> tag.
The include directive lets you separate your content into more manageable
elements, such as those for including a common page header or footer.
The page included could be a fixed HTML page or more JSP content:
<%@ include file="filename.jsp" %>
Directives are messeges to the JSP container and have the following syntax:
<%@ directive attributte="value" %>
Allthough this syntax is consise and easy to type, it is not XML compatible.
Also, directives do not produce any output. Below is a list of the standard
directives.
page
The page directive is always at the top of JSP pages. For examples:
<%@ page import="java.util.Date" %>
<%@ page errorPage="errorPage.jsp" %>
<%@ page session="true" %>
Or you can put them together:
<%@ page language="java"
extends="package.class"
import="java.util.Date"
session="true"
buffer="8kb"
autoFlush="true"
isThreadSafe="true"
info="Sample info about this page..."
errorPage="errorPage.jsp"
contentType="text/html; charset=iso-8859-1"
isErrorPage="false"
%>
The errorPage attribute should contain a relative URL to the error page
that will handle an unhandled error for this page. An error page must
have the isErrorPage attribute set to "true" in order to have
access to the exception object. The details of each attribute are as follows:
language: Defines the scripting language to be used. In JSP 1.1,
the only defined and required scripting language value for this attribute
is java.
extends: The value is a Java class name, that names the superclass
of the class to which this JSP page is transformed.
import: The default import list is java.lang.*, javax.servlet.*,
javax.servlet.jsp.* and javax.servlet.http.*. This value is currently
only defined when the value of the language directive is java.
session: Indicates that the page requires participation in an (http)
session. Default is true.
buffer: Specifies the buffering model for the initial "out"
JspWriter to handle content output from the page. The default buffer size
is 8kb.
autoFlush: Specifies whether the buffered output should be flushed
automatically (true value) when the buffer is filled, or whether an exception
should be raised (false value) to indicate buffer overflow. The default
is true. Note: it is illegal to set autoFlush to false when buffer=none.
isThreadSafe: Indicates the level of thread safety implemented
in the page.
info: Defines an arbitrary string that is incorporated into the
translated page, that can be subsequently obtained from the page's implementation
of the Servlet.getServletInfo() method.
isErrorPage: Default value is false.
errorPage: indicates that if an error ocurrs, where to find the
error-response page.
contentType: 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 is: contentType="text/html; charset=iso-8859-1"
include
You can include a pure HTML file or a JSP file into your current page
by using include directive:
<%@ include file="includePage.html" %>
Note: this directive only allows the relative URL (file) to the file
to be included.
taglib
The taglib directive is used to define a tag library and prefix for the
custom tags used in the JSP page. The syntax is:
<%@ taglib uri="URIToTagLibrary"
prefix="tagPrefix" %>
uri - Either an absolute URI or a relative URI. Prefixes jsp:, jspx:,
java:, javax:, servlet:, sun:, and sunw: are reserved. Empty prefixes
are illegal in this version of the specification.
Example:
<%@ taglib uri="http://www.abcsite.com/ssmmtags"
prefix="ssmm" %>
...
<ssmm:ppp>
...
</ssmm:ppp>
Declarations
You must declare a variable or method in a JSP page before you
use it in the page. Declarations do not produce any output to the out
stream. They are initialized at the same time the page is initialized
and are made available to other declarations, scriptlets and expressions.
Declarations use the following syntax:
<%! ... %>
Example:
<%! int a, b; double c; %>
Note: Always end variable declarations with a semicolon
Expressions
With this element, the results of the expression are displayed on the
output page. Expressions use the following syntax:
<%=
%>
Examples:
<%= "Hello World" %>
<%= m + n + w %>
<%= new java.util.Date() %>
Not: do not include semicolons.
Scriptlets
Scriptlet uses the following syntax:
<%
%>
You can put any Java source code within <%
%> tags. Scriptlets
are not limited to one line of source code. For example:
<%
String userName = null;
if (request.getParameter("userName") ")== null) {
%>
Another example:
<% for (int i=1; i<=5; i++) { %>
<font size="<%= i %>"><%= Hello world%></font>
<% } %>
Note: you must end a statement with a semicolon. If you use quotes and
some other symbols, you should note that: only the following quoting and
escaping conventions are processed in JSP.
|
single quote |
double quote |
back slash |
closing tag |
opening tag |
original symbol |
' |
" |
\ |
%> |
<% |
processed symbol |
\' |
\" |
\\ |
%\> |
<\% |
Comments
If you allow users to view the comments in your source code, you can
include HTML comments in your files:
<!--- This is regular HTML comments ... --->
But if you don't want users to be able to see your comments, you can
use JSP comments and embed them within the <%--
--%> tag:
<%-- This comment can not be seen by clientsfor --%>
|