JSP for beginners

Source: Internet
Author: User

Yesterday, I spent a lot of time figuring out the operating principles of JSP. In general, JSP is the encapsulated servlet. If you do not believe this, go to the following link:

First, you have to figure out why the file with the suffix jsp can run. The reason is very simple. Because tomcat has been configured, the configuration file is the web in the conf directory of Tomcat. open the file and find the following code:

1     <servlet-mapping>2         <servlet-name>jsp</servlet-name>3         <url-pattern>*.jsp</url-pattern>4     </servlet-mapping>5     <servlet-mapping>6         <servlet-name>jsp</servlet-name>7         <url-pattern>*.jspx</url-pattern>8     </servlet-mapping>

Here is the tomcat configuration. You can see from the above URL that the suffix name can be run, either JSP or jspx, or even changed by yourself, for example, set *. change jspx *. the AAA file in Tomcat can be run. What is this? <URL-pattern> *. JSP </url-pattern> indicates URL configuration. If the JSP file is entered in the URL, the <servlet-Name> JSP </servlet-Name>, find the servlet named JSP, Web. there is another piece of code in XML:

 1 <servlet> 2         <servlet-name>jsp</servlet-name> 3         <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class> 4         <init-param> 5             <param-name>fork</param-name> 6             <param-value>false</param-value> 7         </init-param> 8         <init-param> 9             <param-name>xpoweredBy</param-name>10             <param-value>false</param-value>11         </init-param>12         <load-on-startup>3</load-on-startup>13 </servlet>

Then the servlet-class is found by the servlet name, that is, org. Apache. Jasper. servlet. jspservlet. jspservlet is a servlet that inherits the httpservlet, so the JSP file can be run. The jspservlet class can be found in the Tomcat source file.

Then let's talk about the JSP running process. When the JSP file is run for the first time, it will be compiled into a Java file, then compile the Java file into a class file (these files are placed in the Tomcat work directory ). Note that a JSP file can only be compiled once, that is, it is compiled at the first run. In the future, the JSP file will not be compiled as long as it is not modified. For example, a new demo. jsp is created in Tomcat. When the JSP is run, it is compiled into demo_jsp.java and demo_jsp.class. The demo_jsp class inherits httpjspbase, which is also a Servlet and inherits httpservlet. Its code is as follows:

 1 package org.apache.jasper.runtime; 2  3 import java.io.IOException; 4  5 import javax.servlet.ServletConfig; 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse;10 import javax.servlet.jsp.HttpJspPage;11 import javax.servlet.jsp.JspFactory;12 13 import org.apache.jasper.compiler.Localizer;14 15 /**16  * This is the super class of all JSP-generated servlets.17  *18  * @author Anil K. Vijendran19  */20 public abstract class HttpJspBase 21     extends HttpServlet 22     implements HttpJspPage 23         24     25 {26     27     protected HttpJspBase() {28     }29 30     public final void init(ServletConfig config) 31     throws ServletException 32     {33         super.init(config);34     jspInit();35         _jspInit();36     }37     38     public String getServletInfo() {39     return Localizer.getMessage("jsp.engine.info");40     }41 42     public final void destroy() {43     jspDestroy();44     _jspDestroy();45     }46 47     /**48      * Entry point into service.49      */50     public final void service(HttpServletRequest request, HttpServletResponse response) 51     throws ServletException, IOException 52     {53         _jspService(request, response);54     }55     56     public void jspInit() {57     }58 59     public void _jspInit() {60     }61 62     public void jspDestroy() {63     }64 65     protected void _jspDestroy() {66     }67 68     public abstract void _jspService(HttpServletRequest request, 69                      HttpServletResponse response) 70     throws ServletException, IOException;71 }

The httpjspbase class overrides the service method of httpservlet, but does not overwrite doget () or dopost () (by default, the get method is submitted by running through URL), because the service method is called first, the _ jspservice () method is called in this method. The _ jspservice () method in the httpjspbase class is an abstract method, but the demow.jsp class overrides this method, therefore, the _ jspservice () method in the demow.jsp class is called. The above is enough to prove that JSP is a servlet.

JSP can directly write HTML tags, and can directly embed Java programs. Java program segments can be written in <%> or <%! %>, But in <%> and <%! %> What is the difference? <%! %> It is used to define variables or functions. written here, it is defined as a member variable or a member function in the demow.jsp class. The content written in <%> is directly output in the _ jspservice () method.

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.