I. INTRODUCTION of JSP directives
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.
The three directives are defined in the JSP 2.0 specification:
- Page directive
- Include directives
- TAGLIB directive
Basic syntax format for JSP directives:<%@ directive Property name = "Value"%>
For example:
<%@ page contenttype= "text/html;charset=gb2312"%>
If a directive has multiple properties, these attributes can be written in one instruction or separately.
For example:
<%@ page contenttype= "text/html;charset=gb2312"%><%@ page import= "Java.util.Date"%>
You can also write:
<%@ page contenttype= "text/html;charset=gb2312" import= "Java.util.Date"%>
Ii. 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. For example:
The full syntax of the page directive defined in the JSP 2.0 specification:
<%@ page
[language= "Java"]
[extends= "Package.class"]
[import= "{package.class | package.*}, ..."]
[session= "true | false"]
[buffer= "none | 8kb | sizekb"]
[Autoflush= "true | false"]
[Isthreadsafe= "true | false"]
[info= "Text"]
[errorpage= "Relative_url"]
[Iserrorpage= "true | false"]
[contenttype= "MimeType [; Charset=characterset]" | "Text/html; Charset=iso-8859-1 "]
[Pageencoding= "CharacterSet | Iso-8859-1 "]
[Iselignored= "true | false"]
%>
2.1.
the Import property of the page directive
In the JSP page, the JSP engine automatically imports the following package
- java.lang.*
- javax.servlet.*
- javax.servlet.jsp.*
- javax.servlet.http.*
You can introduce multiple classes or packages in the Import property of a page directive, separating each package or class with a comma (,)
For example:
<%@ page import= "java.util.*,java.io.*,java.sql.*"%>
The above statement can also be rewritten to use the Import property of multiple page directives to introduce each package or class separately
For example:
1 <%@ page import= "java.util.Date"%>2 <%@ page import= "java.io.*"%>3 <%@ page import= "java.sql.*"%>
2.2.
the ErrorPage property of the page directive
- The setting value of the ErrorPage 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, it is relative to the current page
- You can use the <error-page> element in the Web. xml file to set up error-handling pages for the entire application.
- <error-page> elements have 3 sub-elements,<error-code>, <exception-type>, <location>
- <error-code> child element specifies the wrong status code, such as:<error-code>404</error-code>
- <exception-type> child element specifies the fully qualified name of the exception class, for example: <exception-type>java.lang.arithmeticexception</exception-type >
- <location> child element specifies the path to the error-handling page starting with "/", for example:<location>/errorpage/404error.jsp</location>
- If you set the ErrorPage property of a JSP page, the error handling set in the Web. xml file will not work for that page.
2.4. Use the <error-page> tag in Web. XML to set up error handling pages for the entire application
Example: Use the <error-page> tab to configure a processing page for 404 errors
under the code for Web. XML :
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "3.0"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <Display-name></Display-name> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list> <!--processing page for 404 error - <Error-page> <Error-code>404</Error-code> < Location>/errorpage/404error.jsp</ Location> </Error-page> </Web-app>
2.5, about the use of <error-page> tags in web. xml to set error handling page for the whole website the solution that can't jump in IE
It is important to note that if the error page is small, then when the access server does not exist on the Web resources or access server error in the Internet Explorer is unable to jump to the error page, the display is IE own error page, and under the Firefox and Google browser (other browser has not been tested) There is no question of attention.
We can use the following experiments to prove
Error-friendly Tip page when configuring 500 error in Web. xml
1 <!--handling page for 500 error-->2 <error-page>3 <error-code>500</error-code>4 <location >/errorpage/500error.jsp</location>5 </error-page>
1 <%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"%>2 <HTML>3 <Head>4 <title>500 (server error) error friendly tip page</title>5 <!--Auto Jump back to page after 3 seconds -6 <Metahttp-equiv= "Refresh"content= "3;url=${pagecontext.request.contextpath}/index.jsp">7 </Head>8 <Body>9 <imgalt= "Sorry, server Error!" Ten src= "${pagecontext.request.contextpath}/img/500error.png"/><BR/> One3 seconds after the automatic jump back to the page, if there is no jump, please click<ahref= "${pagecontext.request.contextpath}/index.jsp">Over here</a> A </Body> - </HTML>
500error.jsp byte size of page
Access to test.jsp in IE after the 500 error, the display is IE own error page, instead of our custom 500 error page, and under Google and Firefox can normally jump to our own customized 500 error page, as shown in:
Many people encounter this problem, and there are two ways to solve it:
1, modify the settings of IE browser (not recommended)
How to: Check "show friendly HTTP error prompt" In "Advanced", Internet Options, ie tools
2. Do not modify the settings of IE browser to ensure custom error page size >1024 bytes
Modify the 500error.jsp, add some more content, so that the number of bytes of the page, the modified 500error.jsp code is as follows:
Add a few more Chinese, let 500error.jsp a few more bytes,500error.jsp Now the number of bytes is as follows:
After testing, when the custom error page size=617bytes, under IE8 can jump to the custom error page, other versions of IE browser is not tested, but for the sake of insurance, custom error page size preferably more than 1024bytes.
2.6. Explicitly declare the page as an error page using the Iserrorpage property of the page directive
If a JSP page is a system error-handling page, it is recommended to set the page directive's iserrorpage property (default = false) to "true" To explicitly declare this JSP page to be an error-handling page.
Example: explicitly declaring an error.jsp page as an error-handling page
<%@ Page Language="Java"Import="java.util.*"pageencoding="UTF-8"Iserrorpage="true"%><HTML> <Head> <title>Error message Friendly Tips page</title> </Head> <Body>Sorry, the error, please contact the administrator to solve! </Body></HTML>
What is the benefit of explicitly declaring an error.jsp page as an error-handling page, and the advantage is that the JSP engine declares a exception object in the Servlet's _jspservice method when it translates a JSP page into a servlet. The exception information that runs the JSP error is then stored in the exception object as follows:
Because the exception object is declared in the _jspservice method of the servlet, the exception object can be used in the error.jsp page, so that the error information can be obtained in the JSP page as follows:
If iserrorpage= "true"is not set, the exception object cannot be used in a JSP page because a exception object is not declared in the _jspservice method of the servlet, as follows:
JSP has 9 large built-in objects, and generally exception objects are not available in JSP pages, only the iserrorpage property of the page directive is set to "true" To explicitly declare that a JSP page is an error-handling page before the exception object can be used in a JSP page.
Iii. include directives
There are two types of statements in the JSP for inclusion:
- @include Instructions
- <jsp:include> directives
3.1.
@include Instructions
@include can contain arbitrary files, of course, just include the contents of the file.
The include directive is used to introduce other JSP pages, and if a different JSP page is introduced using the include directive, the JSP engine translates the two jsps into a servlet. So the introduction of include directives is often referred to as static ingestion.
Syntax:<%@ include file= "Relativeurl"%>, where the file property is used to specify the path to the introduced files. The path begins with "/" and represents the current web app.
Include directive details note the issue:
- The file being introduced must follow the JSP syntax.
- The introduced file can use any extension, even if its extension is the HTML,JSP engine will handle the content of the JSP page, in order to see knowingly, the JSP specification recommended to use. JSPF (JSP fragments ( fragment) ) as the extension of the static ingest file.
- Since the use of the include directive will involve 2 JSP pages and will translate 2 jsps into a servlet, the instructions for these 2 JSP pages do not conflict (except for pageencoding and guide packages).
Examples of include directives use:
New HEAD.JSPF page and FOOT.JSPF page, respectively, as the head and tail of the JSP page, stored in the Jspfragments folder under Webroot, the code is as follows:
HEAD.JSPF Code:
3.2. Summary @include directive
Using @include can contain arbitrary content, and the suffix of the file does not matter. This @include statement, which contains other file contents to its own page, is called a static inclusion, and it is only included in the content of other pages and is statically contained.
3.3. jsp:include directive
The jsp:include directive is dynamically included, and if the contained page is a JSP, it is processed before the result is included, and if it contains a non-*.jsp file, it simply includes the contents of the file statically, functionally similar to @include. The following is a detailed introduction
Javaweb Learning Summary (16)--jsp directive