注意,这里是针对Tomcat容器中get方式提交的servlet中获得参数,参数中有中文的时候乱码的问题:
之前我已经讲过,Tomcat8.0及以上URIEncoding都是utf-8的默认编码,不会出现get方式乱码的问题;
只有Tomcat7.0及以下的版本,默认的URIEncoding才是ISO-8859-1才会出现中文乱码,当然我们也可以在Tomcat的conf中的server.xml中修改默认的URLEncoding来解决此问题(但是我们这里测试是要求不能修改Tomcat容器中的默认编码的时候哦),前面关于Tomcat的编码已经有了详细讲解,链接如下,这里就不再赘述了。
在实现装饰者对getParameter进行加强的时候,我们发现:
HttpServletRequest只是一个接口,我们继承重写不能使用接口;所以我们查阅API发现
其中有一个实现子类HttpServletRequestWrapper,所以后面我们继承自HttpServletRequestWrapper进行重写getParameter!
jsp中的代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>测试get方式中文乱码</title>
</head>
<body>
<form action="${pageContext.servletContext.contextPath }/servlet/TestEncodingServlet">
name:<input type="text" name="name" />
<input type="submit" value="提交" />
</form>
</body>
</html>
servlet中的代码:
package day03;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestEncodingServlet extends HttpServlet {
private static final long serialVersionUID = 3996799889172019227L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String name = request.getParameter("name");
System.out.println(name);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
}
}
xml中的配置:
filter中的代码:
package day03;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
* 解决get方式中文参数乱码问题
* @author mzy
*
*/
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig arg0) throws ServletException {}
@Override
public void destroy() {}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest tmpRequest = (HttpServletRequest)request;
/*
* 如果是POST方式提交
*/
request.setCharacterEncoding("utf-8");
// 装饰者类使用,针对get方式中的中文参数
MyHttpRequest myRequest = new MyHttpRequest(tmpRequest);
/*
* 放行:注意是放行我们装饰之后的request
*/
chain.doFilter(myRequest, response);
}
}
/**
* 装饰者类
* 1.继承自HttpServletRequestWrapper非final的
* @author mzy
*/
class MyHttpRequest extends HttpServletRequestWrapper {
// 2.声明一个被装饰者类型的成员变量
private HttpServletRequest request;
// 3.接收被装饰者类对象
public MyHttpRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
/*
* 4.加强方法:重写getParameter方法
*/
@Override
public String getParameter(String name) {
try {
// 得到原来的参数
String value = request.getParameter(name);
// 判断是否是get方式提交的
if ("GET".equals(request.getMethod())) {
value = new String(value.getBytes("ISO-8859-1"), "utf-8");
}
return value;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
throw new RuntimeException();
}
}
}