您的位置:首页 > Web前端 > JavaScript

用过滤器来解决JSP中文乱码问题

2016-06-25 17:55 429 查看
转载自:http://www.cnblogs.com/liuling/archive/2012/12/17/asdfsdfa.html

首先写一个过滤器的类,如下:package com.zrcx.web.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
* Created by Administrator on 2016/6/24.
*/
public class CharFilter implements Filter {
class MyRequest extends HttpServletRequestWrapper {

@Override
public String getParameter(String param) {
String value = null;
try {
//post
super.setCharacterEncoding(encoding);//把编码转换为encoding
value = super.getParameter(param);
if (super.getMethod().equalsIgnoreCase("GET")) {
if (value != null) {
value = new String(value.getBytes("iso8859-1"), encoding);
}
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}

public MyRequest(HttpServletRequest request) {
super(request);
}

}

protected String encoding = null;

public void destroy() { //销毁
// TODO Auto-generated method stub
this.encoding = null;
}

//对编码问题进行转换
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=" + encoding);
//过滤未登录用户
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String path = req.getServletPath();
String param = req.getQueryString();
if (path != null) {
path = path + "?" + param;//全请求路径
}
if (path.endsWith("myAddress") || path.endsWith("myJingDong") || path.indexOf("myShouCang") != -1 || path.endsWith("updateUser") || path.indexOf("showOrder") != -1 || path.indexOf("showValidOrder") != -1 || path.indexOf("showCancelOrder") != -1 || path.indexOf("fillOrder") != -1) {
HttpSession session = req.getSession();
String userName = (String) session.getAttribute("username");
if (userName == null) {
session.setAttribute("url", path.replaceFirst("/", ""));
System.out.println(session.getAttribute("url"));
resp.sendRedirect("user.do?op=loginAction");
return;
}
}
//把过滤器给下一个过滤器或资源处理器
chain.doFilter(new MyRequest((HttpServletRequest) request), response);
}

public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
this.encoding = filterConfig.getInitParameter("encoding");//encoding在web.xml中指定
}

}
然后在web.xml对该过滤器进行注册和映射:
<filter>
<filter-name>CharFilter</filter-name>
<filter-class>com.zrcx.web.filter.CharFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>上面写的过滤器MyFilter类,本来只能处理post提交的数据(post是先处理后接收,get是先接收后处理)。
但是MyFilter里面在对任
4000
何页面过滤的时候,来了一个偷梁换柱:把原来客户端请求的request给换掉了,换成自己定义的一个request了,即内部类MyRequest,不过该类要继承一个类HttpServletRequestWrapper。

在自定义的一个内部类MyRequest里面,实现了一个好强大的功能,就是重写了request的getParameter()方法。该方法里面即处理了post提交,又能处理get提交,返回的值就是处理后的值,所以该过滤器就能实现处理post和get提交的乱码问题!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: