Browse Source

1110 tomcat使用及手写tomcat

Demi 6 months ago
parent
commit
67a576e62e
39 changed files with 1308 additions and 31 deletions
  1. 1 1
      lesson/src/com/sf/base/Main10.java
  2. 1 3
      lesson/src/com/sf/base/Main12.java
  3. 1 1
      lesson/src/com/sf/base/Main15.java
  4. 1 1
      lesson/src/com/sf/base/Main2.java
  5. 1 1
      lesson/src/com/sf/base/Main20.java
  6. 1 1
      lesson/src/com/sf/base/Main3.java
  7. 1 1
      lesson/src/com/sf/base/Main6.java
  8. 1 1
      lesson/src/com/sf/base/Main9.java
  9. 21 21
      lesson/src/com/sf/json/Main.java
  10. 68 0
      lesson/src/com/sf/leetcode/Solution_LCR012.java
  11. 25 0
      lesson/src/com/sf/socket/Client.java
  12. 44 0
      lesson/src/com/sf/socket/MyTask.java
  13. 47 0
      lesson/src/com/sf/socket/Server.java
  14. 43 0
      lesson/src/com/sf/thread/Main.java
  15. 35 0
      lesson/src/com/sf/thread/MainR.java
  16. 13 0
      lesson/src/com/sf/thread/TestRunnable.java
  17. 14 0
      lesson/src/com/sf/thread/TestThread.java
  18. 46 0
      lesson/src/com/sf/tomcat/RequestHandler.java
  19. 20 0
      lesson/src/com/sf/tomcat/Response.java
  20. 36 0
      lesson/src/com/sf/tomcat/TomcatServerV1.java
  21. 22 0
      lesson/src/com/sf/tomcat/TomcatServerV2.java
  22. 38 0
      lesson/src/com/sf/tomcat/my/HelloServlet.java
  23. 37 0
      lesson/src/com/sf/tomcat/my/LoginServlet.java
  24. 28 0
      lesson/src/com/sf/tomcat/my/MyHttpServlet.java
  25. 42 0
      lesson/src/com/sf/tomcat/my/MyRequestHandler.java
  26. 14 0
      lesson/src/com/sf/tomcat/my/MyServlet.java
  27. 17 0
      lesson/src/com/sf/tomcat/my/MyTomcat.java
  28. 35 0
      lesson/src/com/sf/tomcat/my/Request.java
  29. 38 0
      tomcat-demo/.gitignore
  30. 8 0
      tomcat-demo/.idea/.gitignore
  31. 6 0
      tomcat-demo/.idea/vcs.xml
  32. BIN
      tomcat-demo/.mvn/wrapper/maven-wrapper.jar
  33. 2 0
      tomcat-demo/.mvn/wrapper/maven-wrapper.properties
  34. 316 0
      tomcat-demo/mvnw
  35. 188 0
      tomcat-demo/mvnw.cmd
  36. 50 0
      tomcat-demo/pom.xml
  37. 28 0
      tomcat-demo/src/main/java/com/sf/tomcatdemo/HelloServlet.java
  38. 6 0
      tomcat-demo/src/main/webapp/WEB-INF/web.xml
  39. 13 0
      tomcat-demo/src/main/webapp/index.jsp

+ 1 - 1
lesson/src/com/sf/Main10.java → lesson/src/com/sf/base/Main10.java

