JSP series: (2) JSP basic-page instruction detailed

Source: Internet
Author: User

3.2. Page directive


Role: Tell the Tomcat server how to translate the JSP file

<%@ page

Language= "Java"--tells the server what dynamic language to use to translate JSP files

Import= "Java.util.*"--Tell the server what package the Java file uses (import package),

Separating multiple packages with commas

Pageencoding= "Utf-8"--Tell the server what encoding to use to translate JSP files (into Java files)

Contenttype= "text/html; Charset=utf-8 "--the server sends the browser data type and content encoding.

Note: In the development tool, you only need to

Set pageencoding can solve Chinese garbled problem

Errorpage= "error.jsp"--Specifies the error handling page of the current JSP page.

Iserrorpage= "False"--Specifies whether the current page is an error handling page.

False, not the error handling page, you cannot use the exception built-in object;

True, is the error handling page, which can be used with exception built-in objects.

buffer= the buffer size of the "8kb"--jsp page.

Session= "true"-whether the session function is turned on. False, cannot use the session built-in object;

True, the session built-in object can be used.

Iselignored= "false"--whether the El expression is ignored.

%>


"1" language= "Java" is a fixed notation

"2" import= "java.util.*" is the introduction of the package, separated by "," between multiple packages, for example:


JSP file Encoding problem


"3" pageencoding and contenttype all involve coding problems, but both affect different stages.

We need to figure out which parts of the code are involved.

(1) When the JSP file is stored, it needs to be encoded.

The JSP file will eventually be stored as "byte" on disk.

Therefore, it is necessary to convert the in-memory string into a byte-stream output by referencing a code table.

(2) When converting from a JSP file to a Java file, you need to transcode two times.

The first time, the JSP file read into memory, is to convert the byte stream into a character stream, need to transcode;

The second time is to convert a stream of characters in memory into a stream of bytes in a Java file, which needs to be transcoded.

However, these two times transcoding is done with reference to the encoding specified by pageencoding.

(3) Conversion from a Java file to a class file and two transcoding is also required. Two times transcoding is the encoding specified using Pageencoding.

(4) When the user accesses the JSP file, the content is output by the class file, which requires two times transcoding.

The first time you read a string from a class file, the pageencoding encoding is used;

The second time is when the Tomcat server sends data to the browser, using the encoding specified in ContentType.

(5) In the browser, you can also specify the character encoding to receive the Tomcat server

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/81/FF/wKioL1dG81Dwtv3aAABky9ZsYRE267.png "title=" jsp_ Encoding.png "alt=" Wkiol1dg81dwtv3aaabky9zsyre267.png "/>

Tip: If we only specify pageencoding= "Utf-8" while using MyEclipse, and omit the contenttype attribute, then (1) (2) (3) (4) The encoding involved will be encoded in pageencoding specified format, In order to solve the problem of Chinese garbled.


Exception-related errors:


The "4" ErrorPage is the error handling page that specifies the current JSP page.

The "5" iserorrpage Specifies whether the current page is an error handling page.

False, not the error handling page, you cannot use the exception built-in object;

True, is the error handling page, which can be used with exception built-in objects.

Example: pagedemo.jsp, specifying an error page with ErrorPage

<%@ page language= "java" pageencoding= "Utf-8" errorpage= "common/errorpage.jsp"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >

Example: errorpage.jsp, using iserrorpage= "true" to identify itself as an error page.

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8" iserrorpage= "true"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >




Configure the Global error handling page , which needs to be configured in Web. XML:

<!--global error Handling page configuration-<error-page> <error-code>404</error-code> <location>/common/404. jsp</location> </error-page> <error-page> <error-code>500</error-code> <location >/common/500.jsp</location> </error-page>

Complete Web. XML Configuration Error page:

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" 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_2_5.xsd "> <display-name></display-name> <!--Global error Handling page configuration--  > <error-page> <error-code>404</error-code> <location>/common/404.jsp</location> </error-page> <error-page> <error-code>500</error-code> <location>/common/500.jsp </location> </error-page> </web-app>




Other:

"6" session: Whether to turn on the session function, you may be able to verify this by looking at the Java file converted by JSP.

False, cannot use the session built-in object;

True, the session built-in object can be used.

The buffer size of the "7" buffer:jsp page. The default buffer value is 8KB.

There are nine large built-in objects in the JSP, one of which is an out object, its type is jspwriter, has buffering function, its buffer size is specified by buffer.

Example: We can set the buffer to 1KB for testing, and the purpose of the test is to verify the mechanism of the buffer. The Out object is a JspWriter type with a buffer, whereas response.getwriter () gets an object that is PrintWriter type, does not have a buffer, and outputs the content directly, and we examine the mechanism of the buffer based on the difference between the two.

<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8" buffer= "1kb"%><%system.out.println (" For before check the buffer size: "+ out.getbuffersize ());//Check the buffer size System.out.println (" for before viewing the remaining buffer size: "+ out.getremaining ());// Check the buffer remaining size//If count value is 1023, then 1023 a takes 1023 bytes (byte) less than 1KB, will be output later;//If Count is a value of 1024, which occupies 1KB buffer space, int count = 1023 is first output; for (int i=0;i<count;i++) {out.write ("a");} Out.flush ();//Flush Cache System.out.println ("For after view buffer size:" + out.getbuffersize ());//view buffer size System.out.println (" Check the remaining buffer size after for: "+ out.getremaining ());//view buffer remaining size response.getwriter (). Write (" Hello ");//response.getwriter () Without a buffer, it is output directly. Out.write ("a"); System.out.println ("Last View cache Size:" + out.getbuffersize ());//view buffer size System.out.println ("Last view remaining buffer size:" + Out.getremaining ());//view buffer remaining size%>


"8" Iselignore: Whether the El expression is ignored.



JSP series: (2) JSP basic-page instruction detailed

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.