May 12, 2021 JSP
This section will briefly introduce the basic syntax in JSP development.
Scripts can contain any number of Java statements, variables, methods, or expressions, as long as they are valid in the scripting language.
The syntax format of the script:
<% 代码片段 %>
Alternatively, you can write an XML statement equivalent to it, as follows:
<jsp:scriptlet>
代码片段
</jsp:scriptlet>
Any text, HTML tags, and JSP elements must be written outside the script.
Here's an example, and the first JSP example of this tutorial:
<html>
<head>
<title>Hello World</title>
</head>
<body>
Hello World!<br/>
<%
out.println("Your IP address is " + request.getRemoteAddr());
%>
</body>
</html>
Note: Make sure that Apache Tomcat is installed in the C: .apache-tomcat-7.0.2 directory and that the operating environment is set up correctly.
Save the above code in the hello .jsp, and then place it in the directory of C: .apache-tomcat-7.0.2. webapps.ROOT, open the browser and enter the information in the http://localhost:8080/hello.jsp. When you run, you get the following results:
A declaration statement can declare one or more variables, methods, for later Java code. In a JSP file, you must declare these variables and methods before you can use them.
The syntax format of the JSP declaration:
<%! declaration; [ declaration; ]+ ... %>
Alternatively, you can write an XML statement equivalent to it, as follows:
<jsp:declaration>
代码片段
</jsp:declaration>
Examples of programs:
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>
A script language expression contained in a JSP expression that is first converted to String and then inserted where the expression appears.
Because the value of an expression is converted to String, you can use the expression in a line of text without having to worry about whether it is an HTML tag.
Expression elements can contain any expression that conforms to the Java language specification, but you cannot end the expression with a sign.
The syntax format of the JSP expression:
<%= 表达式 %>
Similarly, you can write an XML statement equivalent to it:
<jsp:expression>
表达式
</jsp:expression>
Examples of programs:
<html>
<head>
<title>A Comment Test</title>
</head>
<body>
<p>
Today's date: <%= (new java.util.Date()).toLocaleString()%>
</p>
</body>
</html>
When you run, you get the following results:
Today's date: 11-Sep-2013 21:24:25
JSP comments have two main functions: commenting on code and commenting out a piece of code.
Syntax format for JSP comments:
<%-- 这里可以填写 JSP 注释 --%>
Examples of programs:
<html>
<head>
<title>A Comment Test</title>
</head>
<body>
<h2>A Test of Comments</h2>
<%-- 该部分注释在网页中不会被显示--%>
</body>
</html>
When you run, you get the following results:
A Test of Comments
Grammar rules that use comments in different situations:
Grammar | Describe |
---|---|
slt;%--Note --%;gt; | JSP comments, comment content will not be sent to the browser or even compiled |
Note !-- -- | HTML comments, which you can see when you view the source code of a web page through your browser |
<\% | Represents a static and lt;% constant |
%\> | Represents a static %?gt; constant |
\' | Single quotes used in properties |
\" | Double quotes used in properties |
The JSP directive is used to set properties related to the entire JSP page.
JSP instruction syntax format:
<%@ directive attribute="value" %>
There are three instruction labels:
Instructions | Describe |
---|---|
<%@ page ... %> | Define the dependent properties of a page, such as scripting language, error page, cache requirements, and so on |
<%@ include ... %> | Contains other files |
<%@ taglib ... %> | The definition that introduces a label library can be a custom label |
JSP behavior tags use the XML syntax structure to control the servlet engine. It dynamically inserts a file, reuses JavaBean components, directs users to another page, generates related HTML for Java plug-ins, and so on.
Behavior tags have only one syntax format, which adheres strictly to XML standards:
<jsp:action_name attribute="value" />
Behavior tags are basically pre-defined functions, and the following table lists some of the available JSP behavior tags: :
Grammar | Describe |
---|---|
jsp:include | Used to include static or dynamic resources on the current page |
jsp:useBean | Find and initialize a JavaBean component |
jsp:setProperty | Set the value of the JavaBean component |
jsp:getProperty | Insert the value of the JavaBean component into output |
jsp:forward | Pass a request object containing user requests from one JSP file to another |
jsp:plugin | Used to include applets and JavaBean objects in the generated HTML page |
jsp:element | Dynamically create an XML element |
jsp:attribute | Defines the properties of dynamically created XML elements |
jsp:body | Defines the body of a dynamically created XML element |
jsp:text | Used to encapsulate template data |
The JSP supports nine automatically defined variables, which the Lakers call hidden objects. An introduction to these nine implied objects can be found in the table below:
Object | Describe |
---|---|
request | An instance of the HttpServletRequest class |
response | An instance of the HttpServletResponse class |
out | An instance of the PrintWriter class that outputs the results to a Web page |
session | An instance of the HttpSession class |
application | An instance of the ServletContext class, related to the application context |
config | An instance of the ServletConfig class |
pageContext | An instance of the PageContext class that provides access to all objects and namespaces of JSP pages |
page | Similar to the this keyword in a Java class |
Exception | The object of the Exception class, which represents the corresponding exception object on the JSP page where the error occurred |
JSP provides full support for the Java language. You can use the Java API in your JSP program and even build Java blocks of code, including judgment statements and loop statements, and so on.
If... Else block, see the following example:
<%! int day = 3; %>
<html>
<head>
<title>IF...ELSE Example</title>
</head>
<body>
<% if (day == 1 | day == 7) { %>
<p> Today is weekend</p>
<% } else { %>
<p> Today is not weekend</p>
<% } %>
</body>
</html>
When you run, you get the following results:
Today is not weekend
Now let's take a look at switch... c ase block, with if... The else block is very different, it uses out.println(), and the whole thing is in the script's label, as follows:
<%! int day = 3; %>
<html>
<head>
<title>SWITCH...CASE Example</title></head>
<body>
<%
switch(day) {
case 0:
out.println("It\'s Sunday.");
break;
case 1:
out.println("It\'s Monday.");
break;
case 2:
out.println("It\'s Tuesday.");
break;
case 3:
out.println("It\'s Wednesday.");
break;
case 4:
out.println("It\'s Thursday.");
break;
case 5:
out.println("It\'s Friday.");
break;
default:
out.println("It's Saturday.");
}
%>
</body>
</html>
After running, you get the following results:
It's Wednesday.
Loop statement
Three basic loop types of Java can be used in JSP programs: for, while, and do... while。
Let's take a look at an example of a for loop:
<%! int fontSize; %>
<html>
<head>
<title>FOR LOOP Example</title>
</head>
<body>
<%for ( fontSize = 1; fontSize <= 3; fontSize++){ %>
<font color="green" size="<%= fontSize %>">
JSP Tutorial
</font><br />
<%}%>
</body>
</html>
When you run, you get the following results:
Replace the example above with a while loop to write:
<%! int fontSize; %>
<html>
<head>
<title>WHILE LOOP Example</title>
</head>
<body>
<%while ( fontSize <= 3){ %>
<font color="green" size="<%= fontSize %>">
JSP Tutorial
</font><br />
<%fontSize++;%>
<%}%>
</body>
</html>
Run with the same results:
JSP supports all Java logic and arithmetic operators.
The following table lists the common JSP operators, with high priority:
Category | Operator | Binding |
---|---|---|
Suffix | () [] . (point operator) | Left to right |
One dollar | ++ - - ! ~ | Right to left |
Multiplicity | * / % | Left to right |
Addability | + - | Left to right |
Shift | >> >>> << | Left to right |
Relationship | > >= < <= | Left to right |
Equal/unequal | == != | Left to right |
bit with | & | Left to right |
Bit difference or | ^ | Left to right |
bit or | | | Left to right |
Logic and | && | Left to right |
Logic or | || | Left to right |
Conditional judgment | ?: | Right to left |
Assignment | = += -= *= /= %= >>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |
The JSP language defines the following constants: