zoukankan      html  css  js  c++  java
  • 嵌入式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)

  • 相关阅读:
    About try...expect...else
    About from __future__ import absolute_import
    How to 安装apache和配置自定义目录, run CGI or py
    Caffe
    About os.environ
    TensorFlow
    Keras
    SQL自定义函数,把sql中小数点后的多于0去掉
    .net杂记 (调用EXE 获取路径 target VB DIV C# Split 页面之间传递值 更改session时间 EXCEL操作 控制电子邮件Mail input记录值的窍门)
    orcle分析函数
  • 原文地址:https://www.cnblogs.com/lmq-1048498039/p/8329481.html
Copyright © 2011-2022 走看看