A JavaServer Page may be broken down into the following pieces:
* static data such as HTML
* JSP directives such as the include directive
* JSP scripting elements and variables
* JSP actions
* custom tags with correct library
Declaration<%! declaration; [ declaration; ]+ ... %>
Example
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>
Explain
A declaration declares one or more variables or methods that you can use in JavaTM code later in the JSP file. You must declare the variable or method before you use it in the JSP file.
You can declare any number of variables or methods within one declaration element, as long as you end each declaration with a semicolon. The declaration must be valid in the Java programming language.
Expression<%= expression %>
Example
The map file has
<%= map.size() %> entries.
Good guess, but nope. Try
<%= numguess.getHint() %>.
Description
An expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within a line of text, whether or not it is tagged with HTML, in a JSP file.
When you write expressions in a JSP file, remember these points:
* You cannot use a semicolon to end an expression (however, the same expression within a scriptlet requires the semicolon; see Scriptlet).
* The expression element can contain any expression that is valid according to the Java Language Specification.
Scriptlet<% code fragment %>
Example
<%
String name = null;
if (request.getParameter("name") == null) {
%>
<%@ include file="error.html" %>
<%
} else {
foo.setName(request.getParameter("name"));
if (foo.getName().equalsIgnoreCase("integra"))
name = "acura";
if (name.equalsIgnoreCase( "acura" )) {
%>
Description
A scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language.
Within a scriptlet, you can do any of the following:
* Declare variables or methods to use later in the file (see also Declaration).
* Write expressions valid in the page scripting language (see also Expression).
* Use any of the implicit objects or any object declared with a
element.
* Write any other statement valid in the scripting language used in the JSP page (if you use the Java programming language, the statements must conform to the Java Language Specification).
Any text, HTML tags, or JSP elements you write must be outside the scriptlet.
Include Directive
<%@ include file="relativeURL" %>
Example
include.jsp:
<--html>
<-head><-title>An Include Test<-/title><-/head>
<-body bgcolor="white">
<-font color="blue">
The current date and time are
<%@ include file="date.jsp" %>
<-/font>
<-/body>
<-/html>
date.jsp:
<%@ page import="java.util.*" %>
<%= (new java.util.Date() ).toLocaleString() %>
Displays in the page:
The current date and time are
Aug 30, 1999 2:38:40
Description
The <%@ include %> directive inserts a file of text or code in a JSP file at translation time, when the JSP file is compiled. When you use the <%@ include %> directive, the include process is static. A static include means that the text of the included file is added to the JSP file. The included file can be a JSP file, HTML file, or text file. If the included file is a JSP file, its JSP elements are parsed and their results included (along with any other text) in the JSP file.
You can only use include to include static files. This means that the parsed result of the included file is added to the JSP file where the <%@ include %> directive is placed. Once the included file is parsed and included, processing resumes with the next line of the calling JSP file.
The included file can be an HTML file, a JSP file, a text file, or a code file written in the Java programming language. Be careful, though, that the included file does not contain <-html>, <-/html>, <-body>, or <-/body> tags. Because the entire content of the included file is added at that location in the JSP file, these tags would conflict with the same tags in the calling JSP file, causing an error.
Some of the behaviors of the <%@ include %> directive depend on the particular JSP container you are using, for example:
* The included file might be open and available to all requests, or it might have security restrictions.
* The JSP page might be recompiled if the included file changes.
Page Directive
<%@ page
[ language="java" ]
[ extends="package.class" ]
[ import="{package.class | package.*}, ..." ]
[ session="true | false" ]
[ buffer="none | 8kb | sizekb" ]
[ autoFlush="true | false" ]
[ isThreadSafe="true | false" ]
[ info="text" ]
[ errorPage="relativeURL" ]
[ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ]
[ isErrorPage="true | false" ]
%>
Example
<%@ page import="java.util.*, java.lang.*" %>
<%@ page buffer="5kb" autoFlush="false" %>
<%@ page errorPage="error.jsp" %>
Description
The <%@ page %> directive applies to an entire JSP file and any of its static include files, which together are called a translation unit. A static include file is a file whose content becomes part of the calling JSP file. The <%@ page %> directive does not apply to any dynamic include files; see for more information.
You can use the <%@ page %> directive more than once in a translation unit, but you can only use each attribute, except import, once. Because the import attribute is similar to the import statement in the Java programming language, you can use a <%@ page %> directive with import more than once in a JSP file or translation unit.
No matter where you position the <%@ page %> directive in a JSP file or included files, it applies to the entire translation unit. However, it is often good programming style to place it at the top of the JSP file.
Attribute
# language="java"
The scripting language used in scriptlets, declarations, and expressions in the JSP file and any included files. In this release, the only allowed value is java.
# extends="package.class"
The fully qualified name of the superclass of the Java class file this JSP file will be compiled to. Use this attribute cautiously, as it can limit the JSP container's ability to provide a specialized superclass that improves the quality of the compiled file.
# import="{package.class | package.* }, ..."
A comma-separated list of Java packages that the JSP file should import. The packages (and their classes) are available to scriptlets, expressions, and declarations within the JSP file. If you want to import more than one package, you can specify a comma-separated list after import or you can use import more than once in a JSP file.
The following packages are implicitly imported, so you don't need to specify them with the import attribute:
java.lang.*
javax.servlet.*
javax.servlet.jsp.*
javax.servlet.http.*
You must place the import attribute before the element that calls the imported class.
# session="true | false"
Whether the client must join an HTTP session in order to use the JSP page. If the value is true, the session object refers to the current or new session.
If the value is false, you cannot use the session object or a element with scope=session in the JSP file. Either of these usages would cause a translation-time error.
The default value is true.
# buffer="none | 8kb | sizekb"
The buffer size in kilobytes used by the out object to handle output sent from the compiled JSP page to the client Web browser. The default value is 8kb. If you specify a buffer size, the output is buffered with at least the size you specified.
# autoFlush="true | false"
Whether the buffered output should be flushed automatically when the buffer is full. If set to true (the default value), the buffer will be flushed. If set to false, an exception will be raised when the buffer overflows. You cannot set autoFlush to false when buffer is set to none.
# isThreadSafe="true | false"
Whether thread safety is implemented in the JSP file. The default value is true, which means that the JSP container can send multiple, concurrent client requests to the JSP page. You must write code in the JSP page to synchronize the multiple client threads. If you use false, the JSP container sends client requests one at a time to the JSP page.
# info="text"
A text string that is incorporated verbatim into the compiled JSP page. You can later retrieve the string with the Servlet.getServletInfo() method.
# errorPage="relativeURL"
A pathname to a JSP file that this JSP file sends exceptions to. If the pathname begins with a /, the path is relative to the JSP application's document root directory and is resolved by the Web server. If not, the pathname is relative to the current JSP file.
# isErrorPage="true | false"
Whether the JSP file displays an error page. If set to true, you can use the exception object in the JSP file. If set to false (the default value), you cannot use the exception object in the JSP file.
# contentType="mimeType [ ;charset=characterSet ]" | "text/html;charset=ISO-8859-1"
The MIME type and character encoding the JSP file uses for the response it sends to the client. You can use any MIME type or character set that are valid for the JSP container. The default MIME type is text/html, and the default character set is ISO-8859-1.
Taglib Directive
<%@ taglib uri="URIToTagLibrary" prefix="tagPrefix" %>
Example
<%@ taglib uri="http://www.jspcentral.com/tags" prefix="public" %>
.
.
Description
The <%@ taglib %> directive declares that the JSP file uses custom tags, names the tag library that defines them, and specifies their tag prefix.
Here, the term custom tag refers to both tags and elements. Because JSP files can be converted to XML, it is important to understand the relationship of tags and elements. A tag is simply a short piece of markup that is part of a JSP element. A JSP element is a unit of JSP syntax that has an XML equivalent with a start tag and an end tag. An element can also contain other text, tags, or elements. For example, a jsp:plugin element always has a start tag and a end tag and may have a element and a element.
You must use a <%@ taglib %> directive before you use the custom tag in a JSP file. You can use more than one <%@ taglib %> directive in a JSP file, but the prefix defined in each must be unique.
The technique for creating custom tags is described in the JavaServer Pages Specification for version 1.1.