Tomcat嵌入式启动
来源:互联网 发布:最新一手淘宝买家数据 编辑:程序博客网 时间:2024/06/09 15:55
1、导入jar包
<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=localhostwebserver.port=8081webserver.webappDir=src/main/webappwebserver.maxPostSize=0webserver.maxThreads=10webserver.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.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、访问页面
0 0
- 启动嵌入式Tomcat
- Tomcat嵌入式启动
- 使用embeded tomcat进行嵌入式tomcat-启动tomcat
- 使用嵌入式tomcat启动web项目
- 程序实现启动和停止嵌入式Tomcat Web服务器
- gradle中使用嵌入式(embedded) tomcat, debug 启动
- tomcat启动
- Tomcat启动
- tomcat启动
- Tomcat启动
- tomcat启动
- tomcat启动
- tomcat 启动
- tomcat 启动
- Tomcat启动
- tomcat启动
- Tomcat 启动
- Tomcat启动
- 详解Java8特性之Stream API补充
- Machine Learning第七讲[支持向量机] --(二)核函数
- Permission denied: user=dr.who, access=READ_EXECUTE, inode="/tmp":root:supergroup:drwx------
- 从零开始学Scala系列(三)之初识循环分支结构
- jupyter notebook python2 python3 kernel共存
- Tomcat嵌入式启动
- MySQL——C语言遇上SQL语句
- 串的模式匹配
- C语言总结
- 5月6日,CountObject,每日20行。
- Hibernate映射集合属性4__排序用的order-by属性
- Codeforces Round #411 D. Minimum number of steps (思维题)
- 【Spring Boot教程】推荐使用Spring Tool Suite(STS)开发
- 强连通分量(方法)