4. JSP Scripting Elements
JSP scripting elements let you insert Java code into the servlet
that will be generated from the current JSP page. There are
three forms:
- Expressions of the form
<%= expression %>
that are evaluated and inserted into the output,
- Scriptlets of the form
<% code %>
that are inserted into the servlet's
service method, and
- Declarations of the form
<%! code %>
that are inserted into the body of the servlet class,
outside of any existing methods.
Each of these is described in more detail below.
4.1 JSP Expressions
A JSP expression is used to insert Java values directly
into the output. It has the following form:
<%= Java Expression %>
The Java expression is evaluated, converted to a string,
and inserted in the page. This evaluation is performed at
run-time (when the page is requested), and thus has full
access to information about the request. For example,
the following shows the date/time that the page was
requested:
Current time: <%= new java.util.Date() %>
To simplify these expressions, there are a number of predefined
variables that you can use. These implicit objects are discussed
in more detail later, but for the purpose of expressions, the most important
ones are:
request, the HttpServletRequest;
response, the HttpServletResponse;
session, the HttpSession associated
with the request (if any); and
out, the PrintWriter
(a buffered version of type JspWriter) used to send
output to the client.
Here's an example:
Your hostname: <%= request.getRemoteHost() %>
Finally, note that XML authors can use an alternative syntax
for JSP expressions:
<jsp:expression>
Java Expression
</jsp:expression>
Remember that XML elements, unlike HTML ones, are case sensitive.
So be sure to use lowercase.
4.2 JSP Scriptlets
If you want to do something more complex than insert a simple
expression, JSP scriptlets let you insert arbitrary
code into the servlet method that will be built to generate
the page. Scriptlets have the following form:
<% Java Code %>
Scriptlets have access to the same automatically
defined variables as expressions. So, for example,
if you want output to appear in the resultant page,
you would use the out variable.
<%
String queryData = request.getQueryString();
out.println("Attached GET data: " + queryData);
%>
Note that code inside a scriptlet gets inserted exactly
as written, and any static HTML (template text) before or after
a scriptlet gets converted to print statements. This
means that scriptlets need not contain complete Java statements,
and blocks left open can affect the static HTML outside of
the scriptlets. For example, the following JSP fragment, containing
mixed template text and scriptlets
<% if (Math.random() < 0.5) { %>
Have a <B>nice</B> day!
<% } else { %>
Have a <B>lousy</B> day!
<% } %>
will get converted to something like:
if (Math.random() < 0.5) {
out.println("Have a <B>nice</B> day!");
} else {
out.println("Have a <B>lousy</B> day!");
}
If you want to use the characters "%>" inside
a scriptlet, enter "%\>" instead. Finally,
note that the XML equivalent of <% Code %>
is
<jsp:scriptlet>
Code
</jsp:scriptlet>
4.3 JSP Declarations
A JSP declaration lets you define methods or fields that get inserted
into the main body of the servlet class (outside of the
service method processing the request). It has
the following form:
<%! Java Code %>
Since declarations do not generate any output, they are normally
used in conjunction with JSP expressions or scriptlets. For example,
here is a JSP fragment that prints out the number of times the
current page has been requested since the server booted (or the
servlet class was changed and reloaded):
<%! private int accessCount = 0; %>
Accesses to page since server reboot:
<%= ++accessCount %>
As with scriptlets, if you want to use the characters "%>",
enter "%\>" instead. Finally,
note that the XML equivalent of <%! Code %>
is
<jsp:declaration>
Code
</jsp:declaration>
NEXT
This tutorial is now available as a book: Core Servlets and JavaServer Pages by Marty Hall, published by Sun Microsystems Press.
Read all about it at CoreServlets.com
Server-Side Web Applications using Java Servlets versions 2.1/2.2 and JavaServer Pages (JSP) version 1.0: A Tutorial
© 1999-2000 Marty Hall.
All source code freely available for unrestricted use.
Created for work in the Research and Technology Development Center of the Johns Hopkins University Applied Physics Lab, for courses in the Johns Hopkins Part-Time MS Program in Computer Science, and for various industry seminars and on-site Java short courses.
Please note that this is a first draft of the tutorial, so please send corrections, comments, and suggestions to me at hall@apl.jhu.edu.
Reprinted with permission from the author. Click here to visit the original version
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|