Servlet and Tomcat

Time:2020-9-16

1、 Tomcat:

  1. Web service software, small and medium-sized Java EE server, only supports a small number of Java EE specification servlet / JSP.
  2. Modify the port number: in conf/ server.xml , which can also be modified in idea
  3. There are three ways to deploy a web project

    • Put the project directly in the webapps directory.
    • Configure conf/ server.xml file
    • Create an XML file of any name in conf Catalina localhost. Write < context docbase = “D: Hello” / > * virtual directory: the name of XML file

Deploy the project to idea, new project, Java EE. Then configure the Tomcat configuration file

2、 Servlet:

  1. realization:

    1. Define a class and implement the servlet interface: public class servletdemo1 implements servlet
    2. Implement interface method
    3. go web.xml To configure the servlet
<! -- configure servlet -- >
            <servlet>
                <servlet-name>demo1</servlet-name>
                <servlet-class>cn.itcast.web.servlet.ServletDemo1</servlet-class>
            </servlet>
        
            <servlet-mapping>
                <servlet-name>demo1</servlet-name>
                <url-pattern>/demo1</url-pattern>
            </servlet-mapping>
  1. Execution principle:
    Servlet and Tomcat

    1. When the server receives the request from the client browser, it will resolve the request URL path and obtain the resource path of the accessed servlet
    2. lookup web.xml File, whether there is a corresponding < URL pattern > tag body content.
    3. If so, find the corresponding < servlet class > full class name
    4. Tomcat loads the bytecode file into memory and creates its object
    5. Call its method

You can also use servlet3, which you can choose when creating a project,
Servlet3 is relatively simple and does not need to be configured web.xml File, only need annotation configuration, where / demo is urlpatterns(), and web.xml The principle of configuration is the same.

@WebServlet("/demo")
public class ser implements Servlet{

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("init");
    }
  1. Life cycle method in servlet:

    1. Created: execute the init method only once

      • By default, the servlet is created the first time it is accessed
      • You can configure when to execute the servlet creation. Configure under the < servlet > tag

        1. When accessed for the first time, the value of '< load on startup >' is negative
        1. When the server starts, create:<load-on-startup>The value of is 0 or a positive integer
    2. The init method of servlet is executed only once, which indicates that there is only one object in the memory of a servlet. The servlet isSingle caseYes.
      When multiple users access at the same time, there may be a thread safety problem.
      Solution: try not to define member variables in servlets. Even if a member variable is defined, do not modify the value for. For example, buy tickets, define a member variable, and they will share it. If you buy tickets at the same time, or refund tickets, there will be problems.
    3. Provide service: execute the service method, execute multiple times: the service method will be called once every time the servlet is accessed.
    4. Destroyed: execute the destroy method only once.

      Executes when the servlet is destroyed. When the server is shut down, the servlet is destroyed,
      The destroy method is executed only when the server is shut down normally.
      The destroy method is executed before the servlet is destroyed and is generally used to release resources.

Recommended Today

How to share queues with hypertools 2.5

Share queue with swote To realize asynchronous IO between processes, the general idea is to use redis queue. Based on the development of swote, the queue can also be realized through high-performance shared memory table. Copy the code from the HTTP tutorial on swoole’s official website, and configure four worker processes to simulate multiple producers […]