解决tomcat遇到的所有乱码问题

**

本文记录我遇见过的所有乱码问题,应该都可以解决,至于原理这里不多讨论,可自行科普相关内容

**

1 tomcat启动控制台乱码

1.1 eclipse没遇到过
1.2 Idea请在tomcat配置中加上参数

-Dfile.encoding=UTF-8

在这里插入图片描述

2 控制台输出结果乱码

2.1如果是get请求,修改tomcat的配置文件server.xml中encoding全部修改为UTF-8
2.2使用String类构造方法
String queryString = new String(queryString.getBytes("iso8859-1"), "utf-8");

3 页面乱码

3.1可能是页面元素编码不对,尝试在html或jsp中添加如下
在这里插入图片描述
如果还乱码,检查是否是jsp页面头指令元素写错。
在这里插入图片描述
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
<%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>
第一行jsp字符编码,第二行jstl
3.2Servlet解决乱码三行代码

req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=utf-8");

这三行代码可应用于

  • Servlet开发或使用SpringMVC开发,方法参数加上HttpServletRequest和HttpServletResponse
  • Servlet开发中配置过滤器在这里插入图片描述
    3.3SpringMVC开发中通常简化配置在web.xml中解决post乱码,原理同上
<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>utf-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

在这里插入图片描述3.4SpringMVC开发使用消息转换器或拦截器解决乱码
-springmvc.xml
-1)默认消息转换器

<mvc:annotation-driven>
		<mvc:message-converters register-defaults="true">
			<bean
				class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>application/json;charset=UTF-8</value>
						<value>text/html;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>

2)自定义拦截器实现
implements HandlerInterceptor或extends HandlerInterceptorAdapter
在进入处理器之前写上上面三行解决乱码代码。
在这里插入图片描述3.5Springboot

package cn.itcast.springboot.demo;

import java.nio.charset.Charset;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@SpringBootApplication(exclude = { RedisAutoConfiguration.class })
@Configuration
public class HelloApplication {

	@RequestMapping("hello")
	@ResponseBody
	public String hello() {
		return "hello world!ya我是是是";
	}
	
	/*
	 * 消息转换器	默认是UTF-8
	 */
	@Bean
	public StringHttpMessageConverter stringHttpMessageConverter() {
		StringHttpMessageConverter converter = new StringHttpMessageConverter(Charset.forName("ISO-8859-1"));
		return converter;
	}

	public static void main(String[] args) {
//        SpringApplication.run(HelloApplication.class, args);
		// 关闭bannner
		SpringApplication app = new SpringApplication(HelloApplication.class);
		app.setBannerMode(Banner.Mode.OFF);
		app.run(args);
	}

}

package cn.itcast.springboot.demo;
import java.nio.charset.Charset;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration //申明这是一个配置
public class MySrpingMVCConfig extends WebMvcConfigurerAdapter{

    // 自定义拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        HandlerInterceptor handlerInterceptor = new HandlerInterceptor() {
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                    throws Exception {
                System.out.println("自定义拦截器............");
                return true;
            }
            
            @Override
            public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                    ModelAndView modelAndView) throws Exception {
                
            }
            
            @Override
            public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
                    Exception ex) throws Exception {
            }
        };
        registry.addInterceptor(handlerInterceptor).addPathPatterns("/**");
    }

    // 自定义消息转化器的第二种方法
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        StringHttpMessageConverter converter  = new StringHttpMessageConverter(Charset.forName("UTF-8"));
        converters.add(converter);
    }

}

4 数据库乱码

1,先断点调试是否是传参过程中就乱码,参考前几种方式解决
还有是否前台页面传参就已经乱码,decode需要两次
点击参考此博文
2,数据库的编码格式
图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值