JSP syntax
This section will briefly introduce the basic syntax in JSP development.
Script program
A script program 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 script program:
<% 代码片段 %>
Alternatively, you can write its equivalent XML statement, like the following:
<jsp:scriptlet> 代码片段 </jsp:scriptlet>
Any text, HTML tag, JSP elements must be written outside the script program.
An example is given below, which is also 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: Please ensure that Apache Tomcat has been installed in C:\apache- tomcat-7.0.2 directory and the running environment has been set correctly.
Save the above code in hello.jsp, then place it in the C:\apache-tomcat-7.0.2\webapps\ROOT directory, open the browser and enter http:/ in the address bar /localhost:8080/hello.jsp. After running, we get the following results:
Chinese encoding problem
If we want to display Chinese normally on the page, we need to add the following code to the head of the JSP file: <>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
After analysis, we will modify the above program to:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> Hello World!<br/> <% out.println("你的 IP 地址 " + request.getRemoteAddr()); %> </body> </html>
So that Chinese can be displayed normally.
JSP declaration
A declaration statement can declare one or more variables and methods for use by subsequent Java code. In a JSP file, you must declare these variables and methods before you can use them.
The syntax format of JSP declaration:
<%! declaration; [ declaration; ]+ ... %>
Alternatively, you can also write its equivalent XML statement, like the following:
<jsp:declaration> 代码片段 </jsp:declaration>
Program example:
<%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %>
JSP expression
The scripting language expression contained in a JSP expression is first converted into String and then inserted into the place where the expression appears.
Since the value of the expression will be converted to String, you can use the expression in a text line regardless of whether it is an HTML tag.
The expression element can contain any expression that conforms to the Java language specification, but a semicolon cannot be used to end the expression.
The syntax format of JSP expression:
<%= 表达式 %>
Similarly, you can also write the equivalent XML statement:
<jsp:expression> 表达式 </jsp:expression>
Program example:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <p> 今天的日期是: <%= (new java.util.Date()).toLocaleString()%> </p> </body> </html>
After running, the following results are obtained:
今天的日期是: 2016-6-25 13:40:07
JSP comments
JSP comments have two main functions: commenting on the code and commenting out a certain section of code.
The syntax format of JSP comments:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> </head> <body> <%-- 该部分注释在网页中不会被显示--%> <p> 今天的日期是: <%= (new java.util.Date()).toLocaleString()%> </p> </body> </html>
After running, the following results are obtained:
今天的日期是: 2016-6-25 13:41:26
The syntax rules for using comments in different situations:
grammar | describe |
---|---|
<%-- Comments --%> | JSP comments, the comment content will not be sent to the browser or even compiled |
<!-- Comments --> | HTML comments, you can see the comment content when viewing the source code of the web page through the browser |
<\% | Represents static <% constant |
Represents static %> constant | |
Single quotes used in attributes | |
Double quotes used in attributes |
JSP directive is used to set properties related to the entire JSP page.
JSP instruction syntax format:
<%@ directive attribute="value" %>
There are three instruction tags:
describe | |||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Define the dependency attributes of the page, such as scripting language, error page, cache requirements, etc. | |||||||||||||||||||||||||||||||||||||||||||||||||
Contains other files | |||||||||||||||||||||||||||||||||||||||||||||||||
Introduce the definition of the tag library, which can be a custom tag |
Syntax | Description | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Used to include static or dynamic resources in the current page | |||||||||||||||||||||||||||||||||||||||||||||||||
Find and initialize a JavaBean component | |||||||||||||||||||||||||||||||||||||||||||||||||
Set the value of JavaBean component | |||||||||||||||||||||||||||||||||||||||||||||||||
Insert the value of the JavaBean component into the output | |||||||||||||||||||||||||||||||||||||||||||||||||
Pass a request object containing the user's request from one JSP file to another file | |||||||||||||||||||||||||||||||||||||||||||||||||
Used to include Applet and JavaBean objects in the generated HTML page | |||||||||||||||||||||||||||||||||||||||||||||||||
Dynamically create an XML element | |||||||||||||||||||||||||||||||||||||||||||||||||
Define attributes of dynamically created XML elements | |||||||||||||||||||||||||||||||||||||||||||||||||
Defines the body of a dynamically created XML element | |||||||||||||||||||||||||||||||||||||||||||||||||
Used to encapsulate template data |
Object | Description | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
request | HttpServletRequestInstance of class | ||||||||||||||||||||||||||||||||||||||||||||||||
HttpServletResponseInstance of class | |||||||||||||||||||||||||||||||||||||||||||||||||
Instance of PrintWriter | class, used to output the results to the web page | ||||||||||||||||||||||||||||||||||||||||||||||||
HttpSession | Instance of class | ||||||||||||||||||||||||||||||||||||||||||||||||
Instance of ServletContext | class, related to application context | ## config||||||||||||||||||||||||||||||||||||||||||||||||
ServletConfig | Instance of classpageContext | ||||||||||||||||||||||||||||||||||||||||||||||||
PageContext | Instance of class, provides access to all objects and namespaces of JSP pagespage | ||||||||||||||||||||||||||||||||||||||||||||||||
An object of the Exception | |||||||||||||||||||||||||||||||||||||||||||||||||
Exception | class, representing the corresponding exception object in the JSP page where the error occurred
Category | Operator | Associativity |
---|---|---|
Suffix | () [] . (dot operator) | Left to right |
One yuan | ++ - - ! ~ | Right to left |
Multiplyability | * / % | Left to right |
Additivity | + - | Left to right |
Shift | >> >>> << | Left to right |
relation | > >= < <= | Left to right |
Equal/unequal | == != | Left to right |
Bits and | & | Left to right |
Bit XOR | ^ | Left to right |
bit or | | | Left to right |
Logic and | && | Left to right |
Logical OR | || | Left to right |
Conditional judgment | ?: | Right to left |
Assignment | = += -= *= /= %= >>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |
JSP literals
JSP language defines the following literals:
Boolean: true and false;
Integer type (int): the same as in Java;
Float type (float): the same as in Java;
String: Start and end with single or double quotes;
Null: null.