使用filter对请求设置编码

一、复习

加强方法:

1)继承

2)装饰模式

3)动态代理。

使用装饰模式:

1)要求装饰者和被装饰者实现同一个接口或者继承同一个类。

2)装饰者要求被装饰者的引用。

3)对于要加强的方法进行加强,对于不需要的加强,执行原先的方法。

二、需求分析

1)使用filter来对所有的请求进行过滤。需要设置的url-pattern设置成/* 并且位置是在所有的filter的过滤器(filter-mapping)的前面。

2)对原先的方法进行加强。

  1、加强的方法---request类-->通过filter的放行方法 重写request。

自己写的版本:

 package jd.com.coding;

 import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @WebFilter(filterName = "FilterAllSeting")
public class FilterAllSeting implements Filter {
public void destroy() {
} public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest re=(HttpServletRequest) req;
HttpServletResponse res=(HttpServletResponse) resp;
chain.doFilter(new SetCoding(re), resp);
} public void init(FilterConfig config) throws ServletException {
} } class SetCoding extends HttpServletRequestWrapper{
private HttpServletRequest request; public SetCoding(HttpServletRequest request){
super(request);
this.request=request;
} @Override
public String getParameter(String name) {
String method=request.getMethod();
String val=request.getParameter(name);
if( method.equalsIgnoreCase("get"))
{ try {
return new String(val.getBytes("ISO_8859_1"), "UTF-8");
}catch (IOException ex){
ex.printStackTrace();
}
} else if(method.equalsIgnoreCase("POST")){
try {
request.setCharacterEncoding("utf-8");
}catch (IOException ex){
ex.printStackTrace();
} }
return super.getParameter(name);
}
}

最终版本:

 package cn.itcast.web.filter;

 import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map; 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;
import javax.servlet.http.HttpServletResponse;
/**
* 统一编码
* @author Administrator
*
*/
public class EncodingFilter implements Filter { @Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub } @Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
//1.强转
HttpServletRequest request=(HttpServletRequest) req;
HttpServletResponse response=(HttpServletResponse) resp; //2.放行
chain.doFilter(new MyRequest(request), response);
} @Override
public void destroy() {
// TODO Auto-generated method stub } }
class MyRequest extends HttpServletRequestWrapper{
private HttpServletRequest request;
private boolean flag=true; public MyRequest(HttpServletRequest request) {
super(request);
this.request=request;
} @Override
public String getParameter(String name) {
if(name==null || name.trim().length()==){
return null;
}
String[] values = getParameterValues(name);
if(values==null || values.length==){
return null;
} return values[];
} @Override
/**
* hobby=[eat,drink]
*/
public String[] getParameterValues(String name) {
if(name==null || name.trim().length()==){
return null;
}
Map<String, String[]> map = getParameterMap();
if(map==null || map.size()==){
return null;
} return map.get(name);
} @Override
/**
* map{ username=[tom],password=[123],hobby=[eat,drink]}
*/
public Map<String,String[]> getParameterMap() { /**
* 首先判断请求方式
* 若为post request.setchar...(utf-8)
* 若为get 将map中的值遍历编码就可以了
*/
String method = request.getMethod();
if("post".equalsIgnoreCase(method)){
try {
request.setCharacterEncoding("utf-8");
return request.getParameterMap();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if("get".equalsIgnoreCase(method)){
Map<String,String[]> map = request.getParameterMap();
if(flag){
for (String key:map.keySet()) {
String[] arr = map.get(key);
//继续遍历数组
for(int i=;i<arr.length;i++){
//编码
try {
arr[i]=new String(arr[i].getBytes("iso8859-1"),"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
flag=false;
}
//需要遍历map 修改value的每一个数据的编码 return map;
} return super.getParameterMap();
} }
上一篇:备忘:MySQL中修改表中某列的数据类型、删除外键约束


下一篇:mysql 批量修改表前缀