这些天在对一个项目进行二次开发,要对输入和输出的数据做一些修改,但是上一个开发人员没有进行良好的封装,所有数据直接从request.getParameter(),获取,然后response.getWrite()直接进行输出操作,如果我要一个一个程序去修改的话,应用程序上40多个servlet都得改程序,第一工作量比较大,第二自己看着恶心,以后出了问题BOSS还得找我,所以干脆想了个办法,利用拦截器继承改写request和response,这样所有进入应用程序的数据都在我的掌控下了。具体实现见如下代码!
1.web.xml的拦截器配置
changeResponseFilter
*.filter.ChangeResponseFilter
changeResponseFilter
*
2.拦截器实现
package com.bjfulei.robo.machine.filter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
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.HttpServletResponse;
import com.bjfulei.robo.machine.util.ThreeDES;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import BO.inter.param.InGeneralParam;
import BO.systools.ListMgr;
import net.sf.json.JSONObject;
/***
* mail: [email protected]
* date: 2016年8月23日 下午6:54:58
*/
public class ChangeResponseFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
MyResponseWrapper responseWrapper = new MyResponseWrapper((HttpServletResponse) response);
MyRequestWrapper requestWrapper = new MyRequestWrapper((HttpServletRequest) request);
chain.doFilter(requestWrapper, responseWrapper);
MyWriter writer = responseWrapper.getMyWriter();
if (writer != null) {
String content = writer.getContent();
response.getWriter().write(ListMgr.string2Unicode2(content));
response.getWriter().flush();
response.getWriter().close();
}
}
public void init(FilterConfig arg0) throws ServletException {
}
}
3.request改写类
package com.bjfulei.robo.machine.filter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class MyRequestWrapper extends HttpServletRequestWrapper {
private Map paramMap;
public MyRequestWrapper(HttpServletRequest request) {
super(request);
paramMap =getParameterMap(request);
}
/**
* FIXME 修改后的获取参数方法
* @param name
* @return
*/
public String getParameter(String name){
return paramMap.get(name);
}
/**
* FIXME 修改参数值的方法
* @param name
* @return
*/
public String updateParameter(String name,String value){
return paramMap.put(name,value);
}
public Map getParameterMap(){
return paramMap;
}
@SuppressWarnings("rawtypes")
public static Map getParameterMap(HttpServletRequest request) {
// 参数Map
Map properties = request.getParameterMap();
// 返回值Map
Map returnMap = new HashMap();
Iterator entries = properties.entrySet().iterator();
Map.Entry entry;
String name = "";
String value = "";
while (entries.hasNext()) {
entry = (Map.Entry) entries.next();
name = (String) entry.getKey();
Object valueObj = entry.getValue();
if(null == valueObj){
value = "";
}else if(valueObj instanceof String[]){
String[] values = (String[])valueObj;
for(int i=0;i
4.response改写类
package com.bjfulei.robo.machine.filter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class MyResponseWrapper extends HttpServletResponseWrapper {
private MyWriter myWriter;
public MyResponseWrapper(HttpServletResponse response) {
super(response);
try {
myWriter = new MyWriter(response.getWriter());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public PrintWriter getWriter() throws IOException {
return myWriter;
}
public MyWriter getMyWriter() {
return myWriter;
}
}
package com.bjfulei.robo.machine.filter;
import java.io.PrintWriter;
public class MyWriter extends PrintWriter{
private StringBuilder buffer;
public MyWriter(PrintWriter out) {
super(out);
buffer = new StringBuilder();
}
@Override
public void write(char[] buf, int off, int len) {
// super.write(buf, off, len);
char[] dest = new char[len];
System.arraycopy(buf, off, dest, 0, len);
buffer.append(dest);
}
@Override
public void write(char[] buf) {
super.write(buf);
}
@Override
public void write(int c) {
super.write(c);
}
@Override
public void write(String s, int off, int len) {
super.write(s, off, len);
}
@Override
public void write(String s) {
//super.write(s);
buffer.append(s);
}
public String getContent(){
return buffer.toString();
}
public void flush(){
}
public void close(){
}
}
利用拦截器实现了所有输入输出数据流的自定义,分享到博客来,希望对新手有所帮助!