JSP Learning (3)--Grammar Knowledge two page directive

Source: Internet
Author: User
Tags response code

This article is followed by a "JSP learning (2)-Grammar knowledge One", continue to learn the syntax of JSP. This article mainly from the JSP Directive page instruction, carries on the detailed study to its each attribute:

JSP directives:

JSP directives are designed for the JSP engine, and JSP directives do not produce any visible output, but simply tell how to handle the rest of the JSP page in the process of converting to a servlet. The three directives are defined in the JSP 2.0 specification:

1) page directive

2) include directive

3) taglib instruction

The taglib directive is an instruction to import a tag library and will be introduced later.

JSP instruction Format:

<%@  instruction (such as page, include, or taglib)  Property 1= "Property value"   property 2= "Property value"  %>

For example:

<%@  page   %>

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

For example:

1     <%@  page   = "Text/html;charset=utf-8" %>2     <%@  page  = "Java.util.Date" %> 

or write:

<%@  page  contentType= "Text/html;charset=utf-8"  import%>

page instruction

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, and in order to maintain the readability and good habits of the program, the page instruction is best placed at the starting position of the page.

The page directive contains the following properties (property values are examples):

(1). language = "Java" indicates the JSP page if the embedded code is Java code.

(2). extends = "Package.class" specifies which class (usually not moving) the servlet inherits when the JSP is converted.

(3). Import = "package name. Class name" or "package name. *" etc. format,

where the JSP engine will automatically import the following packages :

java.lang.*

javax.servlet.*

javax.servlet.jsp.*

java.lservlet.http.*

That's why we use classes or objects like system,response,request in our JSP without having to separate the packets.

Importing multiple classes or packages using the Import property of multiple page directives, or you can import multiple classes or packages in the Import property of a page directive, where each package or class is separated by commas, for example:

  <%@  page  Import %>

(4). Session = "true" (default value) or "false"

If the "session" attribute is set to "true" in the page directive, the session object is automatically created in the JSP converted servlet.

If we specify the "session" property in the page directive to "true", the following code appears automatically in the. java file for the JSP in the "work" Directory of Tomcat:

    Session = Pagecontext.getsession ();

In this case, we can use the session directly in the JSP footsteps, if the "session" attribute in the page instruction is set to "false", then it is not possible to use the session directly in the JSP footsteps, and to get it manually ( Request.getsession ()).

(5). Buffer = "None" or "8 KB" (default value) or "Custom value KB"

Specifies whether the JSP page needs buffering. That is, until the JSP-converted servlet contains a lot of output through an out object, but the output data is not written directly to the browser, but until the buffer is full, the data is sent back to the browser.

(6). AutoFlush = "true" (default value) or "false"

Specifies whether the JSP page refreshes automatically.

(7). IsThreadSafe = "true" (default value) or "false"

If the "IsThreadSafe" property in the JSP is set to "false", then the converted Servlet will inherit the Singlethreadmode interface and run in Singleton (single-threaded) mode, which allows only one instance at a time. If more than one user accesses the JSP at the same time, the visitor will not be able to access it until the page has been fully accessed by the visitor.

Example: When the "IsThreadSafe" property is not set:

After setting the "IsThreadSafe" property to "false":

If the "IsThreadSafe" property is set to "true" and the default value is "true", it means that the converted servlet is thread-safe to run in multi-threaded mode.

(8). Info = "Custom Information ..."

Used to define some instructions in a JSP page, you can get the value of "info" through the page directive in the servlet through the Getservletinfo () method.

(9). errorpage = "/relative URL"

When the JSP page is wrong, or if there is an error in the Java program, and the exception is not handled, the JSP page accessed on the browser can jump to a page specified by the "ErrorPage" property and is displayed friendly.

Note that the value of the "ErrorPage" property is relative to the address under the Web application (to the server-side address, because throwing an exception is the server that handles the jump to which page).

For example, under Web Project "Jsplearning", create a "exceptions" directory and a new "error.jsp" in that directory, remembering that the "pageencoding" in the JSP directive in the JSP To support encoding in Chinese format , the contents are as follows:

1     < Body > 2            Sorry, your page content is wrong ...  3     </body>

Create a "1.jsp" file in the "WebRoot" root directory of the Web project and write a snippet of the JSP script:

1     <% 2             int x = 1; 3             Out.write (1/0);   // will throw an exception 4     %>

At the same time, add the JSP instruction in the upper part of the JSP, and write the property directly here:

ErrorPage "/exceptions/error.jsp"

So now we're going to visit the 1.jsp page of this web app, and you can see:

  

The JSP that throws the exception we don't handle automatically jumps to the page where we set the "ErrorPage" property, so the user doesn't see the "classic" 505 pages and the descriptions of the various exceptions.

Off Topic:

When we are a big project, there will be a lot of pages, when these pages up, if each JSP page set "ErrorPage" is certainly impractical. In this case, we can set the error handling page in the Web. xml file. If you want to set all WEB app settings in Tomcat, set it up in the Web. xml file under Tomcat's "conf" file, or set the error handling page for a web app in the "Web-inf" directory in the website. xml file.

In the Web. xml file, use the <error-page> tag to set the error handling page for the entire application, where the <exception-type> child element specifies the specific exception full package name and exception name, <location The > tag specifies the path to the error-handling page starting with "/" (also the path to the Web application as the root directory, which is the server-side path).

