zoukankan      html  css  js  c++  java
  • 内置tomcat使用servlet

    tomcat启动类

    public class AppTomcat {
        public static void main(String[] args) throws LifecycleException {
            // 创建Tomcat应用对象
            Tomcat tomcat = new Tomcat();
            // 设置Tomcat的端口号
            tomcat.setPort(8080);
            // 是否设置Tomcat自动部署
            tomcat.getHost().setAutoDeploy(false);
            // 创建上下文
            StandardContext standardContext = new StandardContext();
            // 设置项目名
            standardContext.setPath("/sb");
            // 监听上下文
            standardContext.addLifecycleListener(new FixContextListener());
            // 向tomcat容器对象添加上下文配置
            tomcat.getHost().addChild(standardContext);
            // 创建Servlet
            tomcat.addServlet("/sb", "helloword", new HelloServlet());
            // Servlet映射
            standardContext.addServletMappingDecoded("/hello", "helloword");
            //启动tomcat容器
            tomcat.start();
            //等待
            tomcat.getServer().await();
        }
    }

    servlet类

    public class HelloServlet extends HttpServlet{
        private static final long serialVersionUID = 1L;
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("hellowrld");
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
  • 相关阅读:
    使用Apache Curator监控Zookeeper的Node和Path的状态
    mongo创建用户
    window下关闭nginx
    spring 下载地址
    Quartz Spring与Spring Task总结
    oracle 11g 空表也导出
    修改oracle字符集
    linux 查看最大文件
    JAVA https证书相关
    抽象类与接口
  • 原文地址:https://www.cnblogs.com/FlyBlueSky/p/9613611.html
Copyright © 2011-2022 走看看