JavaWebTomcat
Posted leeqico
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaWebTomcat相关的知识,希望对你有一定的参考价值。
1、Tomcat 简介
Tomcat 是一个免费的开放源代码的 Servlet 容器,它是 Apache 软件基金会的一个顶级项目,由 Apache,Sun和其他一些公司及个人共同开发而成。由于有了 Sun 的参与与支持,最新的 Servlet 和 JSP 规范总是能在 Tomcat 中的到体现。(目前Sun已被Oracle收购)
2、Tomcat 的目录结构
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-ed771e259251a951.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
bin:存放启动和关闭 Tomcat 的脚本文件
conf:存放 Tomcat 服务器的各种配置文件
lib:存放 Tomcat 服务器和所有 web 应用程序需要访问的 jar 文件
logs:存放 Tomcat 的日志文件
temp:存放 Tomcat 运行时产生的临时文件
webapps:当发布 web 应用程序时,通常把 web 应用程序的目录及文件放到这个目录
work:Tomcat 将 JSP 生成的 Servlet 源文件和字节码文件放到这个目录下
3、部署并启动 tomcat 服务器.
1). 解压 apache-tomcat-8.0.20.zip 到一个非中文目录下
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-5b502903440d17d5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2). 配置一个环境变量. java_home(指向 JDK 安装的根目录) 或 jre_home
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-c223157fea39d553.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-7af9c370b3b7a7db.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3). 双击 bin 目录下的 startup.bat 文件或者使用命令行启动
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-b445ae86b31eecdc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4). 可以在浏览器中输入 localhost:8080 来检验 Tomcat 安装是否正确.
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-8227194afa8340da.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
5). 若已经启动了一个 Tomcat 应用, 若再启动同一个 Tomcat 应用, 会抛出异常:
java.net.BindException: Address already in use: JVM_Bind:8080。因为端口已经被占用了。
4、关闭 Tomcat
双击 bin 目录下的 shutdown.bat 文件关闭 Tomcat 服务器或者使用命令行关闭shutdown
5、修改 Tomcat 默认的端口号
(1)打开 conf 目录下的 server.xml 文件
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-2e344488c46a1243.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(2)把 port 的值改为其他的端口号即可,这里改为8989。
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-d1d8b5e19e150b7d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(3)重启tomcat,再重新在浏览器访问localhost:8989。
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-44e7181ede756a66.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
6、设置可以在任意目录下启动Tomcat服务器
(1)把D:\devProgram\apache-tomcat-8.0.20\bin添加到path下
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-6bd2d68a134ff8af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(2)添加成功后, 若在任意目录下通过 startup 启动 Tomcat 服务器, 会有提示。即: 要求设置 CATALINA_HOME 这个环境变量。
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-b5a41f97d51e76f5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(3)设置 CATALINA_HOME 环境变量
通过阅读 startup.bat 批处理文件, 可知 CATALINA_HOME 环境变量下有一个 bin 目录,而 bin 目录又有一个 catalina.bat 文件, 由此可知 CATALINA_HOME 指向的应该是 Tomcat安装的根目录,于是把 Tomcat 的根目录设为 CATALINA_HOME 环境变量。此时即可以在任意目录下通过startup.bat、shutdown.bat 启动、关闭 Tomcat 服务器。
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-f0951b9dd9cee62d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-38cb22e8c5a54982.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-02e8de48d21327cc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
这里的shutdown指令是计算机的,所以可以用catalina stop指令。
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-50e351417e60dca5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
7、继续阅读 startup.bat 文件,发现启动tomcat服务器的是catalina.bat 文件
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-a013b04bd4f17fea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
在命令行窗口直接输入 catalina, 提示如下:
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-74be4d288b8daf9a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
常用的参数是start,run 和 stop
run: 在 catalina 同一个命令行窗口下启动服务器;
start: 开启一个新窗口启动服务器;
stop: 关闭服务器。
8、Tomcat的管理程序
Tomcat 提供了一个管理程序:manager,用于部署到 Tomcat 服务器中的 web 应用程序。
要访问 manager web 应用程序,需要添加具有管理员权限的账号,编辑 conf 目录下的 tomcat-users.xml 文件,添加 manager 角色,设置用户名和密码。
(1)先访问tomcat,然后点击manager webapp,提示登录,这里点取消。
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-3cf0303b4c0244f3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(2)点击取消后,出现如下的页面。
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-3b036b6063ff9a3f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(3)把那段代码复制到 conf 目录下的 tomcat-users.xml 文件中,保存后重启tomcat。
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-9495a7f3df332a60.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(4)重新访问tomcat,登录。
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-81c6e8eb2a23af73.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
关注公众号:Java后端生活,干货文章第一时间送到!
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-68ffe9a239c6fade.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
以上是关于JavaWebTomcat的主要内容,如果未能解决你的问题,请参考以下文章