一、安装Tomcat
1、Linux环境安装
1)下载合适版本的Tomcat;
2)将文件apache-tomcat-xxx.tar.gz 传到/usr/tomcat/下(可选),并解压,
解压命令:
tar -xzvf apache-tomcat-xxx.tar.gz
3)查看端口占用以及端口详情
tomcat默认端口为8080,如被占用,则需修改tomcat的端口配置
查看所有端口:
netstat -nltp
查看8080端口是否被占用:
# 方法一
netstat -nltp | grep 8080
# 方法二
lsof -i:8080
# 注:如果提示没有lsof命令,则使用 yum install lsof 进行安装

4)如果8080端口被占用,可以杀调占用进程;或者修改tomcat的端口配置
在tomcat里面,找到conf/server.xml文件, 进入文件找到如下端口配置,修改端口号
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
5)启动Tomcat
进入tomcat里面的bin目录
执行: ./startup.sh -->启动tomcat
执行: ./shutdown.sh -->关闭tomcat
如果启动成功,浏览器中输入:http://ip:8080,即可访问tomcat
二、在Tomcat部署SpringMVC项目
0、在tomcat部署springmvc项目,在windos环境或者linux环境都差不多。
1、把项目war包,放到本地tomcat下的webapps文件夹下

2、启动项目
1)先关闭(可能开启的)项目,运行tomcat下的bin文件夹下的shutdown文件

2)再运行启动文件,在tomcat下的bin文件夹下的startup文件,启动成功后,webapps文件夹下的war包会被解压;

3)浏览器访问
ip写localhost,端口写8080,地址写文件夹的名字

三、Tomcat遇到的问题汇总
1、IDEA启动tomcat项目,说xxx端口已被占用
1)问题描述:

或者

2)原因分析:
上次启动了tomcat项目,在还没有关闭的情况下,直接退出idea;java进程没有关闭,刚才的端口还在被占用。

3)解决:杀掉上一个使用tomcat的进程
方式一:
按快捷键 Ctrl+Shift+Esc 打开任务管理器,在后台进程中找到java.exe,然后点击“结束任务”即可

方式二:
在cmd窗口,用命令关掉进程
先根据端口查询进程id
netstat -aon|findstr 9096

然后根据进程id杀掉进程
taskkill /f /t /im 35292

2、SpringMVC项目,由于接口没加@ResponseBody,导值返回404
1)问题描述
写了一个普通接口,期望返回一个对象(对象的json格式),但是实际却返回的404。
错误示例:
@PostMapping("/json/import")
public Result importByJson(@RequestBody WecomTagQuery wecomTagQuery){
Result result = Result.failure();
//推送
try{
String str = wecomTagService.saveOrUpdateByJson(wecomTagQuery.getTag_list());
result = Result.success();
result.setInfo(str);
}catch (Exception e){
e.printStackTrace();
result.setInfo("执行失败!");
}
return result;
}
错误结果:
<!doctype html>
<html lang="en">
<head><title>HTTP Status 404 – Not Found</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head>
<body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Message</b> /admin/page/wecom/tag/json/import.jsp</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/8.5.39</h3></body>
</html>
2)原因分析
不加**@ResponseBody注解springmvc框架会认为方法返回的是一个 ModelAndViewer对象,即视图对象,那么它就会去找这么一个对象,找不到则报404错。
3)解决
SpringMVC项目,需要直接返回结果的Controller接口,要加@ResponseBody**注解
正确示例:
@PostMapping("/json/import")
@ResponseBody
public Result importByJson(@RequestBody WecomTagQuery wecomTagQuery){
Result result = Result.failure();
//推送
try{
String str = wecomTagService.saveOrUpdateByJson(wecomTagQuery.getTag_list());
result = Result.success();
result.setInfo(str);
}catch (Exception e){
e.printStackTrace();
result.setInfo("执行失败!");
}
return result;
}


4311

被折叠的 条评论
为什么被折叠?



