Tomcat使用过程中遇到的几种乱码问题
程序员文章站
2022-05-01 14:47:35
...
由于tomcat内部的默认编码为ISO-8859-1,故不支持中文,在使用中文的地方要注意编码问题
1.首先一般乱码问题的解决
//servlet中设置
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//jsp中设置
<%@page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
每次都要写一遍麻烦,就写在过滤器里,只写一次即可.
package filter;
import javax.servlet.ServletException;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
public class CharsetFilter implements Filter{
@Override
public void init(FilterConfig config) {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException,ServletException{
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
配置web.xml
<?xml version="1.0" encoding="utf-8" ?>
<web-app>
<filter>
<filter-name>CharsetFilter</filter-name>
<filter-class>filter.CharsetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharsetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.ajax传数据乱码
先前将数据传到servlet中用如下代码
<%@page language="java" contentType="text/html;charset=utf-8"
pageEncoding="utf-8"%>
<html>
<head>
<title>首页</title>
<meta charset="utf-8">
<script type="text/javascript">
var xmlHttp;
function check() {
xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = checkResult;
var username = $$("username").value;
var password = $$("password").value;
var url = "/login?username="username + "&password=" + password;
xmlHttp.open("POST", url, true);
xmlHttp.send(null);
}
function checkResult() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
var json = JSON.parse(xmlHttp.responseText);
var result = json.result;
var html = json.html;
$$("msg").innerHTML = html;
if(result == 1)
setTimeout("window.location.reload();", 800);
}
}
function $$(id) {
return document.getElementById(id);
}
function keylistener(event){
if(event.keyCode == 13){
check();
}
}
</script>
</head>
<body>
<div>
<h3>
后台管理 <small>登录</small>
</h3>
</div>
<div id="login">
<span id="msg"></span>
<div>
<div class="input-group">
<span>帐号</span> <input id="username"
name="username" type="text"
placeholder="UserName" οnkeydοwn="keylistener(event)" />
</div>
<div>
<span>密码</span> <input id="password"
name="password" type="password" οnkeydοwn="keylistener(event)" />
</div>
<a id="forget">忘记密码?</a> <br /> <br />
<div id="btn">
<button οnclick="check()" id="btn-login">登录</button>
<a href="#"><button>注册</button></a>
</div>
</div>
</div>
</body>
</html>
结果乱码:
将check()方法改成如下
function check() {
xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = checkResult;
var username = $$("username").value;
var password = $$("password").value;
var url = "/login";
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
xmlHttp.send("username=" + username + "&password=" + password);//此处传参
}
XMLHTTPRequest对象传数据乱码问题解决:
3.Cookie乱码
登录成功后将数据存到Cookie中,若遇到中文也会乱码
String data = "中文数据";
Cookie cookie = new Cookie("data", URLEncoder.encode(data, "utf-8"));//编码
当取出数据时也要响应的解码
Cookie[] cookies = request.getCookies();
AdminUser au = null;
if(cookies != null){
for(Cookie cookie : cookies){
if("data".equals(cookie.getName())){
au = AdminUserDAO.getAdminUser(URLDecoder.decode(cookie.getValue(), "utf-8"));//解码
}
}
}