Take a closer look at the relationship between JSP application running and Servlet

Source: Internet
Author: User

Some people often ask what is the difference between JSP and Servlet and what is the relationship between them? In fact, Servlet technology was developed for Java server applications very early. We all know that Applet is an application Applet, and Servlet is a server Applet. However, when Microsoft's ASP technology emerged, the output statement of one row was very clumsy when Servlet was used to respond to the output, especially for Complex layout or display pages. JSP is developed on the Servlet technology to meet this requirement. It can be seen that there is an internal kinship between JSP and Servlet. When learning JSP, if we can grasp this connection, we can have a deeper understanding of the operating mechanism of JSP applications and get twice the result with half the effort.

This article analyzes the running process of a JSP application, goes deep into the inside story of JSP operation, and elaborates some technical points in JSP from a new perspective.

HelloWorld. jsp

Take the Tomcat 4.1.17 server as an example to see how the simplest HelloWorld. jsp application runs.

Code List 1: HelloWorld. jsp

HelloWorld. jsp

 
 
  1. < %  
  2.  String message = "Hello World!";  
  3. %> 
  4. < %=message%>  

This file is very simple. It only defines a String variable and outputs it. Put this file in the Tomcat websiteroot directory, start Tomcat, access http: // localhost: 8080/HelloWorld. jsp in the browser, and the output in the browser is "HelloWorld !"

Let's take a look at what Tomcat has done. Go to the workStandalonelocalhost _ directory of Tomcat and find HelloWorld_jsp.java, Which is the source file generated when Tomcat parses HelloWorld. jsp:

Code List 2: HelloWorld_jsp.java

 
 
  1. package org.apache.jsp;  
  2. import javax.servlet.*;  
  3. import javax.servlet.http.*;  
  4. import javax.servlet.jsp.*;  
  5. import org.apache.jasper.runtime.*;  
  6. public class HelloWorld_jsp extends HttpJspBase {  
  7.  ......  
  8. public void _jspService(HttpServletRequest request,   
  9. HttpServletResponse response)throws java.io.IOException, ServletException  
  10.  {  
  11.   JspFactory _jspxFactory = null;  
  12.   javax.servlet.jsp.PageContext pageContext = null;  
  13.   HttpSession session = null;  
  14.   ServletContext application = null;  
  15.   ServletConfig config = null;  
  16.   JspWriter out = null;  
  17.   Object page = this;  
  18.   JspWriter _jspx_out = null;  
  19.   try {  
  20.    _jspxFactory = JspFactory.getDefaultFactory();  
  21.    response.setContentType("text/html;charset=ISO-8859-1");  
  22.    pageContext = _jspxFactory.getPageContext(this, request, response,nulltrue, 8192, true);  
  23.    application = pageContext.getServletContext();  
  24.    config = pageContext.getServletConfig();  
  25.    session = pageContext.getSession();  
  26.    out = pageContext.getOut();  
  27.    _jspx_out = out;  
  28.    String message = "Hello World!";  
  29.    out.print(message);  
  30.   } catch (Throwable t) {  
  31.    out = _jspx_out;  
  32.    if (out != null && out.getBufferSize() != 0)  
  33.     out.clearBuffer();  
  34.    if (pageContext != null) pageContext.handlePageException(t);  
  35.   } finally {  
  36.   if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);  
  37.   }  
  38.  }  
  39. }  

As shown above, HelloWorld. jsp is parsed into a Java class HelloWorld_jsp.java at runtime, which inherits from the org. apache. jasper. runtime. HttpJspBase base class and implements the HttpServlet interface. It can be seen that the JSP application is first compiled into a Servlet before running, which is the key to understanding JSP technology.

We also know that the JSP page contains several built-in objects, such as pageContext, application, config, page, session, and out. You may be surprised, why can these built-in objects be used directly in JSP code snippets. Observe the _ jspService () method. In fact, these built-in objects are defined here. Before parsing code snippets in JSP files, initialize these built-in objects.

First, call the getDefaultFactory () method of JspFactory to obtain the reference of a JspFactory object in Tomcat 4.1.17. JspFactory is an abstract class defined in the javax. servlet. jsp package. It defines two static methods: set/getdefafactory Factory (). The set method is embedded when the JSP Container Tomcat instantiates the page Servlet (HelloWorld_jsp class). Therefore, you can directly call JspFactory. getdefafactory Factory () to obtain the implementation class of this JSP factory. Tomcat calls the org. apache. jasper. runtime. JspFactoryImpl class.

Then, call the getPageContext () method of JspFactoryImpl, fill in a PageContext, and assign it to the built-in variable pageConext. Other built-in objects are obtained through the pageContext. For details about the process, refer to the code above. After the Servlet environment is set, parse the page. The HelloWorld. jsp page defines only one String variable and then outputs it directly. The parsed code is as follows:

Code List 3: code snippet after JSP page Parsing

 
 
  1. String message = "Hello World!";  
  2. out.print(message);  

Above, the JSP application running process should be clear.

  1. Fully use the tag function to compile JSP application JSTL)
  2. JSP line feed solution
  3. Simplified code in JSP expressions
  4. Analysis of JSP server
  5. Monitor JVM available memory in JSP

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.