FAQ


Advertisement



What are JSP scripting elements?

JSP scripting elements enable 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 output,
  • Scriptlets of the form <% code %> that are inserted into the servlets service method, and
  • Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods.

Expression

The Expression element contains a Java expression that returns a value. This value is then written to the HTML page. The Expression tag can contain any expression that is valid according to the Java Language Specification. This includes variables, method calls than return values or any object that contains a toString() method.

Syntax

<%= 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 client host name and the date/time that the page was requested:

<html>
...
<body>
Your hostname : <%=request.getRemoteHost()%><br>
Current time : <%= new java.util.Date() %>
...
</body>
</html>

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.

Scriptlet

The 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 JSP page.
  • Write expressions valid in the page scripting language.
  • Use any of the implicit objects or any object declared with a <jsp:useBean> element.
  • Write any other statement valid in the scripting language used in the JSP page.

Any text, HTML tags, or JSP elements you write must be outside the scriptlet. For example,

<HTML>
<BODY>
<%
// This scriptlet declares and initializes "date"
java.util.Date date = new java.util.Date();
%>
Hello! The time is now
<%
out.println( date );
out.println( "<BR>Your machine's address is " );
out.println( request.getRemoteHost());
%>
</BODY>
</HTML>

Scriptlets are executed at request time, when the JSP container processes the request. If the scriptlet produces output, the output is stored in the out object.

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>

Declaration

A declaration can consist of either methods or variables, static constants are a good example of what to put in a declaration.

The JSP you write turns into a class definition. All the scriptlets you write are placed inside a single method of this class. You can also add variable and method declarations to this class. You can then use these variables and methods from your scriptlets and expressions.

You can use declarations to declare one or more variables and methods at the class level of the compiled servlet. The fact that they are declared at class level rather than in the body of the page is significant. The class members (variables and methods) can then be used by Java code in the rest of the page.

Syntax

<%! declaration; [ declaration;]+...%>

When you write a declaration in a JSP page, remember these rules:

  • You must end the declaration with a semicolon (the same rule as for a Scriptlet, but the opposite of an Expression).
    <% int i = 0;%>
  • You can already use variables or methods that are declared in packages imported by the page directive, without declaring them in a declaration element.
  • 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.
    <% int i = 0; long l = 5L; %>

For example,

<%@ page import="java.util.*" %>
<HTML>
<BODY>
<%!
Date getDate()
{
System.out.println( "In getDate() method" );
return new Date();
}
%>
Hello! The time is now <%= getDate() %>
</BODY>
</HTML>

A declaration has translation unit scope, so it is valid in the JSP page and any of its static include files. A static include file becomes part of the source of the JSP page and is any file included with an include directive or a static resouce included with a <jsp:include> element. The scope of a declaration does not include dynamic resources included with <jsp:include>.

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>


Printer-friendly version Printer-friendly version | Send this 
article to a friend Mail this to a friend

Previous Next vertical dots separating previous/next from contents/index/pdf Contents

  |   |