小小小小涛

内嵌tomcat最简单用法

maven项目引入内嵌tomcat依赖

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>8.5.37</version>
</dependency>

编写如下main方法

 1 package com;
 2 
 3 import org.apache.catalina.Context;
 4 import org.apache.catalina.LifecycleException;
 5 import org.apache.catalina.startup.Tomcat;
 6 import org.apache.tomcat.util.descriptor.web.FilterDef;
 7 import org.apache.tomcat.util.descriptor.web.FilterMap;
 8 import org.springframework.web.filter.OncePerRequestFilter;
 9 
10 import javax.servlet.Filter;
11 import javax.servlet.FilterChain;
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import java.io.IOException;
17 import java.nio.charset.Charset;
18 
19 /**
20  * @author allen
21  * @date 2019-01-22 13:20
22  */
23 public class TomcatWebServer {
24     public static void main(String[] args) {
25         Tomcat tomcat = new Tomcat();
26         HttpServlet httpServlet = new HttpServlet() {
27             @Override
28             protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
29                 resp.getWriter().write("hello, i'm embed tomcat");
30             }
31 
32             @Override
33             protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
34                 doGet(req, resp);
35             }
36         };
37         tomcat.setHostname("localhost");
38         tomcat.setPort(8080);
39         final Context context = tomcat.addContext("/embed-tomcat", null);
40         Tomcat.addServlet(context, "dispatch", httpServlet);
41         context.addServletMappingDecoded("/dispatch", "dispatch");
42         Filter filter = new OncePerRequestFilter() {
43             @Override
44             protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
45                 System.out.println("filter do something");
46                 filterChain.doFilter(httpServletRequest, httpServletResponse);
47             }
48         };
49         FilterDef filterDef = new FilterDef();
50         filterDef.setFilter(filter);
51         filterDef.setFilterName("myFilter");
52         filterDef.addInitParameter("userName", "Allen");
53 
54         FilterMap filterMap = new FilterMap();
55         filterMap.addURLPatternDecoded("/*");
56         filterMap.addServletName("*");
57         filterMap.setFilterName("myFilter");
58         filterMap.setCharset(Charset.forName("UTF-8"));
59 
60         context.addFilterDef(filterDef);
61         context.addFilterMap(filterMap);
62         try {
63             tomcat.init();
64             tomcat.start();
65             tomcat.getServer().await();
66         } catch (LifecycleException e) {
67             e.printStackTrace();
68         }
69     }
70 }

注意其中OncePerRequestFilter是spring提供的对filter接口的实现类,没有引入spring依赖的可自行改用其他实现类。

运行main方法,一个最简单的tomcat实例已经运行起来。浏览器打开http://localhost:8080/embed-tomcat/dispatch,页面可以看到输出内容,控制台也打印了过滤器执行的内容。

springboot默认就是使用tomcat作为内嵌容器的,基本原理通上述代码,只不过spring做了更深层次的封装,结合其ioc容器,可以实现只要往ioc容器中注入servlet、filter接口的实现类,spring的适配器可自动完成配置。

posted on 2019-01-22 15:35  小小小小涛  阅读(4269)  评论(2编辑  收藏  举报

导航