Tomcat's immortality, learning notes of four mainstream middleware integrated by Alibaba P8 boss

import java.io.Writer;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

public class EmbeddedTomcatEx {

public static void main(String[] args) throws LifecycleException,
        InterruptedException, ServletException {

    Tomcat tomcat = new Tomcat();
    tomcat.setPort(8082);

    Context ctx = tomcat.addContext("/", new File(".").getAbsolutePath());

    Tomcat.addServlet(ctx, "Embedded", new HttpServlet() {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) 
                throws ServletException, IOException {
            
            Writer w = resp.getWriter();
            w.write("Embedded Tomcat servlet.\n");
            w.flush();
            w.close();
        }
    });

    ctx.addServletMapping("/*", "Embedded");

    tomcat.start();
    tomcat.getServer().await();
}

}



Let's explain the function of the above code in sections below:

```java
Tomcat tomcat = new Tomcat();
tomcat.setPort(8082);

Tomcat starts on port 8082

Context ctx = tomcat.addContext("/", new File(".").getAbsolutePath());

Each application is mapped to a context. Using this addContext() method, we create a file that is not a JSP file and has no web XML file. We use the root context path and the current working directory of the document library.

Tomcat.addServlet(ctx, "Embedded", new HttpServlet() {
            @Override
            protected void service(HttpServletRequest req, HttpServletResponse resp) 
                    throws ServletException, IOException {
                
                Writer w = resp.getWriter();
                w.write("Embedded Tomcat servlet.\n");
                w.flush();
                w.close();
            }
        });

The addServlet() method adds a new servlet. The servlet uses a stream to respond to some text.

 ctx.addServletMapping("/*", "Embedded");

Servlet mapping controls how to access a servlet named Embedded. For our example, any URL will eventually call our servlet.

tomcat.start();
tomcat.getServer().await();

Finally, the Tomcat server starts.

The server is listening on port 8082, so type the following URL in your Web browser:

http://localhost:8082/

You can see the corresponding effect

summary

Basically, we use embedded Tomcat for the following purposes:

1) Rapid unit testing of Web components such as Java components

Instead of starting / stopping the Tomcat server and opening a Web browser for manual unit testing, you can use embedded Tomcat to automatically perform unit testing.

last

Even job hopping is a learning process. Only comprehensive review, can we better enrich ourselves, arm ourselves, and make our interview road no longer rough! Today, I'd like to share with you a comprehensive collection of Java interview questions on Github, which helped me win the Offer of a large factory and raise my monthly salary to 30K!

Data collection method: Blue portal

I am also the first time to share it with you. I hope I can help you go to your favorite factory! Prepare for gold, silver and four!
There are 20 topics of knowledge points in total, which are:

Dubbo interview topic

JVM interview topics

Java Concurrent interview topic

Kafka interview topics

MongDB interview topic

MyBatis interview topic

MySQL interview topic

Netty interview topic

RabbitMQ interview topic

Redis interview topic

Spring Cloud interview topic

SpringBoot interview topics

zookeeper interview topic

Summary of common interview algorithm questions

Basic topics of computer network

Special topics on Design Patterns


General topics**

[external chain picture transferring... (img-c5WCvTJg-1628503245494)]

Basic topics of computer network

[external chain picture transferring... (img-B3IfJOSu-1628503245495)]

Special topics on Design Patterns

[external chain picture transferring... (img-fO2IpLc1-1628503245495)]

Keywords: Java Back-end Interview Programmer

Added by dewknight on Mon, 27 Dec 2021 23:22:24 +0200