Deploy Tomcat project

Time:2020-9-26

Start Tomcat directly on MAC
Switch to the directory apache-tomcat-8.5.32/bin
Execute the command sh startup.sh
Tomcat started can be seen on terminal

visit http://localhost :8080/ bingo

Execute the command sh when closing Tomcat shutdown.sh

Run a Tomcat Directory Project apache-tomcat-8.5.32/webapps

For example, there is a manager project in this directory, which provides a index.jsp
Deploy Tomcat project

visit http://localhost :8080/manager/ index.jsp

For example, it is configured in examples web.xml One of them

 <servlet-mapping>
        <servlet-name>HelloWorldExample</servlet-name>
        <url-pattern>/servlets/servlet/HelloWorldExample</url-pattern>
    </servlet-mapping>

visit http://localhost :8080/examples/servlets/servlet/HelloWorldExample

Idea Tomcat web with only one Servlet
Create a new Java Web project
New project > java enterprise > select Tomcat as application server > check create project from template

Create a servlet in the web.xml Configure the corresponding servlet configuration in

Configure Tomcat (the configuration process is omitted). When configuring deployment, the application context is configured with the same name as the project

Start Tomcat and visit http://localhost :8080/selfjavawebproj/myservlet
Self Java webproj is the name of the web application. Myservet needs to be in the web.xml Path configured in

Container level of Tomcat
The servlet container of Tomcat is divided into four layers: Engine > host > context > wrapper
The innermost wapper represents a servlet, context represents an application, an application contains multiple servlets, host represents a site, there can be multiple applications under a site, and engine represents the engine, which is used to manage multiple sites.
Conf in Tomcat/ server.xml In, you can configure multiple services
Take my profile as an example

<server>
  < service name = "Catalina" > // a service can have multiple connectors and one engine
 <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
 <Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
 < engine name = "Catalina" defaulthost = "localhost" > // contains multiple hosts
 <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
 </Engine>
 </service>
 <service>
 </service>
</server>

When Tomcat implements the container, it defines the container, which defines the role of parent child, and assembles each level of container through the combination mode

public interface Container extends Lifecycle {
    public void setName(String name);
    public Container getParent();
    public void setParent(Container container);
    public void addChild(Container child);
    public void removeChild(Container child);
    public Container findChild(String name);
}

http://localhost:8080/selfjavawebproj/myservlet
When this request is made,
1. First, select service and engine according to the protocol and port number. Corresponding to the above configuration, there is only one service and the connector supports HTTP1.1 and port 8080. Therefore, the service with Catalina name and engine with Catalina name are found
2 find the corresponding host according to the domain name localhost
3. Find the context according to the URL, so in the previous configuration, the application context is also configured with the application name, where context is selfjava webproj
4 find the wrapper according to the URL, that is, according to the configured path in the web.xml Find the corresponding servlet implementation class in

However, it is not only when the final wrapper starts to process the request. Each layer in the middle will process the request at this level. For example, the engine gets the request first, and after processing the logic of the engine layer, it will send the request to its child host layer. Finally, it will arrive at the wrapper pipeline responsibility chain mode

Specifically, in tomcat, this is pipeline valve pipeline

Value represents a processing point

public interface Valve {
  public Valve getNext();
  public void setNext(Valve valve);
  public void invoke(Request request, Response response)
}

The invoke in value represents the processing logic of the current processing point, and GetNext represents getting the next processing point, getNext.invoke This triggers the processing logic for the next processing point

public interface Pipeline extends Contained {
  public void addValve(Valve valve);
  public Valve getBasic();
  public void setBasic(Valve valve);
  public Valve getFirst();
}

Pipeline maintains the value chain. First is the first in the chain, basic is the last in the chain, and basic is the first value associated with the next level container
The basic of the last level wrapper points to the filter chain, calls the dofilter method in the filter chain, and finally goes to the service method in the servlet