tomcat8 嵌入式 servlet 实例
所属分类 tomcat
浏览量 893
tomcat8 嵌入式启动
http://127.0.0.1:8086/hello
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
public class Main {
public static void main(String[] args) throws Exception {
Tomcat tomcat = new Tomcat();
tomcat.setHostname("dyyx");
tomcat.setPort(8086);
// tomcat.setBaseDir("/tmp/");
String contextPath = "";
StandardContext context = new StandardContext();
context.setPath(contextPath);
context.addLifecycleListener(new Tomcat.FixContextListener());
tomcat.getHost().addChild(context);
tomcat.addServlet(contextPath, "hello", new HelloServlet());
context.addServletMapping("/hello", "hello");
tomcat.start();
tomcat.getServer().await();
}
}
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("hello tomcat," + LocalDateTime.now());
}
}
org.apache.tomcat.embed
tomcat-embed-core
8.5.0
org.apache.tomcat.embed
tomcat-embed-logging-juli
8.5.0
javax.servlet
servlet-api
2.5
provided
完整代码
https://gitee.com/dyyx/demos/tree/master/tomcatdemo
上一篇
下一篇
mysql的读已提交和可重复读
springboot基础配置
mysql redo undo binlog
tomcat8 连接器
Tomcat NIO 处理机制
tomcat8的后台线程