this blog is original: Integrated still Silicon Valley (http://www.atguigu.com) System tutorial (deep thank) and network of existing resources (blogs, documents, books, etc.), the source of resources I will markThe purpose of this blog: ① summary of their own learning process, equivalent to study notes ② to share their own experience to everyone, learn from each other, communication, not commercialcontent inevitably appear problems, welcome to correct, exchange, discussion, you can leave a message, can also be contacted by the following ways. I Internet technology enthusiasts, Internet technology enthusiastsWeibo: Ivan is in 0221qq:951226918
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----------------------------
1. Introduction to JSP directives
1) 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
2) Basic syntax format for JSP directives: <%@ directive Property name = "Value"%>
Example: <%@ page contenttype= "text/html;charset=gb2312"%> Note: The property name portion is case sensitive
3) in JSP 2.0, the page, include, and taglib are defined in three instructions, each of which defines some of the respective properties
4) 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.
First way: <%@ page contenttype= "text/html;charset=gb2312"%> <%@ page import= "Java.util.Date"%>
Second way: <%@ page contenttype= "text/html;charset=gb2312" import= "Java.util.Date"%>
2. Page directive: the page directive isused to define the various properties of a JSP page, regardless of where the page directive appears in the JSP page , it acts as 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 starting position of the entire JSP page .
The full syntax of the page directive defined in the JSP 2.0 specification:
<%@ page
[language= "Java"] Setting the language, the only value currently is Java
[extends= "Package.class"] which class to integrate after the current JSP is translated into a servlet source file on the server (basically meaningless)
* [import= "{package.class | package.*}, ..."] the packages that you need to import
* [session= "true | false"] whether the current JSP is allowed to use session, usually true
[buffer= "none | 8kb | sizekb"]
[Autoflush= "true | false"]
[Isthreadsafe= "true | false"]
[info= "Text"]
* [errorpage= "Relative_url"] Specifies an error page, if the current page error, JSP will be the ErrorPage page as a response page, internal is a forwarding mechanism, that is, the address bar for the first time the requested page address
* [iserrorpage= "true | false"] Specifies whether the current page is an error handling page, and whether the page can use the Exception object, If True, you can use the Exception method, it is generally not recommended to directly visit Ask the page.
What if the customer doesn't have direct access to a page? 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.
* [contenttype= "MimeType [; Charset=characterset]" | "Text/html; Charset=iso-8859-1 "] Specifies the type of response for the current JSP page . after the JSP is actually translated into a servlet, call Response.setcontentype ("Text/html;charset=utf-8");
Usually, the value of JSP page is text/html; Charset=utf-8, CharSet specifies what the character encoding of the returned page is, usually UTF-8
* [pageencoding= "CharacterSet | Iso-8859-1 "] Set the encoding format of the page
* [iselignored= "true | false"] Specifies whether the current JSP page can use an EL expression, and usually evaluates to True
Note: The ①jsp engine generates a corresponding statement that calls the Servletresponse.setcontenttype method based on the ContentType property of the page directive . The ContentType property of the page directive also has the function of explaining the character encoding of the JSP source file.
The setting value of the ② ErrorPage property must use a relative path, which, if preceded by "/", is relative to the current Web application's root directory (note that it is not the site root), otherwise it represents relative to the current page.
You can use the <error-page> element in the Web. xml file to set the error-handling page for the entire application, where the <exception-type> child element specifies the fully qualified name of the exception class,<location> element specifies the path to the error-handling page that begins with "/".
If you set the ErrorPage property of a JSP page, error handling set in the Web. xml file will not work on the page
1 <!--Configuration Error page -2 <Error-page>3 <!--Specify code for error: 404 No resource specified, 500 internal error -4 <Error-code>500</Error-code>5 <!--Specify the location of the response page -6 < Location>/win-inf/error.jsp</ Location>7 </Error-page>8 9 <Error-page>Ten <!--specifying the type of exception - One <Exception-type>Java.lang.exception</Exception-type> A < Location>/web-inf/error.jsp</ Location> - </Error-page>
3.include directive: The include directive is used to inform 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 , which is introduced at the source file level called Static introduction , the current JSP page and the static introduction of the page tightly integrated into a servlet
Syntax: <%@ include file= "Relativeurl"%> where the file property is used to specify the relative path of the introduced files
Attention:
1) The introduction of the file must follow the JSP syntax, wherein the content can contain 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 is recommended to use the. JSPF (JSP fragments) as the extension of the static introduction of the 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 In addition to the import and Pageencoding properties, the other properties of the page directive cannot have different setting values in the two pages;
4) In addition to the instruction element, the other elements in the introduced file are converted into the corresponding Java source code, and then inserted into the current JSP page translated into the servlet source file, the insertion location and the include directive in the current JSP page position consistent;
5) The introduction of files and the introduction of files are merged in the process of being translated into servlets by the JSP engine, rather than merging the source files before translating the merged results . The source file of the current JSP page and the source file of the file being introduced can be encoded in different character sets, even if the pageencoding or ContentType property of the page directive is used in a single sheet to specify the character set encoding of its source file. In another page, you also need to specify the character set used by the source file with the Pageencoding or ContentType property of the page directive;
6)The setting value of the file property must use a relative path , which, if preceded by "/", is relative to the root of the current Web application (note that it is not the site root), otherwise, is represented relative to the current file
4.JSP Label
1) SP also provides an element called action, the use of the action element in the JSP page to complete a variety of common JSP page functions , but also to implement some of the complex business logic of the special functions;
2) The action element takes the syntax format of the XML element, that is, each action element appears as an XML tag in the JSP page;
3) The JSP specification defines some standard action elements, which are prefixed with JSP and are all lowercase , such as,<jsp:include>, <jsp:forward>, and so on;
4) <jsp:include> label,<jsp:forward> label,<jsp:param> label
①<jsp:include> Label
1> <jsp:include> tags are used to insert the output of another resource into the output of the current JSP page , which is introduced in the implementation of JSP page called dynamic introduction ;
2> syntax:<jsp:include page= "relativeurl | <%=expression%>" flush= " true|false"/>
Page property: Used to specify the relative path to the resource being introduced, it can also be obtained by executing an expression
Flush Property: Specifies whether the output of the current JSP page is flushed to the client when the output of the other resource is inserted
3> <jsp:include> differences with static inclusions: (Learn)
I. The dynamic introduction generated two servlet source files, and the target page is included in the way of command methods. org.apche.jasper.runtime.JspRuntimeLibrary.include (Request, Response, "xx.jsp", out, false);
The static inclusion produces only one servlet source file;
Ii. <jsp:include> tag is to insert the output of the imported resource during the execution of the current JSP page, the current JSP page and the dynamically introduced resource are two independent executing entities, and the dynamically introduced resource must be a resource that can be invoked and executed independently by the Web container. ;
The include directive can only introduce 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;
②<jsp:forward> Label
1> <jsp:forward> tags are used to forward requests to another resource;
2> Syntax: <jsp:forward page= "relativeurl | <%=expression%>"/>
The page property is used to specify the relative path of the resource to which the request is forwarded, and it can be obtained by executing an expression;
3> <jsp:forward> label, equivalent to Request.getrequestdispacher (). Foward (); However, labels can be configured with the <jsp:param> sub-label parameters
1 <Jsp:forwardpage= "xx.jsp">2 <Jsp:paramvalue= "Jason"name= "User"/>3 </Jsp:forward> 4 5 <%6 Request.getrequestdispatcher ("xx.jsp"). Forward (request, response);7 %>
③ <jsp:param> tags
1> when you use <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 use <jsp: The param> tag transmits parameter information to this program ;
2> Syntax 1: <jsp:include page= "Relativeurl | <%=expression%> ">
<jsp:param name= "parametername" value= "parametervalue|<%= expression%>"/>
</jsp:include>
Syntax 2: <jsp:forward page= "Relativeurl | <%=expression%> ">
<jsp:param name= "parametername" value= "parametervalue|<%= expression%>"/>
</jsp:include>
the Name property of the 3> <jsp:param> tag is used to specify the parameter name, and the Value property is used to specify the parameter value . Multiple <jsp:param> tags can be used to pass multiple parameters in <jsp:include> and <jsp:forward> tags
[Original]java Web Learning note 16:jsp directive (page,include), JSP tag (Forwar,include,param)