Welcome to “Algorithms and the Beauty of Programming” ↑ pay attention to us!

This article was first published on the wechat official account “Beauty of Algorithms and Programming”. Please follow us and learn more about this series of articles in time.

In Java Web development, you might wonder if Tomcat is a Servlet runtime environment (container), and all requests that go through Tomcat are handled by a Servlet. Servlet is a Java class, JSP is not, so how can JSP run in Tomcat?

In fact, A JSP is a special form of Servlet, and each JSP page is an instance of a Servlet — a JSP page is compiled by the system into a Servlet, which is responsible for responding to user requests. JSP is also a simplification of servlets, and when you use JSP, you still use servlets because each JSP page in a Web application is generated by a Servlet container.

According to the above analysis, we have a question: why JSP is a Servlet, according to what to determine it?

Why are JSP servlets

Start by writing a simple test.jsp page in IntelljIdea

When Tomcat is started, you can find the following files in the Tomcat \work\Catalina\localhost\ROOT\org\apache\ JSP directory:

These two files are the corresponding.java and.class files generated by the test.jsp compiled by the system. The test_jsp.java file is opened

As you can see, this test_jsp class inherits HttpJspBase, And implement the org. Apache. Jasper. Runtime. JspSourceDependent and org. Apache. Jasper. The runtime. JspSourceImports these two interfaces, We know that the way to determine if a class is a Servlet is to see if it implements the Servlet interface indirectly or directly, so we need to see if our test_jsp class implements the interface indirectly or directly. Here it doesn’t implement the Servlet interface directly, So while we can’t see from this that test_jsp is a Servlet, we can wonder if HttpJspBase implements this interface.

So let’s take a closer look at the HttpJspBase class inheritance structure.

Let’s look at the source of the HttpJspBase class

As you can see from the above, this class inherits HttpServlet, which we already know implements the Servlet interface indirectly. If you don’t know, you can use IntelljIdea to hover the mouse over HttpJspBase by holding CTRL + Alt + U to view its inheritance diagram:

Figure 1-1 HttpJspBase inheritance relationship

This should make it clear why JSP is a Servlet.

2 Tomcat process JSP file analysis

From the above analysis, we already know that a JSP is a Servlet. How does a JSP translate into a Servlet? Each Servlet needs to be configured in the web.xml file so that the browser can access the Servlet. Now the test.jsp does not do any configuration in our project’s web.xml. How does the browser access the JSP page? Before we answer this question, let’s look at how Tomcat responds to static resources.

DefaultServlet introduction

Essentially, Tomcat does the same thing for all static resources, meaning that in all places where you don’t configure URL matching, Tomcat’s global configuration takes over. There is a web. XML file in the Tomcat conf directory. If you open it, you will find the following description:

Below you’ll see the Servlet declaration for this global processing, as shown below

The servlet-mapping for DefaultServlet is configured like this:

Since url-pattern is set to /, it should be able to respond to all requests. This is actually a match for all servlet-mapping requests that you don’t define, as explained in the figure above. The self-defined servlets take effect preferentially because the mapping configuration of Servlets in Tomcat is initialized in strict accordance with the declared order and responds to requests in this order. If one of the servlets can respond to requests, it is used. Have relevant questions can refer to the blog:

K1121.iteye.com/blog/156424…

To sum up, all requests that pass through Tomcat are handled by a servlet. If a request does not match any application-specific servlet, it is routed to Tomcat’s DefaultServlet, which is called DefaultServlet. DefaultServlet is configured in /conf/web.xml.

JspServlet is introduced

In the previous section, we learned that Tomcat uses DefaultServlet to handle all static resources. In this section we look at how a JSP request is answered. Similar to DefaultServlet, JSP handling does not need to be configured separately, but exists as a global configuration in /conf/web.xml. The corresponding processing class is JspServlet, which handles all JSP requests. Also if we open /conf/web.xml, we can see the following code and comments:

  

By looking at the comments we can see what this configuration does. Looking down we can see the mapping configuration of the JspServlet, whose URL-pattern is *.jsp and *.jspx. So it can intercept all JSP requests and react accordingly.

Here we see why the browser is still accessible even though we didn’t do anything to configure the JSP files ourselves.

JSP to Servlet

Java and xxx_jsp.class files are generated in the org/ apache/JSP directory. Open the test_jsp. Java file that we already generated. The file structure is shown below:

 

The main conversion action is performed in the method _japService(), as shown in the screenshot of the Servlet class below, which inserts initialization of session, Application, and other objects, all of which are retrieved from the pageContext object at the page level.

 

Where is the Java code in the page? In the conversion process, the HTML page element content can be interpreted as output directly to the front-end page through out.write(), and the Java code (the content contained in <%%>) is written directly to the class for execution. Some code screenshots are as follows:

The content in the red box is the Java code in our JSP page <% %>. The <% %> is removed directly in the transformation and put into the class code, while the rest can be interpreted as out.write() directly to the front-end page. So far we have explained how Tomcat handles JSP files.

3 summary

This article carries on the detailed analysis to the JSP operation principle, can draw the following flow chart:

 

Figure 3-1 WORKING principle diagram of JSP page

According to the working principle diagram of JSP page above, we can get the following conclusions:

– The JSP file must run inside the JSP server.

– JSP files must generate servlets to execute.

JSPS and servlets have the following transformations:

– The static content of the JSP page and JSP scripts are converted to the xxxService() method of the Servlet, similar to the service() method when you create your own Servlet.

– JSP declaration part, converted to member part of the Servlet. All the JSP declaration section can use private, protected, public, such as the static modifier, other places are not.

-jsp output expression (<%=.. %> section), the output expression is converted to an output statement in the Servlet’s xxxService() method.

– The nine built-in objects are either parameters to or local variables of the xxxService() method, so the nine built-in objects can only be used in JSP scripts and output expressions.

\

More interesting articles:

Where2go team



Wechat: The beauty of algorithms and programming

Long press to identify the QR code to follow us!

Tips: Click on the lower right corner of the page “Write a message” to comment, looking forward to your participation! Looking forward to your forwarding!