Script
In some places, you may want to add some good, mature Program In your JSP page, although JSP labels are powerful, it is still difficult to complete some work. In this case, you can use the Script Language Segment to add JSP labels.
The JSP Engine Used supports the scripting language. For Sun's JSP reference, you must use the Java programming language to compile the script, however, other third-party JSP engines allow scripting in other languages.
How to add scripts
First, you must understand some basic rules for adding script elements to JSP pages.
1. Use the page command to define the script on the JSP page (the default value is Java, which is generally not required)
2. Declaration syntax <% !...... %> Declare variables and methods (functions ).
3. expression syntax <% = ...... %> Define a script language expression
4. Script syntax <% ...... %> Operation declarations, expressions, and Other types that are valid Code Segment in the Page scripting language.
5. Add %> tag at the end
Declarations, expressions, and scripts are similar, but there are also some differences. Let's use some examples to illustrate the similarities and differences.
Statement <% !...... %> Contains one or more variables and Methods Separated by semicolons.
Example: <%! Int I = 0; %>
<%! Int A, B; double C; %>
<%! Circle A = new circle (2.0); %>
Before using variables and methods on the page, you must declare
The declared range is usually a JSP page, but if the page uses the include command to include other pages, the range strain can be extended to the included page.
Expression <% = ...... %> You can include any legal language expressions on the page without semicolons.
Example: <% = math. SQRT (2) %>
<% = Item [I] %>
<% = A + B + C %>
<% = New java. util. Date () %>
A key difference between an expression and a script is that a semicolon is not required. If you need to use an expression in the script, you must add a plus sign.
Script <% ...... %> Any number of scripting languages you can write
Example: <% string name = NULL;
If (request. getparmeter ("name") = null {
%>
Remember to end with a semicolon in the script.
Guess digital games
It is very interesting to guess digital games, and you can also learn a lot about expression usage from here.
Code
Display the main screen (numguess. jsp)
<! --
Number guess game
Written by Jason Hunter, CTO, K & A Software
Jasonh@kasoftware.com, http://www.servlets.com
Copyright 1999, K & A Software
Distributed by Sun Microsystems with permission
-->
<% @ Page import = "num. numberguessbean" %>
<JSP: usebean id = "numguess" class = "num. numberguessbean" Scope = "session"/>
<JSP: setproperty name = "numguess" property = "*"/>
<HTML>
<Head> <title> Number guess </title> <Body bgcolor = "white">
<Font size = 4>
<% IF (numguess. getsuccess () {%>
Congratulations! You got it.
And after just <% = numguess. getnumguesses () %> tries. <p>
<% Numguess. Reset (); %>
Care to <a href = "numguess. jsp"> try again </a>?
<%} Else if (numguess. getnumguesses () = 0) {%>
Welcome to the number guess game. <p>
I'm thinking of a number between 1 and 100. <p>
<Form method = get>
What's your guess? <Input type = text name = guess>
<Input type = submit value = "Submit">
</Form>
<%} Else {%>
Good guess, but nope. Try <B> <% = numguess. gethint () %> </B>.
You have made <% = numguess. getnumguesses () %> guesses.
<P> I'm thinking of a number between 1 and 100.
<P> <form method = get>
What's your guess? <Input type = text name = guess>
<Input type = submit value = "Submit">
</Form>
<% }%>
</Font>
</Body>
</Html>
Procedure (numberguessbean. Java)
// Number guess game
// Written by Jason Hunter, CTO, K & A Software
// Jasonh@kasoftware.com, http://www.servlets.com
// Copyright 1999, K & A Software
// Distributed by Sun Microsystems with permission
package num;
Import Java. util. *;
public class numberguessbean {
int answer;
Boolean success;
string hint;
int numguesses;
Public numberguessbean () {
Reset ();
}< br> Public void setguess (string guess) {
numguesses ++;
int g;
try {
G = integer. parseint (guess);
}< br> catch (numberformatexception e) {
G =-1;
}< br> If (G = answer) {
success = true;
}< br> else if (G =-1) {
hint = "a number next time ";
}< br> else if (G hint = "higher";
}< br> else if (G> answer) {
hint = "lower";
}< BR >}< br> Public Boolean getsuccess () {
return success;
}< br> Public String gethint () {
return "" + hint;
}< br> Public int getnumguesses () {
return numguesses;
}< br> Public void reset () {
answer = math. ABS (new random (). nextint () % 100)
+ 1;
success = false;
numguesses = 0;
}< BR >}
Use scripts in JSP files
Numguess. jsp is a very interesting example of script writing. You can see that its structure is actually a very large if ...... Else structure, but many clauses are written in HTML, and it looks like a large program segment.
However, you do not have to use HTML and JSP tags to write scripts like numguess. jsp. Between the <% and %> labels, you can write any number of lines of script code. In general, use as few scripts as possible to process programs, and use servlets or beans as much as possible, in this way, your program looks very clear and clear. Then again, you have to write JSP based on your habits and hobbies. I don't have to force you to use any method. Sun's JSP detailed instruction does not specify the length of the script.
Combine scripts with tags
When using HTML and JSP tags to write scripts, be sure to "seal" The front and back labels. I don't understand. Let's take an example:
<%} Else {%> <! -- Close the JSP tag first -->
... Use JSP labels at this time...
<% }%> <! -- So you should understand it ?! -->
This may seem a bit strange at the beginning, but it ensures that the script is successfully converted when your JSP file is compiled.
So when will the script be executed?
The processing of the original JSP file is divided into two phases: one is the HTTP Compilation Time and the other is the request processing time.
During HTTP compilation, when the user reads the JSP page for the first time, the original code of JSP is compiled into a class, usually servlet. The HTML Tag and JSP tag are processed at the same time. Before that, the user has not submitted any request.
The request processing time is when the user submits a request on the JSP page, then the request is sent to the server by the client by the request object, the JSP Engine executes the compiled JSP file or servlet based on the value submitted by the user.
When you use scripts in JSP pages, you must know when they will be executed. The Declaration has been processed in the HTTP compilation phase. Other scripts and expressions are also available when compiling JSP files. The expression is also executed during HTTP compilation. The expression value is converted into a string that is inserted into the JSP file and compiled together. In fact, scripts can also be used in the request stage.
Example
I now provide all the paths in the UNIX style. If you use Windows, change them to the Windows style path.
1. Assume that a digital game has been installed when tomcat or jswdk is installed.
22.16.jspand .html file in ../jswdk-1.0.1/examples/num
3. The. Java and. class files are in ../jswdk-1.0.1/examples/WEB-INF/JSP/bean/num
4. Open a browser, http: // machine name/examples/JSP/num/numguess. jsp