Tomcat实战案例(Java代码部署)

亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述Tomcat实战案例:Java代码部署相关的知识,希望能为你提供帮助。
Tomcat 下java代码部署可以通过手动部署和自动部署的方式完成,下面用简单的实例介绍部署过程。
?

【Tomcat实战案例(Java代码部署)】

Tomcat 下Java代码部署---手动部署简要说明:所谓手动部署就是类似Apache的httpd 下准备网站的网友文件的过程。

#### 手动部署,准备模拟项目app1 的目录
[root@CentOS84-IP68 ]#mkdir /usr/local/tomcat/webapps/app1/

[root@CentOS84-IP68 ]#vim /usr/local/tomcat/webapps/app1/test.jsp
< %@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
< !DOCTYPE html>
< html>
< head>
< meta charset="utf-8">
< title> jsp例子< /title>
< /head>
< body>
后面的内容是服务器端动态生成字符串,最后拼接在一起
< %
out.println("hello jsp");
%>
%< br>
%< %=request.getRequestURL()%>
%< /body>
%< /html>
[root@CentOS84-IP68 ]#

[root@CentOS84-IP68 ]#chown -R tomcat.tomcat /usr/local/tomcat/webapps/app1/
[root@CentOS84-IP68 ]#tree /usr/local/tomcat/webapps/app1
/usr/local/tomcat/webapps/app1
└── test.jsp

0 directories, 1 file
[root@CentOS84-IP68 ]#curl http://192.168.250.68:8080/app1/test.jsp

< !DOCTYPE html>
< html>
< head>
< meta charset="utf-8">
< title> jsp例子< /title>
< /head>
< body>
后面的内容是服务器端动态生成字符串,最后拼接在一起
hello jsp

%< br>
%http://192.168.250.68:8080/app1/test.jsp
%< /body>
%< /html>

# 上面访问后,work 目录下会自动生成相应的文件
[root@CentOS84-IP68 ]#tree /usr/local/tomcat/work/Catalina/localhost/
/usr/local/tomcat/work/Catalina/localhost/
├── app1
│└── org
│└── apache
│└── jsp
│├── test_jsp.class
│└── test_jsp.java
├── docs
├── examples
├── host-manager
├── manager
├── ROOT
└── shone

