The essence of Tomcat running program

The essence of Tomcat running program

A web container is a running environment where a Java web program runs, just like when we usually run a Java code, we must first create a JDK environment for it. In the same way, if you want to run a web project, you also need an environment. We usually use Tomcat servers for most of them. The Tomcat server is a web container. It is a single server that can only run web projects. There are other hybrid containers that can run not only web projects but also other projects. If you are interested, you can Google it.

The above explanation is in popular terms. In fact, the Tomcat server is a container that manages servlet components. It implements servlet operation by calling its own methods. The specific process is as follows:

1. When a customer clicks on a URL address, the URL points to a servlet instead of a static page.

2. The container first receives the request instead of the servlet. The container judges that the servlet is required for the request according to the request, so it creates two objects httpservletrequest and httpservletresponse.

3. The container then judges which servlet is needed according to the request, then dispatches the thread in the thread pool, and puts the two objects created in step 2 into the thread.

4. The container calls the service() method of the servlet, and the service() method will call the doGet() or doPost() method.

5. The doGet() method generates a dynamic page, and then fills the page into the response object. At this time, the container still has the response object.

6. At the end of the thread, the container converts the response object into an http response, sends it back to the client, and destroys the two objects httpservletrequest and httpservletresponse.

After understanding the Tomcat container, some people will have questions about servlets. what is it? What is it for? It is actually a Java interface, a specification, its role is very single, because most tasks are completed by the container, its main function is to interactively browse and modify data, generate dynamic web content, it only needs to be implemented The 5 methods of its own are just fine. Next, I will briefly introduce these 5 methods, and I will introduce the servlet in detail in the future:

  • 1.init (servletConfig config) or without parameters (core): initialize the instance, and use the parameters obtained by method 2 as the object for correct initialization of the servlet. Use this method to prepare before processing the request, such as connecting to the database. This method is executed once throughout the cycle.

  • 2. getServletConfig(): Obtain an object containing servlet configuration information. When the server starts, the system automatically encapsulates the configuration information in the entire web.xml configuration file into an object, which implements the servletConfig interface.

  • 3.service () (core) service method: Before the method is called by the container, ensure that the init method is correct. Each time the client sends a request, the method is executed once, and the entire cycle is executed n times.

  • 4. getServletInfo(): Get a description of the servlet.

  • 5. destroy() (core): The server pauses or the program ends, and the temporarily used memory is released. The entire cycle is executed once.

Why javaWeb applications must be deployed on servers such as tomcat

Why do javaWeb applications have to be deployed on servers
such as tomcat? As mentioned above , there are also weblogic, websphere, but I don’t understand why they must be deployed to access these web applications?
------Solutions----------------------

Because these server software can listen to http requests and parse http header information, the header information writes which page the user wants to see, and then these software open the corresponding page of your program and run it to get the result before returning it to the user.
If you write a web program that also writes this monitor, you don't need to rely on software such as tomcat, as long as it conforms to the process of the http protocol.

------Solutions----------------------

1. First, you must use http to access your web application. Your server needs to open a port to listen for requests, right?
2. Since the http protocol is used, do you need to parse the http request from the network?
3. After the analysis, should you access the corresponding application system?
4. After the system processes the request, you need to return the result set to the user so that the user can display it in the browser, right? The middleware is to help you complete these things and has opened the listening port to listen to the user's request, parse the http request sent by the user and then access the application system you specify, and then the page you return is returned to the user through tomcat. It’s okay if you’re not willing to have to deal with it yourself

------Solutions----------------------

Ordinary html, the browser can handle it directly. But like servlets, the browser can't directly parse the contents. These need a tool to deal with.
Allow the browser to correctly identify the information when accessing the host.

Ordinary web, like you go to barbecue outside. You just have to take the meat and roast it yourself.
Dynamic web, like you go to a restaurant for a banquet, even if you bring some pork, beef, greens, oil and salt, you will not be able to achieve your goals at all. The banquet is definitely more enviable than the average barbecue.
However, the banquet must require a team of chefs to work in it to ensure that guests can enjoy it.

Guess you like

Origin blog.csdn.net/weixin_42118981/article/details/115273549