JSP operation insider

xiaoxiao2021-03-06  44

I often ask if there is any difference between JSP and Servlet, what is the connection between the two? In fact, the performance of servlet technology is very early, which is developed for the application of Java server. Everyone knows that applet is a applet, servlet is a server-side applet. However, after Microsoft's ASP technology appears, the output statement of a row of rows when using the servlet, is very awkward, and this is the case for complex layout or display pages. JSP is to meet this need to be developed on servlet technology. It can be seen that there is an intrinsic blood relationship between JSP and Servlet. When learning JSP, if you can seize this connection, you can understand the operational mechanism of JSP and achieve the effect of halving. This article will pass the inside of JSP operation through the analysis of a JSP operation process, and the technical points in some JSPs are set forth from a new perspective.   helloworld.jsp   We take Tomcat 4.1.17 servers as an example to see how the simplest helloWorld.jsp runs. Code List 1: HelloWorld.jsp HelloWorld.jsp <% String Message = "Hello World!";%> <% = Message%> This file is very simple, only a string variable is defined, and output. Put this file in the Tomcat's WebApps / root / directory, start Tomcat, access http: // localhost: 8080 / helloworld.jsp, the output in the browser is "HelloWorld!" Let's take a look at Tomcat I did what.

Go to Tomcat / Work / Standalone / LocalHost / _Dext, you can find the following helloWorld_jsp.java, this file is the source file generated when Tomcat resolves HelloWorld.jsp: Code List 2: HelloWorld_Jsp.java package org.apache.jspa package Org.Apache.jsp Import javax.servlet. *; import javax.servlet.http. *; import javax.servlet.jsp. *; import org.apache.jasper.Runtime. *; public class helloworld_jsp Extends httpjspBase {... public void _jspService (HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {JspFactory _jspxFactory = null; javax.servlet.jsp.PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; JspWriter _jspx_out = null; try {_jspxFactory = JspFactory.getDefaultFactory (); response.setContentType ( "text / html; charset = ISO-8859-1"); pageContext = _jspxFactory.getPageContext (this , Request, Response, Null, True, 8192, True; Application = PageContext (); config = pagecontext.getServletConfig (); session = pageContext.getSession (); out = pageContext.get (); _jspx_out = out; string message = "Hello World!"; out.print (message);} catch (throwable t) {OUT = _jspx_out; if (Out! ! = null && out.getBufferSize () = 0) out.clearBuffer (); if (pageContext = null) pageContext.handlePageException (t);!!} finally {if (_jspxFactory = null) _jspxFactory.releasePageContext (pageContext);} }} It can be seen from the above, helloworld.jsp first parsed to a Java class helloworld_jsp.java, which inherits the ORG.APache.jasper.Runtime.httpjspBase base class, and HttpjspBase implements the HTTPSERVLET interface. It can be seen that JSP first compiled into a servlet before running, which is the key to understanding JSP technology.

We also know that there are several objects in the JSP page, such as PageContext, Application, Config, Page, Session, Out, etc. You may be strange, why you can use these built-in objects directly in the code snippet in the JSP. Observe _JspService () method, in fact, these built-in objects are defined here. Initialize these built-in objects before analyzing the code snippet in the JSP file. First, call the JSPFactory's getDefaultFactory () method to obtain a reference to a JSPFactory object of the container implementation (refer to Tomcat 4.1.17). JSPFactory is an abstract class defined in the javax.servlet.jsp package, which defines two static methods set / getDefaultFactory (). The SET method is placed by the JSP container (Tomcat). When the page servlet (ie HelloWorld_JSP class) is placed, it can directly call the JSPFactory.getDefaultFactory () method to get the implementation class of this JSP factory. Tomcat is called org.apache.jasper.Runtime.jspFactoryImpl class. Then, call this JSPFactoryImpl's getPageContext () method, populate a PageContext, and assign the built-in variable PageConext. Other built-in objects are obtained via this pageContext. The specific process see the code above, and details will not be described here. The page servlet's environment is complete, and the page begins to parse the page. The HelloWorld.JSP page only defines a string variable and then outputs it directly. The parsed code is as follows: Code List 3: The code snippet after the JSP page is parsed String Message = "Hello World!"; Out.print (Message); custom label in a large web application, usually use JSP Custom label to package page display logic. An analysis of the parsing process of the custom label is very helpful to our in-depth understanding of the operational mechanism of customized labels. Below we run as an example with the homepage of Struts-Example apps attached in Struts1.1. The download address of Index.jsp Struts1.1b containing custom labels is http://jakarta.apache.org/struts/index.html. Put the downloaded package, you can find Struts-Example.war under the WebApps directory. Copy the WAR package to the Tomcat's WebApps directory, Tomcat will automatically install this package. Access the Struts-Example App to HTTP: // localhost: 8080 / Struts-Example in the browser, the home page of the app is displayed (see Figure 1).

Figure 1 Home code list 4: index.jsp <% @ Page ContentType = "text / html; charset = UTF-8" Language = "java"%> <% @ Taglib URI = "/ Web-INF / STRUTS- Bean.TLD "prefix =" bean "%> <% @ Taglib URI =" / web-inf / struts-html.tld "prefix =" html "%> <% @ Taglib URI =" / web-inf / struts- Logic.TLD "prefix =" logic "%>

......

We are only in index.jsp

The parship parsing is an example of analysis, how does the container analyze this custom label into HTML output. The above code omits other display portions of the page. First, look at the source file of the page in the browser above:

MailReader Demonstration Application (Struts 1.0) ... visible, the container has already

Replace with a string, the title of the page is displayed. The analysis process then, how does the JSP container complete parsing? View in the working directory jakarta-tomcat-4.1.17 / work / standalone / localhost / struts-example, subsequent index_jsp.java file: code list 5: index_jsp.java package org.apache.jsp; import javax.servlet. * Import javax.servlet.http. *; import javax.servlet.jsp. *; import org.apache.jasper.Runtime. *; public class index_jsp extends httpjspbase {file: / / For all custom labels Define processor pool classes rEFERENCE private org.apache.jasper.runtime.TagHandlerPool; _jspx_tagPool_bean_message_key; ...... file: // page class constructor public index_jsp () {_jspx_tagPool_bean_message_key = new org.apache.jasper.runtime.TagHandlerPool (); ......} public void _jspService (HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {...... _jspxFactory = JspFactory.getDefaultFactory (); response.setContentType ( "text / html; charset = UTF-8"); pageContext = _jspxFactory.getPageContext ( This, Request, Response, Null, True, 8192, True; Application = pageContext.getServletContext (); config = pagecontext.getServletConfig (); session = pagecontext.getSession (); out = pagecon text.getOut (); _jspx_out = out; ...... if (_jspx_meth_html_html_0 (pageContext)) return; ......} file: // page is released when the process exits all public properties of custom tags void _jspDestroy () {_jspx_tagPool_bean_message_key.release () ; ...}} The generated index_jsp.java inherited in org.apache. Jasper.Runtime.httpjspBase. Research this document provides us to understand the operational mechanism of custom labels. As can be seen from the above, Tomcat first defines each custom tag and instantizes a TagHandlerPool object for each custom tag. The processing method of the page covers the _ jspservice () method of the parent class, _jspservice method first initializes the environment and assigns a built-in object. Since the index.jsp page is wrapped in a tag package, Tomcat generates a private method for each tab.

The processing method of the label is _Jspx_meth_html_html_0 (). The naming specification of this method can also be seen from here, "_jspx_meth tag prefix tag name This tag appears in the JSP page." Other tags are included in the label, so other tags are parsed in the _jspx_meth_html_html_0 () method. For specific code implementations, see Caidi.com http://linux.ccidnet.com Journal Browse 2003 No. 6. In the _jspx_meth_html_html_0 () method, you first get an instance of org.apache.struts.taglib.html.htmltag from the _jspx_tagpool_html_html_locale pool, then set the page context of this Tag instance, due to the HTML: HTML tag is page The top label, so its Parent is NULL. Then parse the contents of the label. HTML code direct output, the following mainly look at the label

Analysis of the label. Analysis of the Bean: Message Tag is similar to HTML: HTML, Tomcat also puts it in a separate method _jspx_meth_bean_message_0 (). bean: message tag parsing code in Listing 7: _jspx_meth_bean_message_0 () method // fragment processing method private boolean _jspx_meth_bean_message_0 message custom tags (javax.servlet.jsp.tagext.Tag _jspx_th_html_html_0, javax.servlet.jsp.PageContext pageContext) throws Throwable {JspWriter out = pageContext.getOut (); / * ---- bean: message ---- * / org.apache.struts.taglib.bean.MessageTag _jspx_th_bean_message_0 = (org.apache.struts.taglib.bean.MessageTag ) _jspx_tagPool_bean_message_key.get (org.apache.struts.taglib.bean.MessageTag.class); _jspx_th_bean_message_0.setPageContext (pageContext); _jspx_th_bean_message_0.setParent (_jspx_th_html_html_0); _jspx_th_bean_message_0.setKey ( "index.title"); int _jspx_eval_bean_message_0 = _jspx_th_bean_message_0. doStartTag (); if (_jspx_th_bean_message_0.doEndTag () == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) return true; _jspx_tagPool_bean_message_key.reuse (_jspx_th_bean_message_0); return false;} Similarly, html: bean need from the pool Get an instance of a label class and then set the environment. Not detailed here. We only focus on the special processing section of the MessageTag custom label class. The development of custom labels is not within the scope of this article. Define a bean: Message tag in Index.jsp, and set an attribute:

转载请注明原文地址:https://www.9cbs.com/read-52120.html

New Post(0)