JSP scripting elements
Scripting elements manipulate
objects and perform computations. The character sequence that precedes
a scripting element depends on the element’s type: <% for
a scriptlet, <%= for
an expression, and <%! for
a declaration. Scriptlets, expressions, declarations, and server-side
comments are all closed with the sequence %>.
Scriptlets
A scriptlet contains
a code fragment valid in the page-scripting language (usually Java,
but other languages can be defined in the page directive):
1 |
<% cart.processRequest(request); %> |
Expressions
An expression contains an expression valid in the page-scripting
language:
1 |
Value="<%= request.getParameter("amount") %>" |
Declarations
A declaration declares
variables or methods valid in the page-scripting language:
1 |
<%! Connection myconnection; String mystring; %> |
Comments
You can add two types
of comments to a JSP file:
- HTML comments optionally
contain an expression. They are sent to the client and can be viewed
in the page source:1<!-- Copyright (C) 2002 Acme Software --> - Hidden comments document the source file and are
not sent to the client:1<%-- Add new module here --%>
To insert a comment, type it in the Source view.
Inserting a scripting element
To insert a scripting element in a JSP page:
-
Open a JSP page, select the Page tab, and
right-click in the Script editor. -
From the pop-up menu, select New Script>Server>JSP
and then the delimiters for the type of scripting element you want. -
Type the script, expression, or declaration in
the Script editor.
Implicit objects
When a JSP page processes
a request, it has access to a set of implicit objects, each of which
is associated with a given scope. Other objects can be created in scripts.
These created objects have a scope attribute that defines where
the reference to that object is created and removed.
References to an object created in script are stored in the pageContext, request, session,
or application implicit object, according to
the object’s scope.
Implicit object | Description | Scope |
---|---|---|
request | The request triggering the servlet invocation. | Request |
response | The response to the request that triggered the servlet invocation. |
Page |
pageContext | The page context for this JSP. | Page |
session | The session object created for the requesting client (if any). |
Session |
application | The servlet context obtained from the servlet configuration, as in the call getservletConfig().getContext(). |
Application |
out | An object that writes to the output stream. | Page |
config | The ServletConfig instance for this JSP. |
Page |
page | The instance of this page’s implementation class that is processing the current request. A synonym for this when the programming language is Java. |
Page |
exception | The uncaught Throwable exception that caused the error page to be invoked. |
Page |
Implicit objects display on the Language tab page in the System
Tree.
Implicit objects other than the exception object
are always available within scriptlets and expressions. If the JSP
is an error page (the page directive’s isErrorPage attribute
is set to true), the exception implicit object
is also available.
You can often use an implicit object or a Web Target object
model wrapper class to obtain the same functionality. For example,
calling out.println in a server-side
event is equivalent to calling psDocument.Write.
For more information about the exception implicit object,
see “Error handling”.
For more information about server-side events, see “Writing server scripts”.
Scopes
There are four scopes for objects in a JSP application.
Scope | Description |
---|---|
Page | Accessible only in the page in which the object is created. Released when the response is returned or the request forwarded. |
Request | Accessible from pages processing the request in which the object is created. Released when the request has been processed. |
Session | Accessible from pages processing requests in the same session in which the object is created. Released when the session ends. |
Application | Accessible from pages processing requests in the same application in which the object is created. Released when the runtime environment reclaims the ServletContext. |