Post请求和get请求乱码方式解决

POST提交,提交页面显示中文乱码

//设置请求的编码格式
	request.setCharacterEncoding("utf-8");
	//设置响应的编码格式,与第一句的编码格式重复指定了
	//response.setCharacterEncoding("utf-8");

  响应编码格式与JSP页面设置的重复,可不再设置了

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

get提交,提交页面显示中文乱码:

治标的方法:新建一个字符串,获取一个iso-8859-1的字符数组,然后转为utf-8

String username = request.getParameter("username");
	String nn= new String(username.getBytes("iso-8859-1"),"utf-8");

 治本的方法:

在tomcat的配置文件:D:apache-tomcat-7.0.70confserver.xml

<!--在下面8443中指定编码格式
URIEcoding="UTF-8"
userBodyEncodingForURI="true" 用的是request.setCharacterEncoding("utf-8")
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"  在这里添加/>
原文地址:https://www.cnblogs.com/sincoolvip/p/5701214.html