
2.4 建立自己的Java项目
2.4.1 项目的类型选择及命名
所有配置工作完成之后,开始新建自己的项目,项目类型为Web Project,可以自己定义命名,如图2-79所示。

图2-79 新建项目
2.4.2 项目各个目录及文件的功能与意义
在项目的目录下,每个文件或目录都有自己的功能或意义,它们各司其职,负责整个项目的各个模块。接下来了解常用文件或目录的使命,如图2-80所示。

图2-80 目录和文件的意义
2.4.3 项目的编译与部署
项目的编译与部署实际操作起来其实特别简单,操作步骤如图2-81所示。

图2-81 编译与部署操作示意图
生成的Web App 在Tomcat安装目录下的webapps文件夹里可以看到,如图2-82所示。

图2-82 生成的Web App
2.4.4 本地开发环境及服务器搭建完毕后的验证测试
可以先验证本地的应用是否部署成功,首先检查Tomcat服务是否开启,在2.1.4小节图2-39的服务界面中查看。之后在浏览器中输入http://localhost/WeiXinEnterprises/,因为笔者默认使用80端口,所以端口号可以忽略不写,如果使用其他的端口,就需要输入http://localhost:8080/ WeiXinEnterprises/,把其中的8080换成你的端口就可以了。本地部署成功的界面如图2-83所示,部署成功jsp页面需要展示的文本内容如图2-84所示。

图2-83 本地部署成功

图2-84 测试内容
index.jsp为项目的默认页面,可以在web.xml里面配置默认的首页。
把项目部署到服务器验证,然后在外网输入服务器部署之后的项目地址,查看是否在服务器部署成功。下面介绍在服务器部署项目的方式。
第一种是上传war文件。编译war文件的方式也有几种,这里介绍常用的2种,第一种参考2.1.2小节,从图2-17开始看,第二种就是编译类型选择,详细步骤如图2-85所示。
单击“复制”按钮之后,进入如图2-86所示的界面,继续操作。

图2-85 步骤

图2-86 单击Add按钮
单击Add按钮之后,进入如图2-87所示的界面,选择要编译的类型。

图2-87 类型选择
得到编译的war文件之后,登录服务器IP,比如笔者的服务器IP是120.76.133.76(见图2-88),在浏览器中输入可以看到Tomcat的管理页面。
可以看到一个有小猫图形的页面,这就是之前服务器安装Tomcat服务的管理员页面,单击右边的Manager App按钮(见图2-89),输入管理员账号和密码(就是当初在服务器安装Tomcat时填写的账号和密码),登录成功后开始上传war文件,上传完毕后单击Deploy按钮,如图2-90所示。

图2-88 在浏览器中输入服务器IP

图2-89 单击Manager App按钮

图2-90 上传war文件
此时,可以看到正在上传的浏览器左下角有进度显示,等待完成即可,如图2-91所示。完成之后可在顶部的Manager下看到目标war文件已经部署在服务器的Web项目了。
上传成功之后,在Applications下有新的服务器Web应用,如图2-92所示。

图2-91 进度显示

图2-92 新的Web应用
单击该应用,可以进入项目的主页,如图2-93所示。

图2-93 单击Web应用进入主页
第二种是上传编译目录,即如图2-87所示的编译目录,这个目录会被编译到Tomcat安装目录下的webapps目录下,只需将此目录复制到服务器端的Tomcat安装目录下的webapps目录下就可以了,如图2-94所示。

图2-94 复制目标文件到服务器
粘贴完成后结果如图2-95所示,记得重启服务器的Tomcat。重启Tomcat的方式有很多种,这里介绍比较简单的方式。

图2-95 复制文件完成
单击计算机桌面左下角的“开始”按钮,找到并打开“运行”选项,如图2-96所示。
输入services.msc,单击“确定”按钮或按回车键,如图2-97所示。

图2-96 打开运行

图2-97 输入命令
弹出如图2-98所示的界面,选中Apache Tomcat服务项,单击“重启动此服务”选项就可以了。

图2-98 重启动此服务
重启之后,在浏览器输入框输入地址 http://120.76.133.76/WeiXinEnterprises,查看是否重启成功,重启之后部署成功的界面如图2-99所示。

图2-99 部署成功