embedded tomcat context.xml
在网络下载相关的embedded tomcat jar。
也可直接在maven中检索。
在main方法中,输入以下代码:
//新建tomcat实例 Tomcat tomcat = new Tomcat(); //设置临时目录地址 tomcat.setBaseDir(""); //tomcat监听端口 tomcat.setPort(8080); String baseDir = "D:\\workspace\\SpringMVC\\web"; //webapp使用的http路径,以及webapp在本地的代码目录(代码目录要符合servlet标准) Context ct = tomcat.addWebapp("/spring", baseDir); //使用JNDI服务 tomcat.enableNaming(); //加载context.xml文件,里面可以定义一些资源,包括数据库等 ct.setConfigFile(new File(baseDir + File.separator + "META-INF\\context.xml").toURI().toURL()); //收到请求时的解析编码 tomcat.getConnector().setURIEncoding("utf-8"); Long startTime = System.currentTimeMillis(); //启动tomcat tomcat.start(); System.out.println("Tomcat started in " + ((System.currentTimeMillis() - startTime) / 1000.0) + "s"); //挂起主线程,tomcat就会一直在后台运行,直到收到shutdown信号 tomcat.getServer().await();
maven:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <tomcat.version>7.0.32</tomcat.version> </properties> <dependencies> <!-- tomcat.version must be greater than 8.*.* <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jni</artifactId> <version>${tomcat.version}</version> </dependency> --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-util</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-servlet-api</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-juli</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jsp-api</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jasper-el</artifactId> <version>${tomcat.version}</version> </dependency> <!-- --> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jasper</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-i18n-ja</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-i18n-fr</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-i18n-es</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-el-api</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-dbcp</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-coyote</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-api</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-annotations-api</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-catalina-ws</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-catalina-ha</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-catalina-ant</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-logging-log4j</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-logging-juli</artifactId> <version>${tomcat.version}</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>${tomcat.version}</version> </dependency> </dependencies>