"declaration"
Welcome reprint, but please keep the original source of the article →_→
Life One number: http://www.cnblogs.com/smyhvae/
Article Source: http://www.cnblogs.com/smyhvae/p/4048033.html
Body
In our previous article, we learned about Tomcat usage and engineering file deployment, and how JSP files were posted to the server and eventually displayed: Javaweb Learning (i)----JSP introduction and Getting Started (including tomcat usage)
If you do not understand in this article, please review the previous article first.
Now to learn the basic syntax of JSP knowledge.
First, the JSP script element
- 1. Declaration (Declaration)
- 2. Expression (Express)
- 3. Script (Scriplets)
"Declarations" are used less often with "expressions" and "scripts".
1. Declaration (Declaration): Variables and methods for declaring a valid in JSP
Grammar:
<%! Code Content%>
Example:
<%! JSP declaration fragment: Note that it is not recommended to define a variable or method on a JSP page, except that the definition of int i = Ten is not recommended; String name = "Smyhvae"; public void Printname () { System.out.println (name); } %>
2, Expression: Evaluates the expression, converts its result into a string into the output
Grammar:
<%= expression%>
Example:
Current time: <%= new Java.util.Date ()%>
After running, the Web page displays the following effects:
3. Script (scriplets): code between <% and%>, which is a legitimate Java code
Grammar:
<% program code, one or more lines%>
Example:
<% for (int i=0;i<10,i++) {...}? %>
second, the JSP directive element
- 1. Page directive
- 2. include directive
- 3. taglib directive
1. Page instruction :p The age directive indicates how to communicate with the JSP container
The common properties in the page directive are as follows:
<%@ page
Language= "Java"
import= "Importlist" Import Java class Library (the only property that can be reused, other properties can be used at most once)
contenttype= types of "contenttyepinfo" content
session= "True|false"
Isthreadsafe= "True|false" is thread safe, default is True
info= "InfoText"
Errorpage= "Errorpageurl" specifies error page: If an exception occurs, it will jump to this error page
Iserrorpage= "True|false" Specifies whether the current page is an error page. If the other page has an error, it will be transferred here.
%>
Note: Only the import property can be reused, and if reused, it must appear on the first line of the command.
In the new JSP file, the first sentence uses the page command:
2. include directive:
The include directive is to include the specified file before the JSP page is converted to a servlet. This feature allows you to create reusable navigation bars, contact information sections, page counts, and more. ( re-use understanding : For example, there may be multiple pages need to use a title page, you can use the common title page to include the include directive, and then in other pages directly into the title page on the line)
format :
<% @include file= "FileURL"%>
process : The file property is first included in the conversion period of the JSP program, and then the conversion and compilation work is performed.
Example :
Create a new title.jsp file: (The Red box section is the code I added)
Continue to create a new content.jsp file: (The Red box section is the code I added)
The core code above is line 10th: Import the title.jsp title file. After running, the effect is as follows:
Note : Include can only be static , contains only static resources, and is compiled to form a file. In fact, title.jsp and content.jsp together only form a. java file, which is then compiled into a. class file. As shown in the following:
3. taglib directive:
For importing the tag library, as for the concept of the tag library, it will be mentioned later, temporarily skipped.
third, the JSP action element
Mainly includes the following sections:
- <jsp:include>
- <jsp:param>/<jsp:params>
- <jsp:forward>
- <jsp:useBean>
- <jsp:setProperty> and <jsp:getProperty>
1, <jsp:include>: include files when user requests (dynamically included)
Process: Included and included files are compiled, and other files are dynamically included when the user requests the page
Example:
Create a new include.jsp file as a contained content: (The red box is the code I added)
Create a new index.jsp file as the main interface: (The Red box section is the code I added)
In line 12th above, flush= "true" indicates that the page can be refreshed in real time.
After running, the effect is as follows:
Comparison of include directives and include actions:
- Command execution is relatively fast and less flexible (only one file is compiled, but once a file is changed, two files are recompiled)
- Relatively slow motion execution with high flexibility
When used, use the include directive if it is a static page, or use the include action if it is a dynamic page.
2. <jsp:param>/<jsp:params> Transfer parameters
The action element cannot be used alone and can be used with the include tag. Now take a look at how the page passes parameters to the contained page.
Example:
Add further in the index.jsp code above:
1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2 pageencoding=" UTF-8 "%> 3 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 4 <jsp:param value= " Smyhvae "name=" name "/> </jsp:include>15 </body>16
Line 13th is added further, meaning that the parameter name is passed to the other page. Where the key is name and the value is smyhvae.
This is followed by a further addition in the INCLUDE.JSP code above:
1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2 pageencoding=" UTF-8 "%> 3 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 4 String name = Request.getparameter ("name"); Gets the value of the name inside the index page , out.println ("<br/>" +name); <br/> line wrapping in page %>18 </body>19
15, 16 lines are added further. Represents the get parameter passed in the index.jsp page name.
After running, the effect is as follows:
3. <jsp:forward> Forward user request
Role: Server-side jump (forward with the requested data, URL address unchanged)
Example: The project file structure is as follows:
Among them, index.jsp is responsible for sending data, RECEIVE.JSP is responsible for forwarding the data, forward.jsp is responsible for receiving data.
The index.jsp code is as follows:
1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2 pageencoding=" UTF-8 "%> 3 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 4
A 14-line submission represents the form being submitted. The action of the 12-line action means that the form is to be submitted to the receive.jsp page.
The receive.jsp code is as follows: ( the core code is line 10th, which means forwarding to the forward.jsp page )
1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2 pageencoding=" UTF-8 "%> 3 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 4 Ten <jsp:forward page= "forward.jsp" ></jsp:forward></body>12
Forward.jsp? Code is as follows:
1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2 pageencoding=" UTF-8 "%> 3 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 4
After running, the effect is as follows:
We enter the text in the above input box, click the "Submit" button, jump to the following screen: (Note the URL)
4. <jsp:useBean> Create a bean instance and specify its name and scope
JavaBean: Simply put, it is a Java class that can be used repeatedly.
It must comply with the following provisions:
- is a public class.
- Has a public, non-parametric construction method.
- Each property must define a set of GetXXX () and Setxxx () methods to read and store its property values.
JavaBean, which complies with the above provisions, will have the following characteristics: event handling, introspection mechanism, and perpetual storage.
5,<jsp:setproperty /> and <jsp:getproperty />
- <jsp:setproperty>: Setting the Bean's property value
- <jsp:getproperty>: Gets the Bean's property value for display in the page
Example: New Test03 project. The engineering structure is as follows: ( here's how to get the property values of a class in a Java file in a JSP file )
New Java file: Create a person class, put in two parameters
1 package Com.vae.bean; 2 3 public class Person {4 private String name, 5 private int age, 6 7 8 public void SetName (stri ng name) {9 this.name = name;10 } One public void Setage (int.) { this.age = age;13 }14
15 public String getName () { name;17}18 public int getage () { return age;20 }21 @Override23 public String toString () { [name= ' + name + ', age= ' + Age + '] " }26 29}
New index.jsp File:
1 <%@ page language= "java" contenttype= "text/html; Charset=utf-8 "2 pageencoding=" UTF-8 "%> 3 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" > 4 The above method is one of the examples we need to cite.
12 line: ID refers to the instantiated object, class is the Java class. This line corresponds to person person = new person ();
15 Rows: Name is an object, property is an attribute, and value is an attribute.
Method one is equivalent to creating a class in a JSP file, except in the form of a label, and then further obtains the attribute values (18, 19 rows) in the class through GetProperty. This method is not very common, we can take the way two: fragments of the way, that is, inserting Java code to implement (21 to 28 lines).
The results are as follows:
Further study on JSP will be discussed later.
Javaweb Learning Notes 7--jsp script elements, instruction elements, action elements