springboot打war包部署tomcat服务器,以及表单提交数据乱码处理
小白觉得springboot打成jar包直接使用内嵌的tomcat或jetty容器(java -jar xxx.jar)运行项目不利于定位问题,我还是习惯于查看tomcat或nginx的日志来定位问题,今天小白就讲讲springboot打成war部署JavaWeb项目于tomcat。
新建web项目 helloboot
开发工具:Spring Tool Suite(STS)
File->New->Spring Starter Project,如下图所示:

项目结构如下所示:

application.properties配置:
server.port=
server.context-path=/helloboot spring.devtools.restart.additional-exclude=non-restart/**
spring.resources.static-locations=classpath:/resources/static/,classpath:/static/
spring.thymeleaf.cache=false logging.level.org.springframework.security=INFO
pox.xml(自动生成的,未作修改):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion> <groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>war</packaging> <name>helloboot</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5..RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
项目打包
1)在项目的启动类HellobootApplication.java的同级目录下新建ServletInitializer.java,代码如下,其位置可参考项目结构图:
package com.example.demo; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { @Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(HellobootApplication.class);
} }
2)用maven进行打包:Run AS -> Maven install,如下图所示打包成功,会在target目录下生成demo-0.0.1-SNAPSHOT.war

项目部署
进入tomcat的webapps子目录上传,上传完成后重命名为helloboot.war:
cd /root/tomcat/webapps
rz
mv demo-0.0.1-SNAPSHOT.war helloboot.war
修改tomcat的配置文件server.xml文件将8080端口改为8082,当然8080端口也是可以,由于本服务的8080端口已被占用。
进入tomcat的bin目录,启动tomcat:
cd /root/tomcat/bin
sh startup.sh
查看tomcat的日志:
cd /root/tomcat/logs
tail -f catalina.out
从日志可以看出启动成功:

项目访问
访问内网,可以通过xshell的通道访问:

在浏览器地址栏中输入:http://127.0.0.1:8082/helloboot/index,可见项目打包发布成功。

乱码处理
今天小白在测试demo是发现通过表单提交的数据乱码,原因可能有两种,一种前端提交到后台的数据后台解析乱码,另一种是解析正常但是在写入数据库的时候由于数据库编码导致的乱码。小白就这两种情况进行测试,第一种情况通过日志发现打印出来的前端传到后台的数据正常,于是小白查看了mysql数据库的编码,如下所示:
mysql> show variables like 'character_set_database';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| character_set_database | latin1 |
+------------------------+--------+
row in set (0.01 sec)
收集资料尝试发现只要在mysql的my.cnf的中添加一行代码character-set-server=utf8,重新启动mysql即可
[root@localhost etc]# vi /etc/my.cnf
[root@localhost etc]# service mysqld restart
my.cnf:
[root@localhost etc]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at % of total RAM for dedicated server, else %.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links= character-set-server=utf8 log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
修改后的mysql数据库的编码,表单提交数据也正常了。
mysql> show variables like 'character_set_database';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| character_set_database | utf8 |
+------------------------+-------+
row in set (0.06 sec)
springboot打war包部署tomcat服务器,以及表单提交数据乱码处理的更多相关文章
- Springboot解决war包放到Tomcat服务器上404的特殊情况
Springboot解决war包放到Tomcat服务器上404的特殊情况 原文链接:https://www.cnblogs.com/blog5277/p/9330577.html 原文作者:博客园-- ...
- 如何在阿里云上部署war包到tomcat服务器
一. 准备工作:xshell和xftp 首先我们得确保,xshell能够远程连接阿里云ECS,xftp能够保证windows和linux之间的文件传输(当然也可以选择FileZilla,但xftp感觉 ...
- springboot 学习之路 5(打成war包部署tomcat)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- 将war包部署到服务器的详细步骤
第一步: 先将项目打包成war文件,也就是将在项目上单击鼠标右键,选择Export: 选择WAR file,点击下一步: 会出现如下所示,选择你要保存的位置,点击完成: 在你所选择的地方会有个如下所示 ...
- springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据
springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal ...
- 如何在windows上部署war包到tomcat服务器
一. 什么是war包 WAR(Web Archive file)网络应用程序文件,是与平台无关的文件格式,它允许将很多文件组合成一个压缩文件.war专用在web方面,一个war包可以理解为一个web项 ...
- SpringBoot项目war包部署
服务部署 记录原因 将本地SpringBoot项目通过war包部署到虚拟机中,验证服务器部署. 使用war包是为了方便替换配置文件等. 工具 对象 版本 Spring Boot 2.4.0 VMwar ...
- springboot 的war包在Tomcat中启动失败
springboot 默认是通常是打包成jar的,里面会内置一个tomcat容器 有时候我们需要使用以前打成war包的方式部署到对应的tomcat中, 具体springboot 怎么从jar改成war ...
- Web项目打成war包部署Tomcat时运行startup.bat直接闪退部署失败解决方案
即上篇通过将web项目打成war包部署到Tomcat服务器,解决mysql问题后,又出现了新问题,真是一波三折,所以将解决过程分享给大家,希望能帮助到小伙伴们~ 将打好的war包拷贝到Tomcat的w ...
随机推荐
- 关于Vue父子组件传值(复杂数据类型的值)的细节点
vue 父子组件传值是很常见的,多数情况下都是父传递给子的值是基础数据类型,如string,number,boolean, 当父组件值被修改时,子组件能够实时的作出改变. 如果父子传值的类型是复杂数据 ...
- 优化 JS 条件语句及JS 数组常用方法, ---- 看完绝对对日后开发有用
前言: 日常所说的优化优化.最后我们到底优化了哪些,不如让我们从代码质量开始:个人觉得简洁简化代码其实觉得存在感挺强烈的QAQ 1. 获取URL中 ?后的携带参数: 这是我见过最简洁的了,若有更简洁的 ...
- AutoLayout and Sizeclasses讲解
iOS8和iPhone6发布已经过去蛮久了,广大的果粉终于迎来了大屏iPhone,再也不用纠结为大屏买三星舍苹果了…但是对于iOS开发人员来说,迎来了和Android开发开发一样的问题—>各种屏 ...
- 2019-11-29-dotnet-core-输出调试信息到-DebugView-软件
title author date CreateTime categories dotnet core 输出调试信息到 DebugView 软件 lindexi 2019-11-29 10:14:3 ...
- linux c 链接详解3-静态库
3静态库 摘自:Linux C编程一站式学习 透过本节可以学会编译静态链接库的shell脚本! 有时候需要把一组代码编译成一个库,这个库在很多项目中都要用到,例如libc就是这样一个库,我们在不同的程 ...
- mongoose 开源http库
Mongoose是一个用C编写的网络库.它为客户端和服务器模式实现TCP,UDP,HTTP,WebSocket,CoAP,MQTT的事件驱动的非阻塞API. 设计理念: Mongoose有三个基本的数 ...
- 我所了解的https
http大家多少都有些了解,毕竟要上网的话是肯定会接触到它的.http有个很明显的缺点,就是传输是明文的,很不安全.针对这个情况,就推出了https,也就是http+ssl/tls. 对于明文不安全的 ...
- BZOJ[3252]攻略(长链剖分)
BZOJ[3252]攻略 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏<XX半岛> ...
- 【学习】024 springCloud
单点系统架构 传统项目架构 传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一个项目中. 优点:适合于个人或者小团队开发,不适合大团队开发. 分布式项目架构 根据业务需求进行拆分成N个子 ...
- Connection refused 排查过程
Connection refused 排查过程 connection refused 排查 起因 今天在连接 rabbitmq 时,报 Connection refused (如下图),借此机会记 ...