Using Scripting ElementsAt some point, you will probably want to add some good, old-fashioned programming to your JSP files. The JSP tags are powerful and encapsulate tasks that would be difficult or time-consuming to program. But even so, you will probably still want to use scripting language fragments to supplement the JSP tags. The scripting languages that are available to you depend on the JSP engine you are using. With Sun's JSP reference implementation, you must use the JavaTM programming language for scripting, but other vendors' JSP engines may include support for other scripting languages). How To Add ScriptingFirst, you'll need to know a few general rules about adding scripting elements to a JSP source file:
The Difference Between <%, <%=, and <%!Declarations, expressions, and scriptlets have similar syntax and usage, but also some important differences. Let's explore the similarities and differences here, with some examples. Declarations (between <%! and %> tags) contain one or more variable or method declarations that end or are separated by semicolons: <%! int i = 0; %> You must declare a variable or method in a JSP page before you use it in the page. The scope of a declaration is usually a JSP file, but if the JSP file includes other files with the include directive, the scope expands to cover the included files as well. Expressions (between <%= and %> tags) can contain any language expression that is valid in the page scripting language, but without a semicolon: <%= Math.sqrt(2) %> The definition of a valid expression is up to the scripting language. When you use the Java language for scripting, what's between the expression tags can be any expression defined in the Java Language Specification. The parts of the expression are evaluated in left-to-right order. One key difference between expressions and scriptlets (which are described next and appear between <% and %> tags) is that a semicolon is not allowed within expression tags, even if the same expression requires a semicolon when you use it within scriptlet tags. Scriptlets (between <% and %> tags) allow you to write any number of valid scripting language statements, like this: <% Remember that in a scriptlet you must end a language statement with a semicolon if the language requires it. When you write a scriptlet, you can use any of the JSP implicit objects or classes imported by the page directive, declared in a declaration, or named in a <jsp:useBean> tag. The Number Guess GameThe Number Guess game is fun and makes good use of scriptlets and expressions, as well as using the knowledge of HTML forms you gained in the last example. About to Guess a Number ![]() Example CodeDisplaying the Number Guess Screen (numguess.jsp)
Handling the Guess (NumberGuessBean.java)
Using Scripting Elements in a JSP File
The file numguess.jsp is an interesting example of the use of scripting elements, because it is structured as you might structure a source file, with a large You are not required to write scriptlets mingled with HTML and JSP tags, as shown in numguess.jsp. Between the <% and %> tags, you can write as many lines of scripting language code as you want. In general, doing less processing in scriptlets and more in components like servlets or Beans makes your application code more reusable and portable. Nonetheless, how you write your JSP application is your choice, and Sun's JSP 1.0 reference implementation specifies no limit on the length of a scriptlet. Mingling Scripting Elements with TagsWhen you mingle scripting elements with HTML and JSP tags, you must always end a scripting element before you start using tags and then reopen the scripting element afterwards, like this: At first, this may look a bit strange, but it ensures that the scripting elements are transformed correctly when the JSP source file is compiled. When Are the Scripting Elements Executed?A JSP source file is processed in two stages-HTTP translation time and request processing time. At HTTP translation time, which occurs when a user first loads a JSP page, the JSP source file is compiled to a Java class, usually a Java servlet. The HTML tags and as many JSP tags as possible are processed at this stage, before the user makes a request. Request processing time occurs when your user clicks in the JSP page to make a request. The request is sent from the client to the server by way of the request object. The JSP engine then executes the compiled JSP file, or servlet, using the request values the user submitted. When you use scripting elements in a JSP file, you should know when they are evaluated. Declarations are processed at HTTP translation time and are available to other declarations, expressions, and scriptlets in the compiled JSP file. Expressions are also evaluated at HTTP translation time. The value of each expression is converted to a String and inserted in place in the compiled JSP file. Scriptlets, however, are evaluated at request processing time, using the values of any declarations and expressions that are made available to them. How To Run the ExampleThe instructions given here use a UNIX-style pathname. If you are working on Windows, use the same pathname with the proper separator. [Top] [Prev] [Next] [Bottom] Copyright © 1999, Sun Microsystems, Inc. All rights reserved. | ||||||||||||||||||||||||
|
| ||||||||||||