HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
This section will briefly introduce JSP Development of basic grammar.
Java script statement can contain any amount of variables, methods, or expression, so long as they are valid in the scripting language.
Script syntax:
<% 代码片段 %>
Alternatively, you can also write its equivalent XML statement, like this below:
<jsp:scriptlet> 代码片段 </jsp:scriptlet>
Any text, HTML tags, JSP elements must be written out of the script.
Here's an example, but also the first example of a JSP 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 already installed in the C: \ apache-tomcat-7.0.2 directory and the operating environment has been set correctly.
The above code is stored in hello, and then place it in the C: \ apache-tomcat-7.0.2 \ webapps \ ROOT directory under, open a browser and type in the address bar http: // localhost: 8080 / hello . After running the following results:
If we are to properly display the page in Chinese, we need to head in a JSP file, add the following code: <>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
We resolve to modify the above program:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>本教程(w3resource.net)</title> </head> <body> Hello World!<br/> <% out.println("你的 IP 地址 " + request.getRemoteAddr()); %> </body> </html>
So Chinese will be displayed correctly.
A declaration can declare one or more variables, methods, Java code for later use. In the JSP file, you must declare these variables and methods before you use them.
JSP declaration syntax:
<%! declaration; [ declaration; ]+ /en. %>
Alternatively, you can also write its equivalent XML statement, like this below:
<jsp:declaration> 代码片段 </jsp:declaration>
Program example:
<%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %>
A JSP scripting language expressions contained in the expression, is first converted to String, and then inserted into the local expressions arise.
Since the value of the expression is converted into a String, so you can use expressions in a line of text without having of whether it is HTML tags.
Expression element can contain any expression that conforms to the Java language specification, but you can not use a semicolon to the end of the expression.
JSP expression syntax:
<%= 表达式 %>
Similarly, you can also write its 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>本教程(w3resource.net)</title> </head> <body> <p> 今天的日期是: <%= (new java.util.Date()).toLocaleString()%> </p> </body> </html>
After running the following results:
今天的日期是: 2016-6-25 13:40:07
JSP comments have two main functions: to annotate the code and the piece of code commented out.
JSP comment syntax:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>本教程(w3resource.net)</title> </head> <body> <%-- 该部分注释在网页中不会被显示--%> <p> 今天的日期是: <%= (new java.util.Date()).toLocaleString()%> </p> </body> </html>
After running the following results:
今天的日期是: 2016-6-25 13:41:26
Under different circumstances the use of annotation syntax rules:
grammar | description |
---|---|
<% - Comment -%> | JSP comments, the comment will not be sent to the browser will not even be compiled |
<! - Comment -> | You can see the comments in HTML comments, view the page source code through the browser |
<\% | Representative static <% constant |
% \> | Representative static%> constants |
\ ' | Use single quotes in the property |
\ " | Use double quotes in the property |
JSP directive to set the attributes associated with the entire JSP page.
JSP command syntax:
<%@ directive attribute="value" %>
There are three instruction tags:
instruction | description |
---|---|
<% @ Page /en.%> | Dependency property definition page, such as a scripting language, error pages, cache requirements, etc. |
<% @ Include /en.%> | It contains additional files |
<% @ Taglib /en.%> | Introducing tag library definitions can be custom label |
JSP tags using XML syntax structure acts to control the servlet engine. It can dynamically insert a file, reuse JavaBean components, lead the user to another page, the associated HTML generated for the Java plug-in and so on.
Behavior is only one label syntax that strict adherence to XML standards:
<jsp:action_name attribute="value" />
Behavior tab is basically on some pre-defined functions, the following table lists some of the JSP behavior Luo available label ::
grammar | description |
---|---|
jsp: include | For containing static or dynamic resource in the current page |
jsp: useBean | Find and initialize a JavaBean components |
jsp: setProperty | Setting JavaBean components |
jsp: getProperty | The value of the JavaBean components into the output of |
jsp: forward | Transfer request object that contains the user's request from a JSP file to another file |
jsp: plugin | Applet used to contain and JavaBean objects in the generated HTML pages |
jsp: element | Dynamically create an XML element |
jsp: attribute | Attribute XML element defines the dynamic created |
jsp: body | Body XML element defines a dynamically created |
jsp: text | Template used to encapsulate data |
JSP support variables nine automatically defined, rivers and lakes known hidden objects. The nine implicit objects Introduction follows:
Objects | description |
---|---|
request | Examples of HttpServletRequest class |
response | Examples HttpServletResponse class |
out | Examples PrintWriter class is used to output the result on the page |
session | Examples HttpSession class |
application | ServletContext instance of the class, and context-sensitive application |
config | Examples ServletConfig class |
pageContext | Examples PageContext class JSP page provides access to all the objects and namespaces |
page | Similar to the Java class in this keyword |
Exception | Exception class object that represents the error occurred JSP page corresponding exception object |
JSP provides comprehensive support for the Java language. You can use the program in JSP Java API even create blocks of Java code, including the judge statements and loops, and so on.
If /en. else block, consider the following example:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%! int day = 3; %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>本教程(w3resource.net)</title> </head> <body> <h3>IF/en.ELSE 实例</h3> <% if (day == 1 | day == 7) { %> <p>今天是周末</p> <% } else { %> <p>今天不是周末</p> <% } %> </body> </html>
After running the following results:
IF/en.ELSE 实例 今天不是周末
Now look at the switch /en. case block, and if /en. else blocks are very different, it uses out.println (), and the entire script tags are installed in the program, like this:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%! int day = 3; %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>本教程(w3resource.net)</title> </head> <body> <h3>SWITCH/en.CASE 实例</h3> <% switch(day) { case 0: out.println("星期天"); break; case 1: out.println("星期一"); break; case 2: out.println("星期二"); break; case 3: out.println("星期三"); break; case 4: out.println("星期四"); break; case 5: out.println("星期五"); break; default: out.println("星期六"); } %> </body> </html>
Browser access, run the following results:
SWITCH/en.CASE 实例 星期三
Three basic types of circulation in the JSP program, you can use the Java: for, while, and do /en. while.
Let's look at an example of a for loop, the following output of different font sizes of the "Guide":
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%! int fontSize; %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>本教程(w3resource.net)</title> </head> <body> <h3>For 循环实例</h3> <%for ( fontSize = 1; fontSize <= 3; fontSize++){ %> <font color="green" size="<%= fontSize %>"> 本教程 </font><br /> <%}%> </body> </html>
After running the following results:
The above example use a while loop to write:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%! int fontSize; %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>本教程(w3resource.net)</title> </head> <body> <h3>While 循环实例</h3> <%while ( fontSize <= 3){ %> <font color="green" size="<%= fontSize %>"> 本教程 </font><br /> <%fontSize++;%> <%}%> </body> </html>Browser access, output is (fontSize initialized to 0, so the output of a multi-line):
JSP supports all Java logical and arithmetic operators.
The following table lists the JSP Luo common operator precedence from high in the end:
category | Operators | Binding |
---|---|---|
suffix | () []. (Dot operator) | Left to Right |
One yuan | + + - -! ~ | Right to Left |
Multiplicativity | * /% | Left to Right |
Additivity | + - | Left to Right |
Displacement | >> << >>> | Left to Right |
relationship | >> = <<= | 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 | ?: | Right to Left |
Assignment | = + = - = * = / =% = >> = << = & = ^ = | = | Right to Left |
comma | , | Left to Right |
JSP language defines the following words face amount: