• 嵌入式tomcat例子


    嵌入式tomcat学习笔记

     

    -1 依赖

    jdk用的1.8

    <build>

      <plugins>

        <plugin>

          <groupId>org.apache.maven.plugins</groupId>

          <artifactId>maven-compiler-plugin</artifactId>

          <version>3.1</version>

          <configuration>

            <source>1.8</source>

            <target>1.8</target>

          </configuration>

        </plugin>

      </plugins>

    </build>

    -2 相关类讲解

    备注:tomcat8及以上版本使用

    这里有个博客,解释挺详细的:https://www.cnblogs.com/mahuan2/p/6733566.html

     

    Tomcat tomcat = new Tomcat();//创建tomcat实例,用来启动tomcat

    tomcat.setHostname(“localhost”);//设置主机名

    tomcat.setPort(8080);//设置端口

    tomcat.setBaseDir(“.”);//tomcat存储自身信息的目录,比如日志等信息,根目录

    String DEFAULT_PROTOCOL = “org.apache.coyote.http11.Http11NioProtocol”;

    Connector connector = new Connector(DEFAULT_PROTOCOL);//设置协议,默认就是这个协议connector.setURIEncoding(“UTF-8”);//设置编码

    connector.setPort(Configs.getEmbedServerPort());//设置端口

    tomcat.getService().addConnector(connector);

    org.apache.catalina.Context ctx = tomcat.addContext(“myapp”,null);//网络访问路径

    tomcat.addServlet(ctx,”myServlet”,new MessageServlet()); //配置servlet

    ctx.addServletMappingDecoded(“/messageServlet”,”myServlet”);//配置servlet映射路径

    StandardServer server = (StandardServer)tomcat.getServer();//添加监听器,不知何用

    AprLifecycleListener listener = new AprLifecycleListener();

    server.addLifecycleListener(listener);

    //设置appBase为项目所在目录

    tomcat.getHost().setAppBase(

    System.getProperty(“user.dir”)

    +

    File.separator

    +

    ”.”

    );

    //设置WEB-INF文件夹所在目录

    //该文件夹下包含web.xml

    //当访问localhost:端口号时,会默认访问该目录下的index.html/jsp页面

    tomcat.addWebapp(“”,”webapp”);

    tomcat.start();//启动tomcat

    tomcat.getServer().await();//维持tomcat服务,否则tomcat一启动就会关闭

    -3 例子

    =1 项目结构

     

    =2 主类

     

    =3 servlet

     

    访问Servlet路径:http://localhost:8080/myapp/myServlet

    访问html路径:http://localhost:8080(会访问到resources目录下的index.html/jsp)

    访问html路径:http://localhost:8080/test1(会访问到resources/test1下的index.html/jsp,当然,也可以在test1后面加上页面名字,比如test.html)

  • 相关阅读:
    淘宝首页广告圆角切换标签未解之谜(vml)
    chrome的google官方在线安装太坑爹了,找到一个离线下载地址
    kejun写的响应性设计和开发
    HTTP状态码
    xwebkitspeech 语音输入功能
    Avoid Redirects 避免重定向
    webstorm下使用github
    开通了github,用webstorm上传,敲命令行太累。
    jQuery1.6.1下event鼠标事件有BUG,升级到1.7.1可以解决问题。
    从程序员到项目经理(五):不是人人都懂的学习要点
  • 原文地址:https://www.cnblogs.com/lmq-1048498039/p/8329481.html
Copyright © 2020-2023  润新知