Vernacular Tomcat running

This article is suitable for users who do not understand Tomcat, Spring Servlet, or want to understand more deeply.

Tomcat is a Java application program, its function is to provide users with 通过网络访问网站functions, and the technical term is called Tomcat 网络服务器.

1. How does Tomcat realize the function of allowing users to access through the network?
2. Tomcat and Servlet, JSP relationship?
3. How to create a Tomcat program
4. What you need to know about Servlet

  1. tomcat download

1. How does Tomcat realize the function of allowing users to access websites through the network?

Tomcat is a Servlet/JSP container, which allows users to access websites through the internal program Servlet.

First of all, what is a website? A website is a bunch of Html files, JS files, CSS files, and Image files, which are collectively referred to as website static resources.
If you are local, you can directly open the Html file through the browser, and the Html will automatically load the JS and CSS files. And other people can't see your website.
Tomcat is when you enter a URL in the address bar of the browser, 浏览器发送一个网络请求到服务器after the server receives the request, it returns a bunch of data. Based on the returned data, the browser draws on the screen like a painting and converts it into a page visible to the naked eye.

Just like a port: Tomcat is a seaport, and Serlvet is a container, and the items in the container are JSP. One after another transport ships brought containers to the seaport for unloading. After the seaport processed the containers, they returned new containers to the transport ships.
Tomcat is like a fully constructed seaport, as long as it receives a container, it knows what to do with it and returns a new container.

2. Relationship between Tomcat and Servlet, JSP

Tomcat provides some functions specifically for loading and running Servlet programs.

like:

  • Shipping Ship: "Hi Port. I've got a container"
  • Port: "OK, I called the Servlet to handle this container"
  • Servlet: According to the rules set by the port, the container is shipped to process the JSP files inside.

3. How to create a Tomcat program

Above, we have already seen what Tomcat does, so how should we create a Tomcat?

First of all, you have to keep in mind what you are doing, you are creating an application that allows users to remotely access websites through a browser, and this application just happens to be named Servlet.

Or you can create an application that allows users to access websites remotely through a browser, and name it a cat, a dog, it doesn't matter.

What I'm going to tell you next will require you to pay more attention.

Because we let users access the website on the server through their own browsers. This process can be briefly described as:

  • The user is in the browser address bar ->
  • The browser sends a network request to the server ->
  • The server processes the request and returns data to the browser ->
  • The browser receives the returned data and draws it

So the next thing to consider is how Tomcat running on the server handles the network requests sent by the browser.

Today, you can simply understand that all browsers send are Http requests, (Http requests are understood as a file with writing requirements: the first line writes the name, the second line writes today's date, and the third line writes... ), so the problem becomes

  • How does Tomcat handle Http requests?

How does Tomcat handle Http requests? As we said above, Http has a fixed format, the first line is the name, the second line is the date, the third line..., so we only need to put the first line from the Http
file The data is read out, and the second line of data is read out...

So our problem again translates to

  • How to read data from Http file?

Do you still remember what Tomcat is, it is a seaport, what is the Http sent by the browser, it is a container. What to do with the container? That's right, it's Servlet. The question is transformed again:

  • How does the Servlet process the received Http request?

After the container is processed, a new container needs to be given to the transport ship. Note that the returned container is also a container, so a new problem arises here:

  • How does Servlet return Http request?

JSP is loaded in the container, so there are a series of new problems:

  • 1. How does the Servlet know when Tomcat is looking for it?
  • 2. Should the Servlet be created in advance, wait for the Tomcat call, or create it after receiving the call?
  • 3. Http may contain fruit, clothing, or oil. How should Servlet handle different data in Http?
  • 3. After the Servlet processing is completed, what should I do after returning the new Http? Will it continue to exist, or will it be destroyed?

When things come to this point, you can find that most of the Http processing work is actually processed by Servlet, and dedicated personnel are dedicated to the post, so let Servlet handle Http requests exclusively.

As for Tomcat, just clean up his responsibilities and be responsible for scheduling Servlets. Hmm, looks a lot like our manager, he just needs to open his mouth and the workman will lose his leg.

Creating a Tomcat, like creating a seaport, is a huge project.

How much memory does it occupy, how to receive and process Http tasks and order the tasks to the Servlet to work, whether to arrange the Servlet to wait forever, or to fire the squid when it is done... This involves very complicated content. Fortunately, Apache has already done it for us. This is all done, and Apache Tomcat is made , you just need to download and install it, and let this software handle our original purpose:

Let the functionality Apache Tomcatprovided to the user 通过网络访问网站.

4. What you need to know about Servlet

We already know that Servlet is a wage earner in Tomcat, it handles Http requests, and it also knows how to handle them. In the actual application development process, sometimes, some special data needs to be processed. At this time, we need to tell the Servlet how to process the special data according to the requirements we want.

The following is used Java Spring项目as a demonstration:

We know that the Spring project has built-in Tomcat. In actual development, any Http request will be received by Tomcat and then handed over to Servlet for processing. We want Servlet to process specific data, and we only need to let a class inherit HttpServlet.

That is, you only need to focus on the HttpServlet class provided by Tomcat, and write a class file that inherits HttpServlet

@WebServlet("/myServlet")
public class MyServlet extends HttpServlet {
    
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        System.out.println("自定义 servlet 对 Http请求 Get 方法的处理方式" );

        resp.setContentType("text/html");
        PrintWriter pw = resp.getWriter();
        pw.write("特殊数据");
        pw.flush();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
        super.doGet(req, resp);
        System.out.println("自定义 servlet 对 Http请求 Post 方法的处理方式");
    }
}

After we have written the program, visit your local address http://localhost:8080/myServlet, and you will find that the browser will display 特殊数据four words. This means, you can handle any special data you wish here.

reference reading

Guess you like

Origin blog.csdn.net/win7583362/article/details/125954407