13 directories, 6 files
[root@CentOS8

# 删除前面手工部署的JAVA应用
[root@CentOS84-IP68 ]#rm -rf /usr/local/tomcat/webapps/app1/
[root@CentOS84-IP68 ]#ls /usr/local/tomcat/webapps/
docsexampleshost-managermanagerROOTshone
[root@CentOS84-IP68 ]#ls /usr/local/tomcat/work/Catalina/localhost/
docsexampleshost-managermanagerROOTshone
[root@CentOS84-IP68 ]#

Tomcat 下Java代码部署---自动简要说明:所谓自动部署就是打包成war,复制到Tomcat 的网站指定目录下,自动生成网站运行需要的文件等。
#### 自动部署
# 模拟创建一个项目目录
[root@CentOS84-IP68 ]#mkdir -p /data/app2/
# 准备两个测试页面
[root@CentOS84-IP68 ]#vim /data/app2/test.html
< h1> This is shone-test html < /h1>
[root@CentOS84-IP68 ]#vim /data/app2/shone.jsp

< %@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
< !DOCTYPE html>
< html>
< head>
< meta charset="utf-8">
< title> jsp例子< /title>
< /head>
< body>
后面的内容是服务器端动态生成字符串,最后拼接在一起
< %
out.println("hello jsp");
%>
%< br>
%< %=request.getRequestURL()%>
%< /body>
%< /html>

[root@CentOS84-IP68 ]#cd /data/app2
[root@CentOS84-IP68 ]#ll
total 8
-rw-r--r-- 1 root root 355 Apr9 11:19 shone.jsp
-rw-r--r-- 1 root root34 Apr9 11:16 test.html

# #生成war包文件app2.war,此文件的名称决定了tomcat子目录的名称
[root@CentOS84-IP68 ]#jar cvf /data/app2.war *
added manifest
adding: shone.jsp(in = 355) (out= 300)(deflated 15%)
adding: test.html(in = 34) (out= 33)(deflated 2%)

[root@CentOS84-IP68 ]#ll
total 8
-rw-r--r-- 1 root root 355 Apr9 11:19 shone.jsp
-rw-r--r-- 1 root root34 Apr9 11:16 test.html

[root@CentOS84-IP68 ]#ll /data/
total 4
drwxr-xr-x 2 root root40 Apr9 11:19 app2
-rw-r--r-- 1 root root 892 Apr9 11:19 app2.war
[root@CentOS84-IP68 ]#file app2.war
app2.war: Java archive data (JAR)
[root@CentOS84-IP68 ]#chown tomcat.tomcat /data/app2.war
[root@CentOS84-IP68 ]#

## 进入Tomcat目录自动部署 war包
[root@CentOS84-IP68 ]#cd /usr/local/tomcat
[root@CentOS84-IP68 ]#pwd
/usr/local/tomcat
[root@CentOS84-IP68 ]#ls webapps/
docsexampleshost-managermanagerROOTshone
[root@CentOS84-IP68 ]#ls work/Catalina/localhost/
docsexampleshost-managermanagerROOTshone
[root@CentOS84-IP68 ]#ll webapps/
total 4
drwxr-x--- 15 tomcat tomcat 4096 Apr9 05:44 docs
drwxr-x---7 tomcat tomcat99 Apr9 05:44 examples
drwxr-x---6 tomcat tomcat79 Apr9 05:44 host-manager
drwxr-x---6 tomcat tomcat114 Apr9 05:44 manager
drwxr-x---3 tomcat tomcat257 Apr9 10:52 ROOT
drwxr-x---3 tomcat tomcat39 Apr9 10:45 shone

# 复制 app2.war 到Tomcat的webapps目录
[root@CentOS84-IP68 ]#cp -p /data/app2.war webapps/
# 再次查看,tomcat将app2.war自动解压缩
[root@CentOS84-IP68 ]#ll webapps/
total 8
drwxr-x---3 tomcat tomcat56 Apr9 11:24 app2
-rw-r--r--1 tomcat tomcat892 Apr9 11:19 app2.war
drwxr-x--- 15 tomcat tomcat 4096 Apr9 05:44 docs
drwxr-x---7 tomcat tomcat99 Apr9 05:44 examples
drwxr-x---6 tomcat tomcat79 Apr9 05:44 host-manager
drwxr-x---6 tomcat tomcat114 Apr9 05:44 manager
drwxr-x---3 tomcat tomcat257 Apr9 10:52 ROOT
drwxr-x---3 tomcat tomcat39 Apr9 10:45 shone

[root@CentOS84-IP68 ]#ll webapps/app2/
total 8
drwxr-x--- 2 tomcat tomcat44 Apr9 11:24 META-INF
-rw-r----- 1 tomcat tomcat 355 Apr9 11:19 shone.jsp
-rw-r----- 1 tomcat tomcat34 Apr9 11:16 test.html

# work目录会自动生成对应的app2的子目录,但目录内无内容
[root@CentOS84-IP68 ]#tree work/Catalina/localhost/app2/
work/Catalina/localhost/app2/

0 directories, 0 files

# 访问jsp文件后,tomcat会自动将jsp转换和编译生成work目录下对应的java和class文件
[root@CentOS84-IP68 ]#curl http://127.0.0.1:8080/app2/shone.jsp


< !DOCTYPE html>
< html>
< head>
< meta charset="utf-8">
< title> jsp例子< /title>
< /head>
< body>
后面的内容是服务器端动态生成字符串,最后拼接在一起
hello jsp

%< br>
%http://127.0.0.1:8080/app2/shone.jsp
%< /body>
%< /html>
[root@CentOS84-IP68 ]#curl http://127.0.0.1:8080/app2/test.html
< h1> This is shone-test html < /h1>
[root@CentOS84-IP68 ]#tree work/Catalina/localhost/app2/
work/Catalina/localhost/app2/
└── org
└── apache
└── jsp
├── shone_jsp.class
└── shone_jsp.java

3 directories, 2 files

# 自动删除(反部署)
[root@CentOS84-IP68 ]#rm -f webapps/app2.war

# 过几秒再查看,发现app2目录也随之删除
[root@CentOS84-IP68 ]#ls webapps/
app2docsexampleshost-managermanagerROOTshone
[root@CentOS84-IP68 ]#ls work/Catalina/localhost/
docsexampleshost-managermanagerROOTshone
[root@CentOS84-IP68 ]#ls webapps/
docsexampleshost-managermanagerROOTshone
[root@CentOS84-IP68 ]#


    推荐阅读