Introduction to JSP (4)

  • 2020-05-05 11:43:14
  • OfStack

Use the script
In some places, you might want to add some good, mature programs to your JSP page. The JSP tags are powerful, but some of the work is laborious and difficult. At this point you can supplement the JSP tag with a scripting language segment.
The JSP engine used supports scripting languages. The JSP reference for SUN states that scripts must be written in Java, but other third party JSP engines allow scripting in other languages.
How do I add script
First, you must understand some basic rules for adding script elements to the JSP page 1. Define the script with the Page directive on the JSP page (the default value is Java, which is usually not defined)
2. Declare the syntax < %! ... % > Declare variables and methods (functions).
3. Expression syntax < % =... % > Define the scripting language expression
4, script syntax < %...... % > You can manipulate declarations, expressions, and other types of legal code snippets in the page scripting language.
5. Be sure to add %> at the end Label
Declarations, expressions, scripts are similar, but there are some differences. Let's use some examples to illustrate the similarities and differences.
The statement < %! ... % > Contains one or more variables and methods separated by a semicolon.
Example: < %! Int I=0 ; % >
< %! Int a, b ; double c ; % >
< %! Circle a = new circle(2.0) ; % >
You must declare
before using variables and methods on a page The scope declared is usually an JSP page, but if the page contains other pages using the INCLUDE directive, the scope should be extended to the included page.
Expression < % =... % > You can include any valid language expression on the page without a semicolon.
Example: < %= Math.sqrt(2) % >
< %= item[I] % >
< %= a+b+c % >
< %= new java.util.date() % >
One key difference between expressions and scripts is that you don't need semicolons. You must put a semicolon if you want to use expressions in your script.
Script < %...... % > Allows you to write any number of scripting languages
Example: < % String name=null ;
If (request.getParmeter("name")==null{
% >
Remember that you must end your script with a semicolon.
Number guessing game
The number guessing game is fun, and you can learn a lot about expressions here.
Code
Home screen for display (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 > < /head >
< 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 >

Operating procedures (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();
}
public void setGuess(String guess) {
numGuesses++;
int g;
try {
g = Integer.parseInt(guess);
}
catch (NumberFormatException e) {
g = -1;
}
if (g == answer) {
success = true;
}
else if (g == -1) {
hint = "a number next time";
}
else if (g < answer) {
hint = "higher";
}
else if (g > answer) {
hint = "lower";
}
}
public boolean getSuccess() {
return success;
}
public String getHint() {
return "" + hint;
}
public int getNumGuesses() {
return numGuesses;
}
public void reset() {
answer = Math.abs(new Random().nextInt() % 100)
+ 1;
success = false;
numGuesses = 0;
}
}

Use the script
in the JSP file numguess.jsp is a very interesting example of scripting. You see the structure is actually a very large IF... ELSE structure, but most of the clauses are written in HTML, which looks like a large program segment.
However, you don't have to use the HTML and JSP tags to write scripts like numguess.jsp. In < % and % >Between tags, you can write as many lines of scripting code as you want. In general, use servlets or Beans with as few scripts as possible. On the other hand, how to write JSP depends on your habits and hobbies. I do not force you to use any one method. The JSP specification for SUN does not specify the length of the script.

Combine the script
with tags When writing scripts using the HTML and JSP tags, be sure to "seal" the tags before and after. I don't understand, for example:
< % } else { % > < ! -- close the JSP TAB when using -->

. Now use the JSP tag...

< % } % > < ! A: so you understand? ! - >
This may seem a bit odd at first, but it ensures a successful conversion of the script when your JSP file is compiled.

So, when does the script execute?
The processing of an JSP original file is divided into two phases: the compilation time of HTTP and the processing time of the request.

When HTTP is compiled, when the user reads the JSP page for the first time, the source code for JSP is compiled to CLASS, which is usually servlet. The HTML and JSP tags are processed at this time, and no request has been submitted by the user before.

The request processing time is when the user submits a request in the JSP page, and the request is passed from the client to the server 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 on JSP page, you must know when they are executed. Declarations are processed during the HTTP compilation phase, and other scripts and expressions are available when compiling JSP files. The expression is also executed when HTTP is compiled. The value of the expression is converted to a string and inserted into the JSP file to be compiled together. Scripts are also available during the request phase.

How do I run the example
All the paths I've given you are in UNIX style. If you use Windows, change it to Windows style path
1. The number guessing game is already installed when TOMCAT or JSWDK are installed.
2,.jsp and.html files in.. / jswdk - / examples/num
1.0.1 3,.java and.class files in.. / jswdk - / examples WEB 1.0.1 - INF/jsp/bean/
num 4, open the browser, http: / / the name of the machine/examples/jsp/num/numguess jsp

Related articles: