Tomcat嵌入式启动
2017-05-06 18:48
141 查看
1、导入jar包
2、利用Maven建立一个web项目,项目结构如图:
3、项目属性文件webserver.properties
4、启动主方法
5、主要代码,一些配置信息
6、访问页面
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>8.5.13</version> </dependency> <!--主要获取配置属性信息--> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.10</version> </dependency> <!--避免启动出错--> <dependency> <groupId>tomcat</groupId> <artifactId>jasper-runtime</artifactId> <version>5.5.23</version> </dependency>
2、利用Maven建立一个web项目,项目结构如图:
3、项目属性文件webserver.properties
webserver.hostname=localhost webserver.port=8081 webserver.webappDir=src/main/webapp webserver.maxPostSize=0 webserver.maxThreads=10 webserver.acceptCount=10
4、启动主方法
public class TestBoot { public static void main(String[] args) { WebServce.getWebserce().start(); } }
5、主要代码,一些配置信息
public class WebServce { //设置主机或ip private String hostname="localhost"; //设置端口,默认的端口,主要看配置属性 private int port=8899; // private String webappDir="webapp"; //设置 连接时的一些参数 private int maxPostSize=0; private int maxThreads=200; private int acceptCount=100; //tomcat引用 private Tomcat tomcat; public WebServce(){} //获取属性信息 protected void loadProperties()throws IOException{ PropertiesConfiguration conf=new PropertiesConfiguration(); try{ //提供文件名 conf.load("webserver.properties"); }catch (ConfigurationException ce){ } //根据配置文件修改初始值 //第二个参数是默认值,当第一个为空时,使用默认值 this.hostname=conf.getString("webserver.hostname","localhost"); this.port=conf.getInt("webserver.port",8899); this.webappDir=conf.getString("webserver.webappDir","webapp"); this.maxPostSize=conf.getInt("webserver.maxPostSize",0); this.maxThreads=conf.getInt("webserver.maxThreads",200); this.acceptCount=conf.getInt("webserver.acceptCount",100); } //启动 public void start(){ try{ //加载配置 this.loadProperties(); //tomcat实例 this.tomcat=new Tomcat(); this.tomcat.setPort(this.port); this.tomcat.setHostname(this.hostname); //tomcat存储自身信息,保存在项目目录下 this.tomcat.setBaseDir("."); this.configServer(this.tomcat.getServer()); this.tomcat.getEngine(); this.configHost(this.tomcat.getHost()); this.configConnector(this.tomcat.getConnector()); //第一个参数上下文路径contextPath,第二个参数docBase this.tomcat.addWebapp("", System.getProperty("user.dir")+ File.separator+this.webappDir); //这种方式也行 // this.tomcat.getHost().setAppBase(System.getProperty("user.dir")+ File.separator+"."); // this.tomcat.addWebapp("",this.webappDir); this.tomcat.start(); this.tomcat.getServer().await(); }catch(Exception e){ } } private void configHost(Host host) { //user.dir 用户的当前工作目录 host.setAppBase(System.getProperty("user.dir")); } private void configServer(Server server) { AprLifecycleListener listener = new AprLifecycleListener(); server.addLifecycleListener(listener); } //设置连接属性 private void configConnector(Connector connector) { connector.setURIEncoding("UTF-8"); connector.setMaxPostSize(this.maxPostSize); connector.setAttribute("maxThreads", Integer.valueOf(this.maxThreads)); connector. bf17 setAttribute("acceptCount", Integer.valueOf(this.acceptCount)); connector.setAttribute("disableUploadTimeout", Boolean.valueOf(true)); connector.setAttribute("enableLookups", Boolean.valueOf(false)); } public static WebServce getWebserce(){ return new WebServce(); } }
6、访问页面
相关文章推荐
- 启动嵌入式Tomcat
- gradle中使用嵌入式(embedded) tomcat, debug 启动
- 程序实现启动和停止嵌入式Tomcat Web服务器
- 使用embeded tomcat进行嵌入式tomcat-启动tomcat
- gradle中使用嵌入式(embedded) tomcat, debug 启动
- springboot启动嵌入式tomcat报错找不到jar包,关键字:FileNotFoundException,derbyLocale_cs.jar,StandardJarScanner.scan
- 使用嵌入式tomcat启动web项目
- 使用embeded tomcat进行嵌入式javaee开发-启动tomcat
- Tomcat 启动参数说明。
- 启动TOMCAT时出现"-Djaava.endorsed.dirs="的解决办法
- TOMCAT源码分析(启动框架)
- Tomcat启动脚本catalina.sh
- Tomcat启动分析
- 防火墙设置错误造成Tomcat启动缓慢
- Tomcat启动分析
- 关于嵌入式Linux系统的启动(很好的嵌入式linux启动过程分析)
- Tomcat启动分析
- Tomcat启动分析
- tomcat启动时,自动启动一个线程
- win2000下tomcat作为服务启动的注册表文件