We re-rewrite the above example to remove the page directive containing the "ErrorPage" attribute from 1.jsp, since we know that our JSP script fragment in the 1.jsp file throws a arithmeticexception exception, Therefore note this exception and the exception of the package name path, and then need to add to the <exception-type> tag.

Then we go to the Web project "Jsplearning" in the "Web-inf" directory of the XML file, add the following lines of code:

1 <Error-page>2       <Exception-type>Java.lang.ArithmeticException</Exception-type>3       < Location>/exceptions/error.jsp</ Location>4 </Error-page>

After completing the above work, when accessing a resource under the Web application (whether JSP or servlet, etc.), as long as the arithmeticexception exception is thrown, the server immediately jumps the resource to the error handling page that specifies the exception, in our case exceptions/error.jsp, visit 1.jsp again to see the same effect:

  

This completes the configuration of the error-handling page for all the resources in a Web application, and there is, of course, a disadvantage where this method must be used to indicate what error exception (<exception-type> must specify). But if you use <error-code>, you can avoid this kind of annoyance, which will be said later.

Another, when using this method, if the browser, such as IE, in its tools---> Internet Options, select advanced to find a "show friendly HTTP error message" as shown in:

  

Usually this option browser is checked by default, what is the problem, is to use in the Web. xml file Configuration <error-page> This element, if our error handling page content is too small, less than 1024 bytes, Then in the browser is not able to jump to the error handling page, for example, we just/exceptions/error.jsp the <body> tag content is set to: error!, the content is small enough, then we reopen the browser to access 1.jsp:

  

You can see that the error handling page cannot be seen.

Two workarounds: 1, remove the "Show friendly HTTP error message" Hook in Internet Options, 2, add more than 1024 bytes to the content in the/exceptions/error.jsp error handling page (more than one add point is easily exceeded). Both of these methods can see the error handling page again.

It says in <error-page> can set <exception-type> this child element, but we must give definite exception, after all trouble. But in <error-page> can also set the <error-code> this child element, this element is used to indicate when the browser accesses the error response code should be accessed which pages, what are the wrong response code? 404! A. Familiar, t_t!?

We continue to use the above Web project "Jsplearning" as an example, in the previous "exceptions" directory to create a new error404.jsp, add the custom content.

We then continue to add the following code to the Web project's XML:

 1  <  error-page  >  2  <  e Rror-code  >  404</ error-code  >  3  <  location  > /exceptions/ Error404.jsp</ >  4  </ error-page  >  

When we visit a resource that does not exist in the Web application, the server will help us jump to the error handling page that has the 404 error and is displayed friendly:

  

As you can see, this is much more convenient than specifying a particular exception, and of course specifying a specific exception naturally has its role, which is to be considered by the actual situation.

This is a lot to say, but it's a helpful technique for development. The last point here is that if you set the page directive for the "ErrorPage" property on a certain screen, the <error-page> two forms that are set in the Web. xml file do not work for it, meaning that the JSP page only first recognized the " ErrorPage "property.

(Ten). iserrorpage = "true" or "false" (default value)

In conjunction with the previous point, when we set a JSP page as the error handling page, we'd better set the page directive's "Iserrorpage" property to "true".

What's the good of doing this? when this error is processed into a servlet after the JSP page is converted, an exception object, exception, is automatically generated in this servlet, which encapsulates some information that was previously incorrectly accessed. Remember that if you do not set the "Iserrorpage" property of the page directive or "false" in the error handling page, then the converted servlet will not have this exception object.

So let's take the Web project "Jsplearning" above as an example, when we set the "Iserrorpage" property of the page directive for the/exceptions/error.jsp JSP:

    <%@  page  Iserrorpage %>

Let the browser access the error code 1.jsp once the browser jumps to the error handling page, we go to Tomcat's "work" directory to view the Error_jsp.java file, we will find in error_jsp this class of _jspservice () The exception object is defined in the method, which is not set by the JSP conversion servlet with "Iserrorpage" as "true":

  

This exception object with an Out object, Page object, etc., can be used directly in the JSP, remember that this object must be in those who declare "Iserrorpage" is "true" in the JSP directly used.

(one). contentType = "MIME type/corresponding type [(; chrset= an encoding table) optional]"

The "ContentType" property in the page directive tells the JSP engine (or Tomcat) the type of the JSP page, for example:

  <%@ page ContentType %>

When a JSP transitions to a servlet, the corresponding call Servletresponse.setcontenttype (...) is generated according to the "ContentType" attribute in the page directive. Method statement.

  

The "ContentType" property is used to tell the browser what code table to decode to open the contents of the JSP (in fact, the servlet).

This property can be used to solve the JSP in the possible garbled problem, this problem we will discuss later.

( pageencoding ) = "One encoding table"

The page directive's property "Pageencoding" specifies what code table the server will use to translate the JSP file into a. java file. When this "pageencoding" attribute is set, the encoding table in the "ContentType" property is set to the same as it is, so we can simply set the "pageencoding" property to summarize.

iselignored = "true" or "false" (default value)

Tells the JSP engine (or Tomcat) whether the JSP page ignores the EL expression. The default value is "false" that is, the JSP supports El expressions.

JSP Learning (3)--Syntax Knowledge two page directive

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.