How Tomcat runs JSP: A test tool, and a tomcatjsp Test Tool
I recently read JSP/Servlet in my free time. I used to only know how to use JSP, But I know little about its running principle. Now I want to take some notes for reference.
First, briefly describe the running process, and then make a brief analysis based on the Tomcat source code.
JSP running process:
Step 1: translate JSP files into servlet Source Code;
Step 2: Compile the servlet Source code into a bytecode file;
Step 3: load the bytecode and instantiate and initialize it;
Step 4: Call the service method to provide services;
A Brief Analysis of the source code is as follows:
1. Several important files involved in source code
JspServlet. java, JspServletWrap. java, JspRuntimeContext. java, Compiler. java, JDTCompiler. java (AntCompiler. java)
2. Detailed code analysis
JspServlet involves two important methods: service method and serviceJspFile. service method is mainly used to obtain the requested JSP resource path, and serviceJspFile is mainly used to build JspServletWrap.
JspRuntimeContext mainly provides JspServletWrap containers (using HashMap sets ).
Compiler is mainly used to translate JSP files and compile them into bytecode. The specific bytecode generation implementation is implemented by its sub-classes (using the template method design mode ).
JDTCompiler (AntCompiler) is mainly used to generate bytecode files.
JspServletWrap is mainly used to instantiate the Servlet and call init for initialization, and call the servlet service method to process requests.
Further exploration into the specific implementation details. I will write this article today. My level is limited. If you have any questions, please advise!