1. Introduction of JSP Instruction
JSP directives (Directive) are designed for the JSP engine, and they do not directly produce any visible output, but simply tell the engine how to handle the rest of the JSP page.
Basic syntax format for JSP directives:
<%@ directive Property name = "Value"%>
Example: <%@ page contenttype= "text/html;charset=gb2312"%>
Note: The attribute name section is case-sensitive
In the current JSP 2.0, the page, include, and taglib are defined in three instructions, each of which defines some of its own properties.
If you want to set multiple properties of the same instruction in a JSP page, you can set each property individually using multiple instruction statements, or you can set multiple properties of the directive using the same directive statement.
The first way:
The second way:
2. Page directive
The page directive is used to define the various properties of a JSP page, regardless of where the page directive appears in the JSP page, it is the entire JSP page, in order to maintain the readability of the program and follow good programming habits, the page directive is best placed in the entire JSP page start position.
The full syntax of the page directive defined in the JSP 2.0 specification:
①. Import Property: Specifies the class to import for the Servlet that corresponds to the current JSP page. <% @page import= "Java.text.DateFormat"%>
②. Session property: Evaluates to TRUE or FALSE to specify whether the session hidden variable of the current page is available, or whether the HttpSession object must be generated when accessing the current page. <%@ page session= "false"%>
③. ErrorPage and Iserrorpage:
> errorpage specifies what the actual response page is if there is an error on the current page. where/represents the root directory of the current WEB app. <%@ page errorpage= "/error.jsp"%>. The way that the JSP engine uses the request forwarding when responding to error.jsp.
> iserrorpage Specifies whether the current page is an error handling page, which indicates whether the current page can use exception to hide variables. It is important to note that if you specify Iserrorpage= "true" and use the exception method, it is generally not recommended to have direct access to the page. How do you make it impossible for a customer to access a page directly? For a Tomcat server, files under Web-inf cannot be accessed by directly entering an address in the browser. However, forwarding through the request is possible. You can also configure the error page in the Web. xml file:
④. ContentType: Specifies the type of response for the current JSP page. The actual call is Response.setcontenttype ("text/html; Charset=utf-8 "); Typically, the value of the JSP page is text/html; Charset=utf-8. CHARSET specifies what the character encoding of the returned page is, and usually takes a value of UTF-8
⑤. Pageencoding: Specifies the character encoding of the current JSP page. Typically, this value is consistent with CharSet in ContentType.
⑥. Iselignored: Specifies whether the current JSP page can ignore an EL expression. Usually the value is false.
3. include directive
The include directive informs the JSP engine to merge the contents of other files into the servlet source files converted into the current JSP page when translating the current JSP page, a method introduced at the source file level called static ingestion. The current JSP page is tightly combined with a statically introduced page as a servlet.
Grammar:
<%@ include file= "Relativeurl"%>
The file property is used to specify the relative path of the introduced files.
Details:
(1) The introduced file must follow the JSP syntax, where the content can include static HTML, JSP script elements, JSP directives and JSP behavior elements and other common JSP pages have all the content.
(2) The introduced file can use any extension, even if its extension is the HTML,JSP engine will handle the contents of the JSP page, in order to see knowingly, the JSP specification recommends using the. JSPF (JSP fragments) as the extension of the static ingest file.
(3) When translating a JSP file into a servlet source file, the JSP engine merges the introduced file with the instruction element in the current JSP page (except for the page directive that sets the Pageencoding property), so that, in addition to the import and Pageencoding properties, Other properties of the page directive cannot have different setting values in the two pages.
Open A_jsp.java to see what's inside.
Then add the following in the a.jsp, and be sure to put it in front of the introduction b.jsp
Get in b.jsp
4. JSP tag
JSP also provides an element called action, the use of the action element in a JSP page to complete a variety of common JSP page functionality, but also to implement some of the complex business logic of the special functions.
The action element takes the syntax format of an XML element, where each action element appears as an XML tag in the JSP page.
Some standard action elements are defined in the JSP specification, with the tag names prefixed with JSP and all lowercase, such as,<jsp:include>, <jsp:forward>, <jsp:param> Wait a minute.
5. <jsp:include> Label
tags are used to insert the output of another resource into the output of the current JSP page, which is called dynamic Introduction when the JSP page executes.
The page property is used to specify the relative path to the resource being introduced, and it can also be obtained by executing an expression.
The Flush property specifies whether the output of the current JSP page is flushed to the client first when the output of the other resource is inserted.
View A_jsp.java File
6. Comparison of <jsp:include> tags with include directives
(1) <jsp:include> tags are inserted into the output of the resource during the execution of the current JSP page, the current JSP page and the dynamically introduced resources are two independent execution entities, the dynamically introduced resources must be a Web container can be invoked and executed independently of the resources. The include directive only introduces files that follow the JSP format, and the introduced files are combined with the current JSP file to be translated into a servlet source file.
(2) The use of <jsp:include> tags and include directives can be used to separate the content of a page into multiple components, developers no longer have to copy the same HTML code in the header and footer parts into each JSP file, making it easier to complete maintenance work, However, it should be noted that the final output content should follow the HTML syntax structure, for example, if the current page produces
(3) The <jsp:include> tag does not work for the JSP engine to translate the JSP page, it is called during the execution of the JSP page, so it does not affect the compilation of the two pages. Because the include directive is interpreted during the JSP engine's translation of the JSP page, it works on the JSP engine's process of translating the JSP page, and if you use some of the same declarations in multiple JSP pages, you can write these statements in a separate file. Then use the include directive in each JSP page to include that file.
(4) <jsp:include> tags use the page property to specify the relative path to the resource being introduced, while the include directive uses the file property to specify the relative path to the resource being introduced.
7. <jsp:forward> Label
<jsp:forward> tags are used to forward requests to another resource
Equivalent
8. <jsp:param> Label
When using <jsp:include> and <jsp:forward> tags to introduce or forward a request to a resource that is dynamically executed, such as a servlet and JSP page, you can also use the <jsp:param> The label passes the parameter information to this program.
9, JSP Chinese garbled
(1) In the JSP page input Chinese, the request page does not appear garbled: Guarantee Contenttype= "text/html; Charset=utf-8 ", pageencoding=" UTF-8 "CharSet and pageencoding are encoded consistent, and both support Chinese. It is generally recommended that the value be UTF-8, and that the character encoding displayed by the browser be consistent with the encoding of the requested JSP page.
(2) Get the Chinese parameter value: The default parameter is encoded as iso-8859-1 used in the transfer process
①. For POST requests: Call Request.setcharacterencoding ("UTF-8") just before getting the request information (in the call to Request.getparameter or Request.getreader, etc.).
②. For GET requests: The previous method is not valid for get; You can modify the way Tomcat's server.xml files are. Refer to the Usebodyencodingforuri property of the http://localhost:8989/docs/config/index.html document. Add the Usebodyencodingforuri= "true" property to the Connector node.
Javaweb (vii) JSP-2