Javaweb Learning Summary (16)--jsp directive

Source: Internet
Author: User
Tags tomcat server

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:

1 <%@ page contenttype= "text/html;charset=gb2312"%>

If a directive has multiple properties, these attributes can be written in one instruction or separately. For example:

1 <%@ page contenttype= "text/html;charset=gb2312"%>2 <%@ page import= "Java.util.Date"%>

You can also write:

1 <%@ 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:

1 <%@ page  2     [language= "java"]  3     [extends= "Package.class"]  4     [import= "{Package.class | Package.*}, ... "]  5     [session= "true | false"]  6     [buffer= "none | 8kb | sizekb"]  7     [autoflush= "true | false"]  8     [isthreadsafe= "true | false"]  9     [info= "text"]     [errorpage= "Relative_url"] [     isError Page= "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:

1 <%@ 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.3.Use the ErrorPage property to indicate the error page that jumps after an error

For example, the test.jsp page resembles the following code:

1 <%@ page language= "java" import= "java.util.*" errorpage= "/errorpage/error.jsp" pageencoding= "UTF-8"% > 2 

In test.jsp, the ErrorPage property of the page directive indicates that the error jumps to "/errorpage/error.jsp", andthe error.jsp page code is as follows:

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2     

The results of the operation are as follows:

  

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 :

1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <web-app version= "3.0"  3     xmlns= "http://java.sun.com/xml/ Ns/javaee "  4     xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "  5     xsi:schemalocation=" http ://java.sun.com/xml/ns/javaee  6     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> 7   < Display-name></display-name>     8   <welcome-file-list> 9     <welcome-file> index.jsp</welcome-file>10   </welcome-file-list>11   <!--handling page for 404 errors-->13   <error-page>14       <error-code>404</error-code>15       <location>/errorpage/ 404error.jsp</location>16   </error-page>17   </web-app>

The 404error.jsp code is as follows:

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 

When accessing a nonexistent web resource, it jumps to the 404 error-handling page 404error.jspconfigured in XML. config, as shown in:

  

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>

The code for the 500error.jsp page is as follows:

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 

500error.jsp byte size of page

Running results under IE8 Browser:

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

  

After such a setup, the Access server error can jump directly to our custom 500 error page, as shown in:

  

This approach requires modifying the configuration of the client browser and is not recommended in this way.

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:

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 "Ten     src=" ${pagecontext.request.contextpath}/img/500error.png "/><br/>11     3 seconds after automatically jump back to the page, If there is no jump, please click <a href= "${pagecontext.request.contextpath}/index.jsp" > here </a>12   </body>13 </ Html>

Add a few more Chinese, let 500error.jsp a few more bytes,500error.jsp Now the number of bytes is as follows:

  

Access under IE, when the server error, you can normally jump to 500error.jsp this custom error page, as shown:

  

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

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8" iserrorpage= "true"%> 2 

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:

    1. @include Instructions
    2. <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:

    1. The file being introduced must follow the JSP syntax.
    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 (fragment)) as the extension of the static ingest file.
    3. 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:

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 

FOOT.JSPF Code:

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>2 

Create a includetagtest.jsp page under the Webroot folder and use it on the includetagtest.jsp page @include Directives introduce HEAD.JSPF pages and FOOT.JSPF pages with the following code:

1 <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%> 2 <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" > 3 

The results of the operation are as follows:

  

Let's look at the source code after the JSP engine translates includetagtest.jsp into the includetagtest_jsp class and find the work\catalina\localhost\javaweb_jsp_ of the Tomcat server Locate the Includetagtest_jsp.java in the study_20140603\org\apache\jsp directory, as shown in:

  

Open the Includetagtest_jsp.java, and the code inside is as follows:

1 package org.apache.jsp; 2 3 Import javax.servlet.*; 4 Import javax.servlet.http.*; 5 Import javax.servlet.jsp.*; 6 Import java.util.*; 7 Import java.util.*; 8 Import java.util.*; 9 Public final class Includetagtest_jsp extends Org.apache.jasper.runtime.HttpJspBase11 implements Org.apache.jaspe   r.runtime.jspsourcedependent {The private static final jspfactory _jspxfactory = Jspfactory.getdefaultfactory (); 14 15 private static Java.util.List _jspx_dependants;16the static {_jspx_dependants = new java.util.ArrayList (2); _jspx_dependants.add ("/JSPFRAGMENTS/HEAD.JSPF "); _jspx_dependants.add ("/JSPFRAGMENTS/FOOT.JSPF ");Javax.el.ExpressionFactory _el_expressionfactory;24 Private Org.apache.AnnotationProcessor _jsp_annotat ionprocessor;25 public Object getdependants () {_jspx_dependants;28}29, public void _jspinit () {3 1 _el_expressionfactory = _jspxfactory.getjspapplicationcontext (Getservletconfig (). Getservletcontext ()). Getexpressionfactory (); _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getservletconfig (). Getservletcontext (). getattribute (Org.apache.AnnotationProcessor.class.getName ());}34, public void _jspdestroy ( ) {}37 _jspservice (httpservletrequest request, httpservletresponse response) throws Java.i O.ioexception, servletexception {pagecontext PageContext = null;42 HttpSession session = null;43 Servlet     Context application = null;44 servletconfig config = null;45 jspwriter out = null;46 Object page = this;47 JspWriter _jspx_out = null;48 PageContext _jspx_paGe_context = null;49 try {response.setcontenttype ("Text/html;charset=utf-8"), PageContext = _js Pxfactory.getpagecontext (this, request, response,54 null, True, 8192, true); _jspx_page_context       = pagecontext;56 application = pagecontext.getservletcontext (); config = Pagecontext.getservletconfig (); 58 Session = Pagecontext.getsession (); n-out = Pagecontext.getout (); _jspx_out = out;61 Out.write ("\ r \ n"); Out.write ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" >\r\n "); Out.write (" out.write ("

Out.write ("\ r \ n");out.write ("Out.write (""); Out.write ("\ r \ n");out.write ("

Out.write ("\ r \ n"), Bayi out.write ("</body>\r\n"), Out.write ("

As you can see, the contents of the HEAD.JSPF and FOOT.JSPF pages are displayed using the out.write output to the browser.

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.