不知道大家发现没有上一小节我其实什么都没有讲,就只是放了自己的代码,没有Filter,每一个Servlet里面也没有对请求与响应的编码规范,这些就都在Filter里面进行操作了,好了下面开始放入Filter。
- EncodingFilter
- 拦截post请求的乱码问题
- 对于表单提交的数据乱码问题
- request.setCharacterEncoding("UTF-8");
- 对于服务器向浏览器发送数据乱码问题
- response.setContentType("text/html;charset=UTF-8");
public class EncodingFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = null;
HttpServletResponse resp = null;
//拿到HttpServletRequest和HttpServletResponse 对象 进行编码设置
req = (HttpServletRequest) request;
resp = (HttpServletResponse) response;
//设置响应的编码
resp.setContentType("text/html;charset=UTF-8");
//设置请求的编码
req.setCharacterEncoding("UTF-8");
//放行
chain.doFilter(req, resp);
}
public void init(FilterConfig fConfig) throws ServletException {
}
}
- 还有只要的一点就是对web.xml进行配置
filter-mapping可以有多个,也就是说一个Filter可以对多个不同路径下的资源进行响应与请求的处理
- 那对于get方法呢????
- 注意:
doFilter中ServletRequest和ServletResponse是没有直接调用处理乱码的两个方法,因为这两个方法是在
httpServletRequest和httpServletResponse里面, 所以应该做强转,向下转型对get请求,并没有处理
get请求 需要打回原形再用正确的编码进行处理 关键是在什么时候打回原形
在获取用户名的时候打回原形,也就是在调用request.getParamter()方法的时候,这个时候,先获取到一个错误的字符串,打回原形,再用正确的编码重新创建一个新的字符串
思考:把打回原形的功能直接放到.getParamter()方法里面去做,其实也就是对getParamter()做了一个增强
我这里先写一个 ,下一章 我单独补充一个简单案例 你知道什么是增强了
- 增强改写 同时使用 web.xml对
- 编码在配置文件中配置,可以使用init 方法拿到
package com.itbear.myFilter;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class MyHttpServletRequest extends HttpServletRequestWrapper{
private String encoding;
public MyHttpServletRequest(HttpServletRequest request, String encoding) {
super(request);
this.encoding = encoding;
}
@Override
public String getParameter(String name) {
String method = super.getMethod();
if("GET".equals(method)){
String str = super.getParameter(name);
try {
return new String(str.getBytes("ISO-8859-1"),encoding);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return super.getParameter(name);
}
}
package com.itbear.myFilter;
import java.io.IOException;
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.HttpServletResponse;
public class EncodingFilter implements Filter {
private String encoding;
public EncodingFilter() {
}
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
MyHttpServletRequest req = new MyHttpServletRequest((HttpServletRequest) request,encoding);
HttpServletResponse res = (HttpServletResponse) response;
res.setContentType("text/html;charset="+encoding);
req.setCharacterEncoding(encoding);
chain.doFilter(req, res);
}
public void init(FilterConfig fConfig) throws ServletException {
String encoding = fConfig.getInitParameter("encoding");
this.encoding = encoding;
}
}
web.xml 配置
<filter>
<display-name>EncodingFilter</display-name>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.itbear.myFilter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>