使用过滤器解决JSP页面的乱码问题

时间:2021-11-07 07:11:35
乱码详情
 总结:讨论了使用GET和POST方法,控制台和JSP页面显示的问题。
最终发现:在servlet或者过滤器中添加:request.setCharacterEncoding("utf-8");
        res.setContentType("text/html;charset=utf-8");
 即可解决页面显示乱码问题。
JSP页面中
<%@ page language="java"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/encodingServlet" method="get"> //选择GET或者POST提交
 
<input type="text" name="text" > <input type="submit"> </form> </body> </html>
过滤器页面:  
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response; //放行!!!——————————————————————
chain.doFilter(newRequest, res); }
编写的servlet的get方法中:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String str=request.getParameter("text");
if(str!=null)System.out.println(str);
response.getWriter().write(str);
}
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAeoAAAB1CAYAAACBKqIYAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAHYcAAB2HAY/l8WUAABIPSURBVHhe7d37c1Rlnsfx/ieS7V+c0S1mZ63BKhFqSwRm14zWbCLsCI5Ta0bLwcziUhkcNcXNgDiQsbYgspaGS5LmJgYd10YuhntClQwJCGmUS2IM6UAiBESNito/+MN3z3PO053T3eckpzsdOKTfr6pPlZx7Ou355Dl9ujsgAADAtyhqAAB8jKIGAMDHKGoAAHyMogYAwMcoagAAfIyiBgDAxyhqAAB8jKIGAMDHKGoAAHyMogYAwMcoagAAfIyiBgDAxyhqAAB8jKIGgFF27NgxaWhoIDmMekzzBUUNAKNMFUssFiM5jHpM80VGRf3WW28RQgixxYs333xTfvjhB5LDqMc0X2Rc1AAAC0V980JRu6CoAWCQ13Pi1q1b5fvvvyc5jHpM8wV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H6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAxihoAAB+jqAEA8DGKGgAAH6OoAQDwMYoaAAAfo6gBAPAtkf8HUxwtAV2C4MoAAAAASUVORK5CYII=" alt="" width="287" />
我们在过滤器中注释掉

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

控制台显示
JSP页面显示
GET
你好啊 
???
POST
你好å 
:浣犲ソ鍟�
在filte过滤器加上:request.setCharacterEncoding("utf-8");
        res.setContentType("text/html;charset=utf-8");
控制台显示
JSP页面显示
GET
你好啊 
你好啊
POST
你好啊 
你好啊
这里JSP页面出现乱码是因为:在getParameter()方法中,得到的字符串是乱码的。
在Filter中:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response; //放行!!!——————————————————————
//
EnhanceRequest newRequest=new EnhanceRequest(req);
  // req.setCharacterEncoding("utf-8");
// res.setContentType("text/html;charset=utf-8");
System.out.println("filtering");
chain.doFilter(newRequest, res); }
使用装饰者模式,增强getParameter().
小插曲:  装饰者模式:
增强类与被增强类实现统一接口。
增强类中传入被增强的类
需要增强的方法重写。
之后在Filter中增强req:
class EnhanceRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
public EnhanceRequest(HttpServletRequest req) {
super(req);
this.request=req;
}
//对getParameter增强
@Override
public String getParameter(String name) {
String parameter =request.getParameter(name);//乱码
try {
parameter=new String(parameter.getBytes("iso8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return parameter;
} }
控制台显示
JSP页面显示
GET
???
???
POST
你好啊
???
 同时发现:当JSP页面中去除掉这条代码contentType="text/html; charset=UTF-8",将造成JSP页面显示数据为乱码。
结论:要解决JSP页面和控制台的乱码问题,只要在filter过滤器加上:request.setCharacterEncoding("utf-8");
        res.setContentType("text/html;charset=utf-8");
或者就在servlet页面上添加:request.setCharacterEncoding("utf-8");
        res.setContentType("text/html;charset=utf-8");
 便可解决POST和GET方法的乱码问题。