通过装饰者模式来增强request对象的getParameter()方法处理GET和POST的中文乱码问题
在实际JAVA开发中会经常遇到中文乱码,由于通过浏览器传递的参数是通过ISO8859-1进行编码,在Controller接受之后,不处理直接输出则显示是乱码。
- 处理POST方式提交的中文
通过设置request对象的字符集来处理。 request.setCharacterEncoding("UTF-8");
- 处理GET方式提交的中文
- 由于GET提交方式通过传参的方式提交数据,是不能通过设置字符集来出来,需要通过String对象来进行重新编码,将ISO8859-1编码进行解码,再使用UTF-8进行编码。
String value=super.getParameter(name);
value=new String(value.getBytes("ISO8859-1"),"UTF-8");
单个表单表单,我们可以通过这种直接的方式来出来,如果多个表单如何处理呢?难道一个一个在Servlet中去进行处理,这么工作量也太大了。
解决思路
通过Filter类过滤器来进行来处理
第一步:新建一个增强类MyHttpServletReuqest
目的:用来增强HttpServletRequest类中的Request对象。
代码如下:
由于JAVA中已经内置了HttpServletRequest的装饰类HttpServletRequestWrapper,我们只需要通过继承方式来获得装饰类HttpServletRequestWrapper的方法,然后进行方式重写。
public class MyHttpServletReuqest extends HttpServletRequestWrapper {
//将HttpServletRequest 定义为MyHttpServletReuqest 的属性request
private HttpServletRequest request;
public MyHttpServletReuqest(HttpServletRequest request){
super(request);
this.request=request;
}
@Override
public String getParameter(String name) {
//获得请求方法
String method=request.getMethod();
if("GET".equalsIgnoreCase(method)){
String value=super.getParameter(name);
try {
value=new String(value.getBytes("ISO8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}else if("POST".equalsIgnoreCase(method)){
try {
//设置request的字符集
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return super.getParameter(name);
}
}
第二步:新建一个通用的字符集编码过滤器
public class GenericEncodingFilter implements Filter{
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
throws IOException, ServletException {
//在过滤器中增强Request对象,将request作为参数传给增强类
HttpServletRequest reqs=(HttpServletRequest) req;
MyHttpServletReuqest request=new MyHttpServletReuqest(reqs);
chain.doFilter(request, resp);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
第三步:在工程配置文件web.xml中配置字符集编码过滤器GenericEncodingFilter
<filter>
<filter-name>GenericEncodingFilter</filter-name>
<!--这是GenericEncodingFilter类的全路径-->
<filter-class>Filter.GenericEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GenericEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
第四步:在控制器中新建一个PostGetmethod类
该类用来接收表单递交的。
public class PostGetmethod extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username=request.getParameter("username");
System.out.println(username);
request.getRequestDispatcher("/Filter/demo4.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
第五步:在工程配置文件(web.xml)中配置PostGetmethod类
<servlet>
<description></description>
<servlet-name>PostGetmethod</servlet-name>
<!--这里是类PostGetmethod的路径-->
<servlet-class>FrontController.PostGetmethod</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PostGetmethod</servlet-name>
<url-pattern>/PostGetmethod</url-pattern>
</servlet-mapping>
第六步:新建一个测试文件demo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div>
<h2>Get方式提交表单</h2>
<form action="${pageContext.request.contextPath}/PostGetmethod" method="get">
<label>用户名:</label><input name="username" value="" /><br />
<button type="submit">提交</button>
</form>
</div>
<div>
<h2>POST提交表单</h2>
<form action="${pageContext.request.contextPath}/PostGetmethod" method="post">
<label>用户名:</label><input name="username" value="" /><br />
<button type="submit">提交</button>
</form>
</div>
</body>
</html>
测试结果:
执行结果:
整个过程的工作原理是: