javaweb(1)之tomcat使用

安装

一、点击下载html

二、解压到一个目录。java

三、进入解压后的 bin 目录,双击该文件夹下的 startup.bat 便可运行。web

四、若运行成功,会有一个窗口悬停以下:tomcat

访问地址: localhost:8080 ,若出现以下界面,则表示安装运行成功。服务器

注:若未出现上述效果,通常是 jdk 环境变量未配置好,能够参考:搭建Java环境app

使用

目录介绍

bin     # 包含了一些jar、bat文件
conf    # tomcat的配置相关文件。例如:server.xml、web.xml
lib     # tomcat运行所需的jar文件
logs    # 存放日志文件
temp    # 临时文件
webapps # 发布到tomcat服务器上的项目,就存放在这个目录
work    # jsp翻译成class文件存放地

发布

现有以下文件:webapp

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <h1>hello web!!!</h1>
</body>
</html>
hello.html

将其发布到 tomcat 有大体有以下两种方式:jsp

  • 拷贝到webapps

    拷贝该文件到 webapps/ROOT 下,访问 http://localhost:8080/hello.html :

    还能够在 webapps 下新建一个文件夹,而后将 hello.html 拷贝到该目录。好比我在此新建的是 pages 文件夹,接着就能够经过 localhost:8080/pages/hello.html 访问:
    ide

  • 配置虚拟路径

    假如 hello.html 文件被我存放在 D:\dev\workspace\webserver\pages 下,能够在 conf/server.xml 中找到 host 节点,加入内容以下:
    <!--
      docBase:项目的路径地址
      path:对应的虚拟路径 必定要以/打头
     -->
    <Context docBase="D:/dev/workspace/webserver/pages" path="/a"></Context>

    接下来就能够访问 localhost:8080/a/hello.html :
    spa

     还有一种方法,在 conf/catalina/localhost/ 下新建一个 xml 文件,名字能够本身定义,这里我使用 b.xml ,加入内容以下:

    <?xml version='1.0' encoding='utf-8'?>
    <!--
      docBase:项目的路径地址
     -->
    <Context docBase="D:/dev/workspace/webserver/pages"></Context>

    接着就能够访问 localhost:8080/b/hello.html :

IDEA中使用tomcat

一、选择菜单 Run ,点击 Edit Configurations。

二、点击 + 号,选择到 Tomcat Server,点击 Local。

三、设置 tomcat 名称,点击 Deployment 。

四、点击 + 号,选择 Artifact,将当前 web 项目添加到部署目录中。

五、编辑 Application context,它就是访问时起始路径,点击 OK 。

六、修改 web 目录下的 index.jsp :

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>Hello</title>
  </head>
  <body>
  Hello tomcat!!!
  </body>
</html>

七、在 Application Servers 窗口,选中要运行的 tomcat 实例,右键点击 Run/Connect 运行。

八、完成,访问 http://localhost:8080/hello/ 。

相关文章
相关标签/搜索