Jsp learning-JSP operating principle, nine implicit objects and common JSP labels

Source: Internet
Author: User

I. JSP running principle when each JSP page is accessed for the first time, the WEB Container will send the request to the JSP Engine (a Java program) for processing. The JSP Engine first translates JSP into a _ jspServlet (essentially a servlet) and calls it according to the servlet call method. Since JSP is translated into servlet during the first access, the first access is usually slow, but the second access, if the JSP Engine finds that JSP has not changed, it will not translate but directly call it, therefore, the execution efficiency of the program will not be affected. When the JSP Engine calls the _ jspServlet corresponding to JSP, nine web development-related objects will be passed or created for _ jspServlet. To facilitate developers to obtain the reference of these web objects when compiling JSP pages, the JSP technology designer specially defines nine corresponding variables, developers can use these variables on the JSP page to quickly obtain references to these nine objects. What are these nine objects and their functions are also the knowledge points frequently examined by the written examination. Note: JspAPI online viewing II. Nine implicit objects 1. Which nine implicit objects ?? As shown in: RequestResponseSessionApplication servletContextConfig servletConfigPage thisExceptionOut jspWriterpageContext why is it an implicit object? I personally think it is because of the built-in jsp and hidden inclusion, so I have defined the exclusive term and implicit object. except for the out and pageContext, the other seven are existing in servlet. Here we mainly introduce the out and pageContext unique to jsp. 2. the out implicit object is used to send text data to the client. The out object is returned by calling the getOut method of the pageContext object. Its function and usage are very similar to the PrintWriter object returned by the ServletResponse. getWriter method. The out implicit object type on the JSP page is JspWriter. JspWriter is equivalent to a PrintWriter with the cache function. You can adjust the cache size by setting the buffer attribute of the page instruction on the JSP page, even disable its cache. The out object calls ServletResponse only when the content written to the out object meets any of the following conditions. the getWriter method returns the PrintWriter object to write the content in the buffer zone of the out object to the buffer zone provided by the Servlet engine:> set the buffer attribute of the page command to disable the out Object Caching function> the buffer of the out object is full> the entire JSP page ends as follows: <% out. writes ("11111"); response. getWriter (). write ("222222"); %> result: Why? The first output of the above 11... is displayed as 222. 111? The main reason is that Jspwriter first writes data to the buffer zone and displays the buffer content only when the jsp page ends. at the same time, the content of the html page on the jsp page is also output through the jspwriter object (that is, output in the out mode). Therefore, copy the Code <body> 00000 <%/out. writes ("11111"); response. getWriter (). write ("222222"); %> </body> the copied Code has the following output, in addition, try not to write any java code on the jsp page. 3. the pageContext implicit object pageContext object is the most important object in JSP technology. It represents the running environment of JSP pages. This object not only encapsulates references to other 8 hidden objects, it is a domain object and can be used to save data. In addition, this object also encapsulates some common operations that are often involved in web development, such as introducing and redirecting other resources and retrieving attributes in other domain objects. Use pageContext to obtain other objects: getException method returns exception implicit object getPage method returns page implicit object getRequest method returns request implicit object getResponse method returns response implicit object getServletConfig method returns config implicit object getServletContext method returns application implicit object getSession method returns session the getOut method of the implicit object returns the meaning of the out implicit object pageContext encapsulating the other eight built-in objects, this is mainly used for custom tags. method of pageContext object: copy the code public void setAttribute (java. lang. string name, java. lang. object value) public java. lang. object getAttribute (Java. lang. string name) public void removeAttribute (java. lang. string name) The pageContext object also encapsulates the public java method for accessing other domains. lang. object getAttribute (java. lang. string name, int scope) public void setAttribute (java. lang. string name, java. lang. object value, int scope) public void removeAttribute (java. lang. string name, int scope) represents the constant PageContext of each domain. APPLICATION_SCOPEPageContext.SESSION_SCOPEPageContext.REQUEST_SCOPEPage Context. PAGE_SCOPE findattricontext method (* focus, search for attributes in each domain) EL expression copy code JavaWeb contains four domain objects, which are sorted in ascending order as pageContext (scope: page) ---> request domain (request time) ---> session (the entire session has failed) ---> application/servletContext (the application context has failed) Example: copy the Code <body> <% request. setAttribute ("data", "abc"); String data = pageContext. getAttribute ("data", PageContext. REQUEST_SCOPE ). toString (); out. write (data); out. write ("<br>"); out. write (pageContext. findAttribute ("Data "). toString (); // copy the code from the page request session application domain to find the corresponding value. %> </body> note: the value in the EL expression depends on pageContext. findAttribute () method. III. common JSP labels 1. <jsp: include> label is used to insert the output content of another resource into the output content of the current JSP page, this introduction method when executing JSP pages is called dynamic introduction. Syntax: <jsp: include page = "relativeURL | <% = expression %>" flush = "true | false"/> the page attribute specifies the relative path of the introduced resource, it can also be obtained by executing an expression. The flush attribute specifies whether to refresh the output content of the current JSP page to the client before inserting the output content of other resources. <Jsp: include> tags are dynamically introduced. The two jsp pages involved in the <JSP: include> tag are translated into two servlets, the contents of the two servlets are merged during execution. The include command is a static introduction. The two JSP pages involved are translated into a servlet, whose content is merged at the source file level. Whether it is the <jsp: include> label or the include command, they will combine the content of the two JSP pages and output them. Therefore, do not duplicate the HTML global architecture labels on these two pages, otherwise, the content output to the client will be a messy HTML document. 2. The <jsp: forward> label is used to forward requests to another resource. Syntax: <jsp: forward page = "relativeURL | <% = expression %>"/> the page attribute specifies the relative path of the resource to which the request is forwarded, it can also be obtained by executing an expression. 3. <jsp: param> label when you use the <jsp: include> and <jsp: forward> labels to introduce or forward requests to other resources, you can use <jsp: param> the tag transmits parameters to the resource. 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> <jsp: param> the name attribute of the tag is used to specify the parameter name, the value attribute is used to specify the parameter value. You can use multiple <jsp: param> tags in the <jsp: include> and <jsp: forward> tags to pass multiple parameters. Example: home. jsp copy Code <% @ page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN "" http://www.w3.org/TR/html4/loose.dtd "> <Html>

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.