首页 > 代码库 > Tomcat 插件启动 Web程序

Tomcat 插件启动 Web程序

Tomcat Embedded  开发人员无需搭建Tomcat的环境就可以使用内嵌式Tomcat进行开发,减少搭建J2EE容器环境的时间和开发时容器频繁启动所花时间,提高开发的效率

使用Maven创建项目需要添加一下几个包:

        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-catalina</artifactId>
            <version>8.5.19</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-core</artifactId>
            <version>8.5.19</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <version>8.5.19</version>
        </dependency>
        <dependency>  
            <groupId>org.apache.tomcat.embed</groupId>  
            <artifactId>tomcat-embed-el</artifactId>  
            <version>8.5.19</version>
        </dependency>  
        <dependency>
            <groupId>org.apache.tomcat</groupId>
            <artifactId>tomcat-jasper</artifactId>
            <version>8.5.19</version>
        </dependency>

先定义一个简单的HttpServlet,代码如下:

public class HomeServlet extends HttpServlet {  
    private static final long serialVersionUID = 1L;  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {  
        System.out.println("request scheme: " + req.getParameter("name"));  
        resp.getWriter().print("hello tomcat 陆建陆建陆建陆建陆建陆建陆建陆建陆建陆建陆建陆建陆建陆建陆建陆建");  
    }  
}

返回一串字符。

编写插件启动:

public class EmbeddedServer {
    static final int port = 9080;  
    static final String docBase = "target/tomcat";
    
    public static void main(String[] args) {
        Tomcat tomcat = new Tomcat();  
        tomcat.setPort(port);  
        tomcat.setBaseDir(docBase);  
        tomcat.getHost().setAutoDeploy(false);  
        tomcat.getEngine();
        String contextPath = "/book";  
        
        Connector connector = tomcat.getConnector();
        connector.setURIEncoding("UTF-8");
        connector.setUseBodyEncodingForURI(true);
        
        
        tomcat.setConnector(connector);
        
        
        
        StandardContext context = new StandardContext();
        context.setPath(contextPath); 
        context.addLifecycleListener(new FixContextListener());  
   
        
        tomcat.getHost().addChild(context);
        
        
        
        
        tomcat.addServlet(contextPath, "homeServlet", new HomeServlet());  
        context.addServletMappingDecoded("/home", "homeServlet");  
        try {
            tomcat.start();
        } catch (LifecycleException e) {
            // TODO Auto-generated catch block
            System.out.println(e.getMessage());
        }   
        tomcat.getServer().await();  
    }
}

如需添加过滤器:

Filter代码:

public class CORSFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // TODO Auto-generated method stub
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse res,
            FilterChain chain) throws IOException, ServletException {
        // TODO Auto-generated method stub
        HttpServletResponse response = (HttpServletResponse) res;
        response.addHeader("Access-Control-Allow-Origin", "*");
        response.addHeader("Access-Control-Allow-Credentials", "true");
        response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        response.addHeader("Access-Control-Max-Age", "3600");
        response.addHeader("Access-Control-Allow-Headers",
            "X-Requested-With,X_Requested_With,DEVICE_ID,accept,content-type");
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        
    }
}

在启动插件中添加代码:

        FilterDef def = new FilterDef();
        def.setFilter(new CORSFilter());
        def.setFilterName("corsFileter");
        FilterMap defMap = new FilterMap();
        defMap.setFilterName("corsFileter");
        defMap.addURLPattern("*");
        context.addFilterDef(def);
        context.addFilterMap(defMap);

 

Tomcat 插件启动 Web程序