This section will briefly introduce the basic syntax in JSP development.
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 the script program:
<% code snippet%>
Alternatively, you can write the equivalent XML statement, like this:
<jsp:scriptlet> code snippet</jsp:scriptlet>
Any text, HTML tags, and 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 the C:apache-tomcat-7.0.2 directory and the running environment has been set up correctly.
Save the above code in hello.jsp, then place it in the C:apache-tomcat-7.0.2webappsROOT directory, open the browser and enter http://localhost:8080/hello in the address bar .jsp. After running, I get the following results:
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 write the equivalent XML statement, like this:
<jsp:declaration>Code snippet</jsp:declaration>
Program example:
<%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %>
The scripting language expression contained in a JSP expression is first converted into a String and then inserted into the place where the expression appears.
Since the value of the expression is converted to a String, you can use the expression in a line of text 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 terminate the expression.
Syntax format of JSP expression:
<%= expression%>
Likewise, you can write the equivalent XML statement:
<jsp:expression> expression</jsp:expression>
Program example:
<html> <head><title>A Comment Test</title></head> <body><p> Today's date: <%= (new java.util.Date()).toLocaleString()%></ p></body> </html>
After running, I get the following results:
Today's date: 11-Sep-2013 21:24:25
JSP comments have two main functions: commenting on the code and commenting out a certain section of code.
The syntax format of JSP comments:
<%-- You can fill in JSP comments here--%>
Program example:
<html> <head><title>A Comment Test</title></head> <body> <h2>A Test of Comments</h2> <%-- This part of the comments will not be displayed on the web page-- %> </body> </html>
After running, I get the following results:
A Test of Comments
Grammar rules for using comments in different situations:
grammar | describe |
---|---|
<%--Comment--%> | 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 directives are used to set properties related to the entire JSP page.
JSP instruction syntax format:
<%@ directive attribute="value" %>
There are three directive tags:
instruction | describe |
---|---|
<%@ page ... %> | Define the dependency attributes of the page, such as script language, error page, cache requirements, etc. |
<%@ include ... %> | include other files |
<%@ taglib ... %> | Introduce the definition of the tag library, which can be a custom tag |
JSP behavior tags use XML syntax structures to control the servlet engine. It can dynamically insert a file, reuse JavaBean components, guide the user to another page, generate relevant HTML for Java plug-ins, and more.
Behavior tags have only one syntax format, which strictly adheres to the XML standard:
<jsp:action_name attribute="value" />
Behavior tags are basically some pre-defined functions. The following table lists some of the available JSP behavior tags::
grammar | describe |
---|---|
jsp:include | Used to include static or dynamic resources in the current page |
jsp:useBean | Find and initialize a JavaBean component |
jsp:setProperty | Set the value of JavaBean component |
jsp:getProperty | Insert the value of the JavaBean component into the output |
jsp:forward | Pass a request object containing the user's request from one JSP file to another |
jsp:plugin | Used to include Applet and JavaBean objects in generated HTML pages |
jsp:element | Dynamically create an XML element |
jsp:attribute | Define attributes of dynamically created XML elements |
jsp:body | Defines the body of a dynamically created XML element |
jsp:text | Used to encapsulate template data |
JSP supports nine automatically defined variables, known as implicit objects. An introduction to these nine implicit objects is shown in the table below:
object | describe |
---|---|
request | Instance of HttpServletRequest class |
response | Instance of HttpServletResponse class |
out | An instance of the PrintWriter class, used to output results to a web page |
session | Instance of HttpSession class |
application | An instance of the ServletContext class, related to the application context |
config | Instance of ServletConfig class |
pageContext | An instance of the PageContext class, providing access to all objects and namespaces of the JSP page |
page | Similar to this keyword in Java classes |
Exception | An object of the Exception class represents the corresponding exception object in the JSP page where the error occurred. |
JSP provides full support for the Java language. You can use Java API in JSP programs and even create Java code blocks, including judgment statements and loop statements, etc.
If…else block, see this 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>
After running, I get the following results:
Today is not weekend
Now let's look at the switch...case block. It is very different from the if...else block. It uses out.println() and is entirely contained in the script's tag, like this:
<%! 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, the following results are obtained:
It's Wednesday.
Loop statement
Java's three basic loop types can be used in JSP programs: for, while, and do...while.
Let's 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>
After running, I get the following results:
JSP TutorialJSP Tutorial JSP TutorialRewrite the above example using a while loop:
<%! 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>
I get the same result after running:
JSP TutorialJSP Tutorial JSP TutorialJSP supports all Java logical and arithmetic operators.
The following table lists common JSP operators, in order of priority:
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 |
Bit AND | & | left to right |
Bit XOR | ^ | left to right |
bit or | | | left to right |
logical AND | && | left to right |
logical or | || | left to right |
Conditional judgment | ?: | right to left |
Assignment | = += -= *= /= %= >>= <<= &= ^= |= | right to left |
comma | , | left to right |
JSP language defines the following constants:
Boolean: true and false;
Integer type (int): the same as in Java;
Float: the same as in Java;
String: starts and ends with single or double quotes;
Null: null.