簡(jiǎn)單記錄一下內(nèi)部tomcat啟動(dòng)
maven pom.xml
<dependencies>
<!-- embed tomcat dependency -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>8.5.28</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>8.5.28</version>
</dependency>
<!-- spring dependency -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.4.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.4.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 添加編譯插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
tomcat啟動(dòng)類(lèi)
/**
*
* @author Programming is an art from.
* @Description: TODO
*/
public class TomcatStart {
public static int TOMCAT_PORT = 8080;
public static String TOMCAT_HOSTNAME = "127.0.0.1";
public static String WEBAPP_PATH = "src/main";
public static String WEBINF_CLASSES = "/WEB-INF/classes";
public static String CLASS_PATH = "target/classes";
public static String INTERNAL_PATH = "/";
public static void main(String[] args) throws ServletException, LifecycleException {
TomcatStart.run();
}
public static void run() throws ServletException, LifecycleException {
Tomcat tomcat = new Tomcat();
tomcat.setPort(TomcatStart.TOMCAT_PORT);
tomcat.setHostname(TomcatStart.TOMCAT_HOSTNAME);
tomcat.setBaseDir("."); // tomcat 信息保存在項(xiàng)目下
/*
* https://www.cnblogs.com/ChenD/p/10061008.html
*/
StandardContext myCtx = (StandardContext) tomcat
.addWebapp("/access", System.getProperty("user.dir") + File.separator + TomcatStart.WEBAPP_PATH);
/*
* true時(shí):相關(guān)classes | jar 修改時(shí),會(huì)重新加載資源佛寿,不過(guò)資源消耗很大
* autoDeploy 與這個(gè)很相似砌创,tomcat自帶的熱部署不是特別可靠摆尝,效率也不高横堡。生產(chǎn)環(huán)境不建議開(kāi)啟斧账。
* 相關(guān)文檔:
* http://www.blogjava.net/wangxinsh55/archive/2011/05/31/351449.html
*/
myCtx.setReloadable(false);
// 上下文監(jiān)聽(tīng)器
myCtx.addLifecycleListener(new AprLifecycleListener());
/*String webAppMount = System.getProperty("user.dir") + File.separator + TomcatStart.CLASS_PATH;
WebResourceRoot root = new StandardRoot(myCtx);
root.addPreResources(
new DirResourceSet(root, TomcatStart.WEBINF_CLASSES, webAppMount, TomcatStart.INTERNAL_PATH));*/
// 注冊(cè)servlet
tomcat.addServlet("/access", "demoServlet", new DemoServlet());
// servlet mapping
myCtx.addServletMappingDecoded("/demo.do", "demoServlet");
tomcat.start();
tomcat.getServer().await();
}
}
注意谴返! contextPath不要設(shè)置為 /
否則會(huì)報(bào)錯(cuò), 錯(cuò)誤信息為以下。
警告: A context path must either be an empty string or start with a '/' and do not end with a '/'. The path [/] does not meet these criteria and has been changed to []
Exception in thread "main" java.lang.NullPointerException
at org.apache.catalina.startup.Tomcat.addServlet(Tomcat.java:341)
at org.apache.catalina.startup.Tomcat.addServlet(Tomcat.java:325)
at cn.learn.config.TomcatStart.run(TomcatStart.java:63)
at cn.learn.config.TomcatStart.main(TomcatStart.java:33)
servlet class
/**
*
* @author Programming is an art from.
* @Description: TODO
*/
public class DemoServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().print("access success!!!");
}
}
啟動(dòng)main方法咧织,訪(fǎng)問(wèn)成功嘍嗓袱!
http://127.0.0.1:8080/access/demo.do