@@ -1,4 +1,4 @@
-package com.sf;
+package com.sf.base;
 
 public class Main10 {
 

+ 1 - 3
lesson/src/com/sf/Main12.java → lesson/src/com/sf/base/Main12.java

@@ -1,6 +1,4 @@
-package com.sf;
-
-import java.io.Serializable;
+package com.sf.base;
 
 public class Main12 {
 

+ 1 - 1
lesson/src/com/sf/Main15.java → lesson/src/com/sf/base/Main15.java

@@ -1,4 +1,4 @@
-package com.sf;
+package com.sf.base;
 
 public class Main15 {
     // String创建了几个对象?

+ 1 - 1
lesson/src/com/sf/Main2.java → lesson/src/com/sf/base/Main2.java

@@ -1,4 +1,4 @@
-package com.sf;
+package com.sf.base;
 
 public class Main2 {
 

+ 1 - 1
lesson/src/com/sf/Main20.java → lesson/src/com/sf/base/Main20.java

@@ -1,4 +1,4 @@
-package com.sf;
+package com.sf.base;
 
 import java.io.Serializable;
 

+ 1 - 1
lesson/src/com/sf/Main3.java → lesson/src/com/sf/base/Main3.java

@@ -1,4 +1,4 @@
-package com.sf;
+package com.sf.base;
 
 public class Main3 {
 

+ 1 - 1
lesson/src/com/sf/Main6.java → lesson/src/com/sf/base/Main6.java

@@ -1,4 +1,4 @@
-package com.sf;
+package com.sf.base;
 
 import java.util.Arrays;
 

+ 1 - 1
lesson/src/com/sf/Main9.java → lesson/src/com/sf/base/Main9.java

@@ -1,4 +1,4 @@
-package com.sf;
+package com.sf.base;
 
 public class Main9 {
     public static void main(String[] args) {

+ 21 - 21
lesson/src/com/sf/json/Main.java

@@ -1,21 +1,21 @@
-package com.sf.json;
-
-import com.google.gson.Gson;
-
-public class Main {
-
-    public static void main(String[] args) {
-        Person person = new Person("zhangsan", 18);
-        // 使用gson包提供的 Gson类创建对象
-        Gson gson = new Gson();
-        // 提供了toJson方法 可以将对象转为json
-        String str = gson.toJson(person);
-        // {"name":"zhangsan","age":18}
-        System.out.println(str);
-
-        // 怎么将字符串再转为对象 使用fromJson
-        // 参数对应的是 json字符串 对象对应的类型
-        Person person1 = gson.fromJson(str, Person.class);
-        System.out.println(person1);
-    }
-}
+//package com.sf.json;
+//
+//import com.google.gson.Gson;
+//
+//public class Main {
+//
+//    public static void main(String[] args) {
+//        Person person = new Person("zhangsan", 18);
+//        // 使用gson包提供的 Gson类创建对象
+//        Gson gson = new Gson();
+//        // 提供了toJson方法 可以将对象转为json
+//        String str = gson.toJson(person);
+//        // {"name":"zhangsan","age":18}
+//        System.out.println(str);
+//
+//        // 怎么将字符串再转为对象 使用fromJson
+//        // 参数对应的是 json字符串 对象对应的类型
+//        Person person1 = gson.fromJson(str, Person.class);
+//        System.out.println(person1);
+//    }
+//}

+ 68 - 0
lesson/src/com/sf/leetcode/Solution_LCR012.java

@@ -0,0 +1,68 @@
+package com.sf.leetcode;
+
+public class Solution_LCR012 {
+
+    public static void main(String[] args) {
+        int[] nums = {1, 7, 3, 6, 5, 6};
+        int pivotIndex = new Solution_LCR012().pivotIndex(nums);
+        System.out.println(pivotIndex);
+    }
+
+    // [1,7, 3, 6, 5, 6]
+    public int pivotIndex(int[] nums) {
+        int n = nums.length;
+        // 计算总和
+        int total = 0;
+        for (int i = 0; i < n; i++) {
+            total += nums[i];
+        }
+        // total = 28
+
+        // 计算元素的前缀和 是否符合要求
+        int sum = 0;
+        for (int i = 0; i < n; i++) {
+            //  0 1 8 11 17 22 28
+            // 后面元素之和 = 所有元素之和 - 自身 - 前面元素之和
+            // 11 = 28 - 6 - 11
+            if (sum == total - nums[i] - sum) {
+                return i;
+            }
+            sum += nums[i];
+        }
+        // 找不到符合条件的索引
+        return -1;
+    }
+
+    // 使用正序和 和 逆序和 计算
+    public int pivotIndex1(int[] nums) {
+        int n = nums.length;
+        int[] sort = new int[n + 1];
+        int[] reSort = new int[n + 1];
+
+        for (int i = 1; i <= n; i++) {
+            // sort[1] = sort[0] + nums[0]
+            sort[i] = sort[i - 1] + nums[i - 1];
+        }
+        int index = -1;
+        if (reSort[n - 1] == sort[n - 1]) {
+            index = n - 1;
+        }
+        for (int i = n - 2; i >= 0; i--) {
+            // reSort[n-2] = reSort[n-1] + nums[n-1];
+            reSort[i] = reSort[i + 1] + nums[i + 1];
+            if (reSort[i] == sort[i]) {
+                index = i;
+            }
+        }
+
+
+//        for (int i = 0; i < n; i++) {
+//            if (sort[i] == reSort[i]) {
+//                return i;
+//            }
+//        }
+
+        return index;
+    }
+
+}

+ 25 - 0
lesson/src/com/sf/socket/Client.java

@@ -0,0 +1,25 @@
+package com.sf.socket;
+
+import java.io.OutputStream;
+import java.net.Socket;
+import java.util.Random;
+
+public class Client {
+
+    public static void main(String[] args) throws Exception {
+        // 本机的ip地址 对应 127.0.0.1  端口号是四位
+        // 对于的是 要连接的 服务端的ip地址 和 端口号
+        Socket socket = new Socket("127.0.0.1", 1234);
+        // 作为客户端 需要将数据传给服务端
+        // 此时也可以看作是输入输出
+        OutputStream os = socket.getOutputStream();
+        Random rand = new Random();
+        String message = "Hello Socket " + rand.nextInt();
+        // 把数据写入到outputstream输出流中
+        os.write(message.getBytes());
+        os.close();
+
+        Thread.sleep(10000);
+        socket.close();
+    }
+}

+ 44 - 0
lesson/src/com/sf/socket/MyTask.java

@@ -0,0 +1,44 @@
+package com.sf.socket;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.Socket;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+public class MyTask implements Runnable {
+
+    private Socket socket;
+
+    public MyTask(Socket socket) {
+        this.socket = socket;
+    }
+
+    @Override
+    public void run() {
+        InputStream inputStream = null;
+        try {
+            inputStream = socket.getInputStream();
+            // 通过字节数组  将输入流的内容显示为字符串
+            byte[] bytes = new byte[1024];
+            // 将输入流的数据 写入字节数组中
+            int len = inputStream.read(bytes);
+            // 通过传入内容的大小 来构建字符串
+            String message = new String(bytes, 0, len);
+            System.out.println(message);
+
+            LocalDateTime now = LocalDateTime.now();
+            // 定义格式
+            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+            String str = now.format(formatter);
+            System.out.println("当前时间:" + str);
+
+            // 当前服务端的处理 是一个一个处理的 第一个结束后 要等待5s 才处理第二个
+            // 模拟服务端的处理时间
+            Thread.sleep(5000);
+
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

+ 47 - 0
lesson/src/com/sf/socket/Server.java

@@ -0,0 +1,47 @@
+package com.sf.socket;
+
+import java.io.InputStream;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+public class Server {
+
+    public static void main(String[] args) throws Exception {
+        // 创建一个端口号为 1234的进程
+        ServerSocket serverSocket = new ServerSocket(1234);
+        // 等待客户端的连接
+        while (true){
+            // 通过 accept()方法 可以接收到一个连接过来的客户端
+            Socket socket = serverSocket.accept();
+            // 此时接收数据  是输入
+//            InputStream inputStream = socket.getInputStream();
+//            // 通过字节数组  将输入流的内容显示为字符串
+//            byte[] bytes = new byte[1024];
+//            // 将输入流的数据 写入字节数组中
+//            int len = inputStream.read(bytes);
+//            // 通过传入内容的大小 来构建字符串
+//            String message = new String(bytes, 0, len);
+//            System.out.println(message);
+//
+//            System.out.println(System.currentTimeMillis());
+//            //  打印当前时间
+//            LocalDateTime now = LocalDateTime.now();
+//            // 定义格式
+//            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+//            String str = now.format(formatter);
+//            System.out.println("当前时间:" + str);
+//
+//            // 当前服务端的处理 是一个一个处理的 第一个结束后 要等待5s 才处理第二个
+//            // 模拟服务端的处理时间
+//            Thread.sleep(5000);
+
+            MyTask myTask = new MyTask(socket);
+            Thread thread = new Thread(myTask);
+            thread.start();
+        }
+    }
+}
+
+

+ 43 - 0
lesson/src/com/sf/thread/Main.java

@@ -0,0 +1,43 @@
+package com.sf.thread;
+
+public class Main {
+
+    public static void main(String[] args) {
+//        // 创建一个线程类的对象
+//        TestThread thread1 = new TestThread();
+//        // 触发线程开始运行
+//        thread1.start();
+//
+//        TestThread thread2 = new TestThread();
+//        thread2.start();
+
+        // Thread-1 is running
+        // Thread-0 is running
+
+        for (int i = 0; i < 50; i++) {
+            // Thread.currentThread() 通过类的方法获取当前线程
+            System.out.println(Thread.currentThread().getName() + " " + i);
+            if (i == 10) {
+                // 第一个任务 第一次创建线程的对象
+                new TestThread().start();
+            }
+            if (i == 30) {
+                // 第二个任务 第二次创建线程的对象
+                new TestThread().start();
+            }
+        }
+        // 此时每个线程都处理了 0-49 并且每个i互相独立
+
+        // 匿名内部类
+        new Thread(){
+            @Override
+            public void run(){
+                System.out.println(Thread.currentThread().getName() + " run");
+            }
+        }.start();
+
+        // lambda表达式
+        new Thread(() -> System.out.println(Thread.currentThread().getName() + " run")).start();
+
+    }
+}

+ 35 - 0
lesson/src/com/sf/thread/MainR.java

@@ -0,0 +1,35 @@
+package com.sf.thread;
+
+public class MainR {
+
+    public static void main(String[] args) {
+//        // 创建线程
+//        Runnable runnable = new TestRunnable();
+//        // 先创建runnable对象 然后在Thread构造参数中 传入Runnable
+//        Thread thread = new Thread(runnable);
+//        thread.start();
+//
+//        new Thread(new TestRunnable()).start();
+
+        for (int i = 0; i < 50; i++) {
+            System.out.println(Thread.currentThread().getName()
+                    + " " + i);
+
+            // 如果使用同一个runnable 同一时间触发多个线程
+            // 此时runnable中的变量是共享的
+            TestRunnable runnable = new TestRunnable();
+            if (i == 10) {
+                Thread thread = new Thread(runnable);
+                thread.start();
+//            }
+
+//            if (i == 30) {
+                // 如果使用不同的runnable对象 变量是独立的
+                TestRunnable runnable2 = new TestRunnable();
+                Thread thread1 = new Thread(runnable2);
+                thread1.start();
+            }
+        }
+
+    }
+}

+ 13 - 0
lesson/src/com/sf/thread/TestRunnable.java

@@ -0,0 +1,13 @@
+package com.sf.thread;
+
+public class TestRunnable implements Runnable {
+    int i;
+
+    @Override
+    public void run() {
+        for (; i < 50; i++) {
+            System.out.println(Thread.currentThread().getName()
+                    + " " + i);
+        }
+    }
+}

+ 14 - 0
lesson/src/com/sf/thread/TestThread.java

@@ -0,0 +1,14 @@
+package com.sf.thread;
+
+public class TestThread extends Thread {
+    int i;
+
+    @Override
+    public void run() {
+        // 运行线程的方法
+//        System.out.println(getName() + " is running");
+        for (; i < 50; i++) {
+            System.out.println(getName() + " " + i);
+        }
+    }
+}

+ 46 - 0
lesson/src/com/sf/tomcat/RequestHandler.java

@@ -0,0 +1,46 @@
+package com.sf.tomcat;
+
+import java.io.*;
+import java.net.Socket;
+
+public class RequestHandler implements Runnable {
+
+    private Socket socket;
+
+    public RequestHandler(Socket socket) {
+        this.socket = socket;
+    }
+
+    @Override
+    public void run() {
+        try {
+            // 得到客户端发送的请求内容 http请求
+            InputStream inputStream = socket.getInputStream();
+            // 读取inputstream中的内容
+            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
+            String msg;
+            while ((msg = bufferedReader.readLine()) != null) {
+                if (msg.length() == 0) break;
+                System.out.println(msg);
+            }
+
+            // 返回的结果 http响应
+            String resp = Response.body + "OK";
+            OutputStream outputStream = socket.getOutputStream();
+            outputStream.write(resp.getBytes());
+            outputStream.flush();
+            outputStream.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (socket != null) {
+                try {
+                    socket.close();
+                } catch (IOException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+
+        }
+    }
+}

+ 20 - 0
lesson/src/com/sf/tomcat/Response.java

@@ -0,0 +1,20 @@
+package com.sf.tomcat;
+
+import java.io.OutputStream;
+
+public class Response {
+
+    private OutputStream outputStream;
+
+    public Response(OutputStream outputStream) {
+        this.outputStream = outputStream;
+    }
+
+    public static final String body = "HTTP/1.1 200 OK\r\n"
+            + "Content-Type: text/html; charset=utf-8\r\n"
+            + "\r\n";
+
+    public OutputStream getOutputStream() {
+        return outputStream;
+    }
+}

+ 36 - 0
lesson/src/com/sf/tomcat/TomcatServerV1.java

@@ -0,0 +1,36 @@
+package com.sf.tomcat;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+public class TomcatServerV1 {
+
+    public static void main(String[] args) throws Exception {
+        ServerSocket serverSocket = new ServerSocket(8080);
+        // 检测服务是否关闭
+        while (!serverSocket.isClosed()) {
+            Socket socket = serverSocket.accept();
+            // 得到客户端发送的请求内容 http请求
+            InputStream inputStream = socket.getInputStream();
+            // 读取inputstream中的内容
+            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
+            String msg;
+            while ((msg = bufferedReader.readLine()) != null) {
+                if(msg.length() == 0) break;
+                System.out.println(msg);
+            }
+
+            // 返回的结果 http响应
+            String resp = Response.body + "OK";
+            OutputStream outputStream = socket.getOutputStream();
+            outputStream.write(resp.getBytes());
+            outputStream.flush();
+            outputStream.close();
+            socket.close();
+        }
+    }
+}

+ 22 - 0
lesson/src/com/sf/tomcat/TomcatServerV2.java

@@ -0,0 +1,22 @@
+package com.sf.tomcat;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.ServerSocket;
+import java.net.Socket;
+
+public class TomcatServerV2 {
+
+    public static void main(String[] args) throws Exception {
+        ServerSocket serverSocket = new ServerSocket(8080);
+        // 检测服务是否关闭
+        while (!serverSocket.isClosed()) {
+            Socket socket = serverSocket.accept();
+            // 每有一个客户端的请求 就创建一个线程来处理
+            RequestHandler requestHandler = new RequestHandler(socket);
+            new Thread(requestHandler).start();
+        }
+    }
+}

+ 38 - 0
lesson/src/com/sf/tomcat/my/HelloServlet.java

@@ -0,0 +1,38 @@
+package com.sf.tomcat.my;
+
+import com.sf.tomcat.Response;
+
+import java.io.OutputStream;
+
+// 将所有为实现的方法实现
+public class HelloServlet extends MyHttpServlet{
+
+    @Override
+    public void doGet(Request request, Response response) {
+        doPost(request, response);
+    }
+
+    @Override
+    public void doPost(Request request, Response response) {
+        try {
+            String resp = Response.body + "Hello Servlet";
+            OutputStream outputStream = response.getOutputStream();
+            outputStream.write(resp.getBytes());
+            outputStream.flush();
+            outputStream.close();
+
+        }catch(Exception e){
+            e.printStackTrace();
+        }
+    }
+
+    @Override
+    public void init() {
+
+    }
+
+    @Override
+    public void destroy() {
+
+    }
+}

+ 37 - 0
lesson/src/com/sf/tomcat/my/LoginServlet.java

@@ -0,0 +1,37 @@
+package com.sf.tomcat.my;
+
+import com.sf.tomcat.Response;
+
+import java.io.OutputStream;
+
+public class LoginServlet extends MyHttpServlet{
+
+    @Override
+    public void doGet(Request request, Response response) {
+        doPost(request, response);
+    }
+
+    @Override
+    public void doPost(Request request, Response response) {
+        try {
+            String resp = Response.body + "Login Servlet";
+            OutputStream outputStream = response.getOutputStream();
+            outputStream.write(resp.getBytes());
+            outputStream.flush();
+            outputStream.close();
+
+        }catch(Exception e){
+            e.printStackTrace();
+        }
+    }
+
+    @Override
+    public void init() {
+
+    }
+
+    @Override
+    public void destroy() {
+
+    }
+}

+ 28 - 0
lesson/src/com/sf/tomcat/my/MyHttpServlet.java

@@ -0,0 +1,28 @@
+package com.sf.tomcat.my;
+
+import com.sf.tomcat.Response;
+
+// 接口是规范
+// 抽象类 来实现接口的一部分
+// 所有Http请求用MyHttpServlet处理
+public abstract class MyHttpServlet implements MyServlet{
+
+    // 因为当前的核心处理是 get 和 post
+    //  针对get做get处理 针对post做post处理
+    @Override
+    public void service(Request request, Response response) {
+        // 如果在比较变量和字面量是否相等时  往往把字面量放前 避免空指针问题
+        if("get".equalsIgnoreCase(request.getMethod())){
+            // 可以用GET  也可以用不区分大小写的比较 equalsIgnoreCase
+            doGet(request, response);
+        }else{
+            doPost(request, response);
+        }
+    }
+
+    // 拆成不同的方法 处理不同的请求方式
+    // 用浏览器能发送的只有get请求
+    public abstract void doGet(Request request, Response response);
+    public abstract void doPost(Request request, Response response);
+
+}

+ 42 - 0
lesson/src/com/sf/tomcat/my/MyRequestHandler.java

@@ -0,0 +1,42 @@
+package com.sf.tomcat.my;
+
+import com.sf.tomcat.Response;
+
+import java.net.Socket;
+import java.util.HashMap;
+import java.util.Map;
+
+public class MyRequestHandler implements Runnable {
+
+    private Socket socket;
+    public MyRequestHandler(Socket socket) {
+        this.socket = socket;
+    }
+
+    // 先让 /hello请求对应的处理逻辑 就是HelloServlet
+    // 以后是通过配置来找到的
+//    Map<String,String> map = new HashMap<String, String>();
+//    {
+//        map.put("/hello","HelloServlet");
+//    }
+
+    @Override
+    public void run() {
+        try {
+            // 封装请求与响应
+            Request request = new Request(socket.getInputStream());
+            Response response = new Response(socket.getOutputStream());
+            // 先让 /hello请求对应的处理逻辑 就是HelloServlet
+            if("/hello".equals(request.getUri())){
+                HelloServlet servlet = new HelloServlet();
+                servlet.service(request, response);
+            }else if("/login".equals(request.getUri())){
+                LoginServlet servlet = new LoginServlet();
+                servlet.service(request, response);
+            }
+
+        }catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 14 - 0
lesson/src/com/sf/tomcat/my/MyServlet.java

@@ -0,0 +1,14 @@
+package com.sf.tomcat.my;
+
+import com.sf.tomcat.Response;
+
+// 所有请求都用MyServlet处理
+public interface MyServlet {
+    // 一个处理逻辑的 生命周期
+    // 计算机中生命周期的概念: 指的是事物从出生到死亡的过程
+    void init();
+    // 接收请求 和 返回响应
+    void service(Request request, Response response);
+    // 销毁
+    void destroy();
+}

+ 17 - 0
lesson/src/com/sf/tomcat/my/MyTomcat.java

@@ -0,0 +1,17 @@
+package com.sf.tomcat.my;
+
+import java.net.ServerSocket;
+import java.net.Socket;
+
+public class MyTomcat {
+
+    public static void main(String[] args) throws Exception {
+        ServerSocket serverSocket = new ServerSocket(8080);
+        // 检测服务是否关闭
+        while (!serverSocket.isClosed()) {
+            Socket socket = serverSocket.accept();
+            MyRequestHandler handler = new MyRequestHandler(socket);
+            new Thread(handler).start();
+        }
+    }
+}

+ 35 - 0
lesson/src/com/sf/tomcat/my/Request.java

@@ -0,0 +1,35 @@
+package com.sf.tomcat.my;
+
+import java.io.BufferedReader;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+
+// 请求实体类
+public class Request {
+
+    // 请求方式 get post
+    private String method;
+    // 资源路径 /hello /login
+    private String uri;
+
+    public Request(InputStream inputStream){
+        // 从请求流中解析出想要的部分
+        try {
+            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
+            // GET /hello HTTP/1.1
+            String[] split = reader.readLine().split(" ");
+            method = split[0];
+            uri = split[1];
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+    }
+
+    public String getMethod() {
+        return method;
+    }
+
+    public String getUri() {
+        return uri;
+    }
+}

+ 38 - 0
tomcat-demo/.gitignore

@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store

+ 8 - 0
tomcat-demo/.idea/.gitignore

@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 6 - 0
tomcat-demo/.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$/.." vcs="Git" />
+  </component>
+</project>

BIN
tomcat-demo/.mvn/wrapper/maven-wrapper.jar


+ 2 - 0
tomcat-demo/.mvn/wrapper/maven-wrapper.properties

@@ -0,0 +1,2 @@
+distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.5/apache-maven-3.8.5-bin.zip
+wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar

+ 316 - 0
tomcat-demo/mvnw

@@ -0,0 +1,316 @@
+#!/bin/sh
+# ----------------------------------------------------------------------------
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#    https://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+# ----------------------------------------------------------------------------
+
+# ----------------------------------------------------------------------------
+# Maven Start Up Batch script
+#
+# Required ENV vars:
+# ------------------
+#   JAVA_HOME - location of a JDK home dir
+#
+# Optional ENV vars
+# -----------------
+#   M2_HOME - location of maven2's installed home dir
+#   MAVEN_OPTS - parameters passed to the Java VM when running Maven
+#     e.g. to debug Maven itself, use
+#       set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
+#   MAVEN_SKIP_RC - flag to disable loading of mavenrc files
+# ----------------------------------------------------------------------------
+
+if [ -z "$MAVEN_SKIP_RC" ] ; then
+
+  if [ -f /usr/local/etc/mavenrc ] ; then
+    . /usr/local/etc/mavenrc
+  fi
+
+  if [ -f /etc/mavenrc ] ; then
+    . /etc/mavenrc
+  fi
+
+  if [ -f "$HOME/.mavenrc" ] ; then
+    . "$HOME/.mavenrc"
+  fi
+
+fi
+
+# OS specific support.  $var _must_ be set to either true or false.
+cygwin=false;
+darwin=false;
+mingw=false
+case "`uname`" in
+  CYGWIN*) cygwin=true ;;
+  MINGW*) mingw=true;;
+  Darwin*) darwin=true
+    # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
+    # See https://developer.apple.com/library/mac/qa/qa1170/_index.html
+    if [ -z "$JAVA_HOME" ]; then
+      if [ -x "/usr/libexec/java_home" ]; then
+        export JAVA_HOME="`/usr/libexec/java_home`"
+      else
+        export JAVA_HOME="/Library/Java/Home"
+      fi
+    fi
+    ;;
+esac
+
+if [ -z "$JAVA_HOME" ] ; then
+  if [ -r /etc/gentoo-release ] ; then
+    JAVA_HOME=`java-config --jre-home`
+  fi
+fi
+
+if [ -z "$M2_HOME" ] ; then
+  ## resolve links - $0 may be a link to maven's home
+  PRG="$0"
+
+  # need this for relative symlinks
+  while [ -h "$PRG" ] ; do
+    ls=`ls -ld "$PRG"`
+    link=`expr "$ls" : '.*-> \(.*\)$'`
+    if expr "$link" : '/.*' > /dev/null; then
+      PRG="$link"
+    else
+      PRG="`dirname "$PRG"`/$link"
+    fi
+  done
+
+  saveddir=`pwd`
+
+  M2_HOME=`dirname "$PRG"`/..
+
+  # make it fully qualified
+  M2_HOME=`cd "$M2_HOME" && pwd`
+
+  cd "$saveddir"
+  # echo Using m2 at $M2_HOME
+fi
+
+# For Cygwin, ensure paths are in UNIX format before anything is touched
+if $cygwin ; then
+  [ -n "$M2_HOME" ] &&
+    M2_HOME=`cygpath --unix "$M2_HOME"`
+  [ -n "$JAVA_HOME" ] &&
+    JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
+  [ -n "$CLASSPATH" ] &&
+    CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
+fi
+
+# For Mingw, ensure paths are in UNIX format before anything is touched
+if $mingw ; then
+  [ -n "$M2_HOME" ] &&
+    M2_HOME="`(cd "$M2_HOME"; pwd)`"
+  [ -n "$JAVA_HOME" ] &&
+    JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
+fi
+
+if [ -z "$JAVA_HOME" ]; then
+  javaExecutable="`which javac`"
+  if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
+    # readlink(1) is not available as standard on Solaris 10.
+    readLink=`which readlink`
+    if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
+      if $darwin ; then
+        javaHome="`dirname \"$javaExecutable\"`"
+        javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
+      else
+        javaExecutable="`readlink -f \"$javaExecutable\"`"
+      fi
+      javaHome="`dirname \"$javaExecutable\"`"
+      javaHome=`expr "$javaHome" : '\(.*\)/bin'`
+      JAVA_HOME="$javaHome"
+      export JAVA_HOME
+    fi
+  fi
+fi
+
+if [ -z "$JAVACMD" ] ; then
+  if [ -n "$JAVA_HOME"  ] ; then
+    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
+      # IBM's JDK on AIX uses strange locations for the executables
+      JAVACMD="$JAVA_HOME/jre/sh/java"
+    else
+      JAVACMD="$JAVA_HOME/bin/java"
+    fi
+  else
+    JAVACMD="`\\unset -f command; \\command -v java`"
+  fi
+fi
+
+if [ ! -x "$JAVACMD" ] ; then
+  echo "Error: JAVA_HOME is not defined correctly." >&2
+  echo "  We cannot execute $JAVACMD" >&2
+  exit 1
+fi
+
+if [ -z "$JAVA_HOME" ] ; then
+  echo "Warning: JAVA_HOME environment variable is not set."
+fi
+
+CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
+
+# traverses directory structure from process work directory to filesystem root
+# first directory with .mvn subdirectory is considered project base directory
+find_maven_basedir() {
+
+  if [ -z "$1" ]
+  then
+    echo "Path not specified to find_maven_basedir"
+    return 1
+  fi
+
+  basedir="$1"
+  wdir="$1"
+  while [ "$wdir" != '/' ] ; do
+    if [ -d "$wdir"/.mvn ] ; then
+      basedir=$wdir
+      break
+    fi
+    # workaround for JBEAP-8937 (on Solaris 10/Sparc)
+    if [ -d "${wdir}" ]; then
+      wdir=`cd "$wdir/.."; pwd`
+    fi
+    # end of workaround
+  done
+  echo "${basedir}"
+}
+
+# concatenates all lines of a file
+concat_lines() {
+  if [ -f "$1" ]; then
+    echo "$(tr -s '\n' ' ' < "$1")"
+  fi
+}
+
+BASE_DIR=`find_maven_basedir "$(pwd)"`
+if [ -z "$BASE_DIR" ]; then
+  exit 1;
+fi
+
+##########################################################################################
+# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
+# This allows using the maven wrapper in projects that prohibit checking in binary data.
+##########################################################################################
+if [ -r "$BASE_DIR/.mvn/wrapper/maven-wrapper.jar" ]; then
+    if [ "$MVNW_VERBOSE" = true ]; then
+      echo "Found .mvn/wrapper/maven-wrapper.jar"
+    fi
+else
+    if [ "$MVNW_VERBOSE" = true ]; then
+      echo "Couldn't find .mvn/wrapper/maven-wrapper.jar, downloading it ..."
+    fi
+    if [ -n "$MVNW_REPOURL" ]; then
+      jarUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
+    else
+      jarUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
+    fi
+    while IFS="=" read key value; do
+      case "$key" in (wrapperUrl) jarUrl="$value"; break ;;
+      esac
+    done < "$BASE_DIR/.mvn/wrapper/maven-wrapper.properties"
+    if [ "$MVNW_VERBOSE" = true ]; then
+      echo "Downloading from: $jarUrl"
+    fi
+    wrapperJarPath="$BASE_DIR/.mvn/wrapper/maven-wrapper.jar"
+    if $cygwin; then
+      wrapperJarPath=`cygpath --path --windows "$wrapperJarPath"`
+    fi
+
+    if command -v wget > /dev/null; then
+        if [ "$MVNW_VERBOSE" = true ]; then
+          echo "Found wget ... using wget"
+        fi
+        if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
+            wget "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath"
+        else
+            wget --http-user=$MVNW_USERNAME --http-password=$MVNW_PASSWORD "$jarUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath"
+        fi
+    elif command -v curl > /dev/null; then
+        if [ "$MVNW_VERBOSE" = true ]; then
+          echo "Found curl ... using curl"
+        fi
+        if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then
+            curl -o "$wrapperJarPath" "$jarUrl" -f
+        else
+            curl --user $MVNW_USERNAME:$MVNW_PASSWORD -o "$wrapperJarPath" "$jarUrl" -f
+        fi
+
+    else
+        if [ "$MVNW_VERBOSE" = true ]; then
+          echo "Falling back to using Java to download"
+        fi
+        javaClass="$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.java"
+        # For Cygwin, switch paths to Windows format before running javac
+        if $cygwin; then
+          javaClass=`cygpath --path --windows "$javaClass"`
+        fi
+        if [ -e "$javaClass" ]; then
+            if [ ! -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
+                if [ "$MVNW_VERBOSE" = true ]; then
+                  echo " - Compiling MavenWrapperDownloader.java ..."
+                fi
+                # Compiling the Java class
+                ("$JAVA_HOME/bin/javac" "$javaClass")
+            fi
+            if [ -e "$BASE_DIR/.mvn/wrapper/MavenWrapperDownloader.class" ]; then
+                # Running the downloader
+                if [ "$MVNW_VERBOSE" = true ]; then
+                  echo " - Running MavenWrapperDownloader.java ..."
+                fi
+                ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$MAVEN_PROJECTBASEDIR")
+            fi
+        fi
+    fi
+fi
+##########################################################################################
+# End of extension
+##########################################################################################
+
+export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
+if [ "$MVNW_VERBOSE" = true ]; then
+  echo $MAVEN_PROJECTBASEDIR
+fi
+MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
+
+# For Cygwin, switch paths to Windows format before running java
+if $cygwin; then
+  [ -n "$M2_HOME" ] &&
+    M2_HOME=`cygpath --path --windows "$M2_HOME"`
+  [ -n "$JAVA_HOME" ] &&
+    JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
+  [ -n "$CLASSPATH" ] &&
+    CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
+  [ -n "$MAVEN_PROJECTBASEDIR" ] &&
+    MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
+fi
+
+# Provide a "standardized" way to retrieve the CLI args that will
+# work with both Windows and non-Windows executions.
+MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $@"
+export MAVEN_CMD_LINE_ARGS
+
+WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
+
+exec "$JAVACMD" \
+  $MAVEN_OPTS \
+  $MAVEN_DEBUG_OPTS \
+  -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
+  "-Dmaven.home=${M2_HOME}" \
+  "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
+  ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"

+ 188 - 0
tomcat-demo/mvnw.cmd

@@ -0,0 +1,188 @@
+@REM ----------------------------------------------------------------------------
+@REM Licensed to the Apache Software Foundation (ASF) under one
+@REM or more contributor license agreements.  See the NOTICE file
+@REM distributed with this work for additional information
+@REM regarding copyright ownership.  The ASF licenses this file
+@REM to you under the Apache License, Version 2.0 (the
+@REM "License"); you may not use this file except in compliance
+@REM with the License.  You may obtain a copy of the License at
+@REM
+@REM    https://www.apache.org/licenses/LICENSE-2.0
+@REM
+@REM Unless required by applicable law or agreed to in writing,
+@REM software distributed under the License is distributed on an
+@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+@REM KIND, either express or implied.  See the License for the
+@REM specific language governing permissions and limitations
+@REM under the License.
+@REM ----------------------------------------------------------------------------
+
+@REM ----------------------------------------------------------------------------
+@REM Maven Start Up Batch script
+@REM
+@REM Required ENV vars:
+@REM JAVA_HOME - location of a JDK home dir
+@REM
+@REM Optional ENV vars
+@REM M2_HOME - location of maven2's installed home dir
+@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
+@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending
+@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
+@REM     e.g. to debug Maven itself, use
+@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
+@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
+@REM ----------------------------------------------------------------------------
+
+@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
+@echo off
+@REM set title of command window
+title %0
+@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
+@if "%MAVEN_BATCH_ECHO%" == "on"  echo %MAVEN_BATCH_ECHO%
+
+@REM set %HOME% to equivalent of $HOME
+if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
+
+@REM Execute a user defined script before this one
+if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
+@REM check for pre script, once with legacy .bat ending and once with .cmd ending
+if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %*
+if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %*
+:skipRcPre
+
+@setlocal
+
+set ERROR_CODE=0
+
+@REM To isolate internal variables from possible post scripts, we use another setlocal
+@setlocal
+
+@REM ==== START VALIDATION ====
+if not "%JAVA_HOME%" == "" goto OkJHome
+
+echo.
+echo Error: JAVA_HOME not found in your environment. >&2
+echo Please set the JAVA_HOME variable in your environment to match the >&2
+echo location of your Java installation. >&2
+echo.
+goto error
+
+:OkJHome
+if exist "%JAVA_HOME%\bin\java.exe" goto init
+
+echo.
+echo Error: JAVA_HOME is set to an invalid directory. >&2
+echo JAVA_HOME = "%JAVA_HOME%" >&2
+echo Please set the JAVA_HOME variable in your environment to match the >&2
+echo location of your Java installation. >&2
+echo.
+goto error
+
+@REM ==== END VALIDATION ====
+
+:init
+
+@REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
+@REM Fallback to current working directory if not found.
+
+set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
+IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
+
+set EXEC_DIR=%CD%
+set WDIR=%EXEC_DIR%
+:findBaseDir
+IF EXIST "%WDIR%"\.mvn goto baseDirFound
+cd ..
+IF "%WDIR%"=="%CD%" goto baseDirNotFound
+set WDIR=%CD%
+goto findBaseDir
+
+:baseDirFound
+set MAVEN_PROJECTBASEDIR=%WDIR%
+cd "%EXEC_DIR%"
+goto endDetectBaseDir
+
+:baseDirNotFound
+set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
+cd "%EXEC_DIR%"
+
+:endDetectBaseDir
+
+IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
+
+@setlocal EnableExtensions EnableDelayedExpansion
+for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
+@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
+
+:endReadAdditionalConfig
+
+SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
+set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
+set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
+
+set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
+
+FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
+    IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
+)
+
+@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
+@REM This allows using the maven wrapper in projects that prohibit checking in binary data.
+if exist %WRAPPER_JAR% (
+    if "%MVNW_VERBOSE%" == "true" (
+        echo Found %WRAPPER_JAR%
+    )
+) else (
+    if not "%MVNW_REPOURL%" == "" (
+        SET DOWNLOAD_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.1.0/maven-wrapper-3.1.0.jar"
+    )
+    if "%MVNW_VERBOSE%" == "true" (
+        echo Couldn't find %WRAPPER_JAR%, downloading it ...
+        echo Downloading from: %DOWNLOAD_URL%
+    )
+
+    powershell -Command "&{"^
+		"$webclient = new-object System.Net.WebClient;"^
+		"if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
+		"$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
+		"}"^
+		"[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
+		"}"
+    if "%MVNW_VERBOSE%" == "true" (
+        echo Finished downloading %WRAPPER_JAR%
+    )
+)
+@REM End of extension
+
+@REM Provide a "standardized" way to retrieve the CLI args that will
+@REM work with both Windows and non-Windows executions.
+set MAVEN_CMD_LINE_ARGS=%*
+
+%MAVEN_JAVA_EXE% ^
+  %JVM_CONFIG_MAVEN_PROPS% ^
+  %MAVEN_OPTS% ^
+  %MAVEN_DEBUG_OPTS% ^
+  -classpath %WRAPPER_JAR% ^
+  "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^
+  %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
+if ERRORLEVEL 1 goto error
+goto end
+
+:error
+set ERROR_CODE=1
+
+:end
+@endlocal & set ERROR_CODE=%ERROR_CODE%
+
+if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost
+@REM check for post script, once with legacy .bat ending and once with .cmd ending
+if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat"
+if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd"
+:skipRcPost
+
+@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
+if "%MAVEN_BATCH_PAUSE%"=="on" pause
+
+if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE%
+
+cmd /C exit /B %ERROR_CODE%

+ 50 - 0
tomcat-demo/pom.xml

@@ -0,0 +1,50 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.sf</groupId>
+    <artifactId>tomcat-demo</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <name>tomcat-demo</name>
+    <packaging>war</packaging>
+
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+        <maven.compiler.target>17</maven.compiler.target>
+        <maven.compiler.source>17</maven.compiler.source>
+        <junit.version>5.10.2</junit.version>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>jakarta.servlet</groupId>
+            <artifactId>jakarta.servlet-api</artifactId>
+            <version>5.0.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-api</artifactId>
+            <version>${junit.version}</version>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-engine</artifactId>
+            <version>${junit.version}</version>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-war-plugin</artifactId>
+                <version>3.4.0</version>
+            </plugin>
+        </plugins>
+    </build>
+</project>

+ 28 - 0
tomcat-demo/src/main/java/com/sf/tomcatdemo/HelloServlet.java

@@ -0,0 +1,28 @@
+package com.sf.tomcatdemo;
+
+import java.io.*;
+
+import jakarta.servlet.http.*;
+import jakarta.servlet.annotation.*;
+
+@WebServlet(name = "helloServlet", value = "/hello-servlet")
+public class HelloServlet extends HttpServlet {
+    private String message;
+
+    public void init() {
+        message = "Hello World!";
+    }
+
+    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
+        response.setContentType("text/html");
+
+        // Hello
+        PrintWriter out = response.getWriter();
+        out.println("<html><body>");
+        out.println("<h1>" + message + "</h1>");
+        out.println("</body></html>");
+    }
+
+    public void destroy() {
+    }
+}

+ 6 - 0
tomcat-demo/src/main/webapp/WEB-INF/web.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
+         version="5.0">
+</web-app>

+ 13 - 0
tomcat-demo/src/main/webapp/index.jsp

@@ -0,0 +1,13 @@
+<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
+<!DOCTYPE html>
+<html>
+<head>
+    <title>JSP - Hello World</title>
+</head>
+<body>
+<h1><%= "Hello World!" %>
+</h1>
+<br/>
+<a href="hello-servlet">Hello Servlet</a>
+</body>
+</html>