I often ask if there is any difference between JSP and Servlet, what is the connection between the two? In fact, the emergence of servlet technology is very early, it is for Java at the time.
server
Development of end applications. Everyone knows that applet is the application applet, servlet is
server
End small program. 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 by analyzing an JSP operation process, and elaborate from a new perspective. HelloWorld.jsp We take Tomcat 4.1.17
server
Take a look at how the simplest helloworld.jsp is running. Code List 1: HelloWorld.jsp
HelloWorld.jsp
<%
String message = "Hello World!";
%>
<% = message%>
This file is very simple, only defines a string variable and outputs. 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.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.getServletContext ();
CONFIG = pageContext.getServletConfig ();
Session = pageContext.getsession ();
OUT = PageContext.getut ();
_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);
}
}
}
As can be seen from the above, helloWorld.jsp first parsed to a Java class helloworld_jsp.java, which inherits in the org.apache.jasper.Runtime.httpjspBase base class, 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: JSP page parsed code snippets String Message = "Hello World!";
OUT.PRINT (Message);
The parsing process of custom labels typically uses JSP custom labels to encapsulate page display logic in a large web application in a large web application. 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"%>
hEAD>
......
body>
html: html>
We only analyze as an example only as an example of
hEAD>
......
body>
html>
It can be seen that the container has replaced the
The analysis process then, how does the JSP container complete parsing? View Index_Jsp.java files after working directory Jakarta-Tomcat-4.1.17 / work / standalone / localhost / struts-example: 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 {
/ / Define references to processor pools for all custom labels
Private org.apache.jasper.runtime.taghandlerpool;
_jspx_tagpool_bean_message_key;
......
// page class construction method
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 = PageContext.getut ();
_Jspx_out = OUT;
......
IF (_jspx_meth_html_html_0 (pageContext))
Return;
......
}
// Page Releases properties of all custom labels when exiting
Public void _jspdestroy () {
_jspx_tagpool_bean_message_key.release ();
......
}
}
The generated index_jsp.java inherits 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
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. The HTML code is directly output. The following mainly look at the
BEAN: Message Tag List 7: _JSPX_METH_BEAN_MESSAGE_0 () method pieces
/ / Treatment method for Message custom labels
Private boolean _jspx_meth_bean_message_0 (
Javax.Servlet.jsp.tagext.tag _jspx_th_html_html_0,
Javax.servlet.jsp.pageContext pageContext) throws throwable {
Jspwriter out = pageContext.getut ();
/ * ---- 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, the HTML: Bean also needs to get an instance of a label class from the pool, 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. A bean: Message label is defined in Index.jsp, and sets an attribute:
Chihua, a label object instance, in order to improve the running efficiency, Tomcat is done to all custom labels, and the pool is done by org.apache.jasper. Runtime.taghandlerpool class. TagHandlerPool classes have two main methods, the code is as follows: Code List 8: TagHandlerpool.java
Public class taghandlerpool {
Private static final int max_pool_size = 5;
PRIVATE TAG [] Handlers;
Public synchronized tag get (class handlerclass) throws jspexception {...}
Public synchronized void reuse (tag handler) {...}
}