Using scripts
In some places, you probably want to add some good, mature program to your JSP page, JSP label although very powerful, but the completion of some work is more difficult. You can then use the Scripting Language section to supplement the JSP tags.
The JSP engine used supports scripting languages, and Sun's JSP reference notes that scripts must be written using the Java programming language, but other Third-party JSP engines allow scripting to be written in other languages.
How to increase the script
First, you must understand some of the basic rules for adding script elements to JSP pages
1, in the JSP page to define the script with page directives (the default value is Java, generally do not need to define)
2, declaration syntax <%!......%> declare variables and methods (functions).
3. Expression syntax <%=......%> definition scripting language expressions
4. Script syntax 〈%......%> can manipulate declarations, expressions, and other types of legitimate code snippets in the page scripting language.
5, must at the end Add%> label
declarations, expressions, scripts used are somewhat similar, but there are some differences let's use some examples to tell the same and different points.
The declaration <%!......%> 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 you use variables and methods in a page, you must declare
The scope of the declaration is usually a JSP page, but if the page contains additional pages using the include directive, the scope should be extended to the included page.
The expression <%=......%> can contain any legitimate language expressions in the page without semicolons.
Example: <%= math.sqrt (2)%>
<%= Item[i]%>
<%= A+b+c%>
<%= new Java.util.date ()%>
One of the key differences between expressions and scripts is that you don't need semicolons. If you need to use an expression in your script, you must add a semicolon.
Script <%......%> allow you to write any number of scripting languages
Example: <% String name=null;
If (Request.getparmeter ("name") ==null{
%>
Remember that you must use a semicolon to end the script.
Guess number Game
Guessing the number game is very interesting, and from here you can learn a lot about the use of expressions.
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 "%>
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>
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;
}
}
Using scripts in JSP files
Numguess.jsp is a very interesting example of scripting, you see his structure is actually a very large if ... else structure, but a clause is written in HTML and looks like a large program segment.
But you don't necessarily have to write scripts with HTML and JSP tags like numguess.jsp. Between <% and%> tags, you can write any number of lines of scripting code, in general, as little as possible to use the script to process the program, and as much as possible using servlets or beans, so that your program will look very clear, clear. Then again, how to write JSP still depends on your habits and hobbies, I do not have to use any method, Sun's JSP detailed description of the book does not specify the length of the script.
Combining Scripts with tags
When using HTML and JSP tags to write scripts, pay attention to the label before and after do not forget, must be "sealed" good. Don't understand, for instance:
<%} else {%> <!--when using JSP tags, close--> first
... At this time with the JSP tag bar ...
<%}%> <!--so you know what you're looking for? -->
This may seem a bit strange at first, but it ensures that your JSP files are compiled with a successful conversion of the script.
So when does the script execute?
A JSP original file processing is divided into two stages: one is the HTTP compile time, one is the request processing time.
When HTTP compiles, when the user first reads the JSP page, the JSP's original code is compiled into class, usually a servlet. HTML tags and JSP tags were processed at the same time, before the user had any requests submitted.
The request processing time is when the user submits a request in the JSP page, when the request is uploaded by the client to the server side, and 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. Declarations have been processed during the HTTP compilation phase, and other scripts, expressions are also available when compiling JSP files. The expression was also executed when HTTP was compiled. The value of the expression is converted to a string inserted into the JSP file to be compiled together. In fact, in the request phase, the script is also available.
How to run an example
All I'm giving you is a UNIX-style path, and if you're using Windows, change to a Windows style path
1, guess the number of games in the pack tomcat or JSWDK has been installed.
2. JSP and. html files in the. In/jswdk-1.0.1/examples/num
3. Java and. class files in. In/jswdk-1.0.1/examples/web-inf/jsp/bean/num
4, open the browser, http://machine name/examples/jsp/num/numguess.jsp
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.