包装模式简介:

package com.itheima.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader; public class MyBufferedReader extends BufferedReader {//似你 BufferedReader包装类
//针对父类已经是一个包装类的情况下,包装模式可以进一步简化 /*private BufferedReader br;//还有你
private int i=0;
public MyBufferedReader(Reader in) {
super(in);
this.br =(BufferedReader) in;//拜托你
}
@Override
public String readLine() throws IOException {
String str = br.readLine();
if(str==null)
return null;
i++;
return i+" "+str;
}*/ //改下后的
//private BufferedReader br;//还有你
private int i=0;
public MyBufferedReader(Reader in) {
super(in);
//this.br =(BufferedReader) in;//拜托你
}
@Override
public String readLine() throws IOException {
String str = super.readLine();
if(str==null)
return null;
i++;
return i+" "+str;
}
}

先继承同样的接口,然后将接口传进来,通过构造方法

测试

package com.itheima.test;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader; public class WrapperTest { public static void main(String[] args) {
try {
FileReader fr = new FileReader("src/com/itheima/test/MyBufferedReader.java");
BufferedReader br=new BufferedReader(fr);
MyBufferedReader mbr = new MyBufferedReader(br);
String line=null;
while((line=mbr.readLine())!=null){
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
} }

tomcat 的高版本已经解决了get请求的乱码问题,如果要包装的类实现的接口已经有一个包装类了,可以直接继承那个包装类,进行简单的包装,简单包装后直接写需要包装的方法就可以了。

package com.itheima.web.filter;

import java.io.IOException;
import java.io.UnsupportedEncodingException; 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; /**
* Servlet Filter implementation class CharacterEncodingFilter
*/
public class CharacterEncodingFilter implements Filter { public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp; request.setCharacterEncoding("UTF-8");//post乱码
response.setCharacterEncoding("UTF-8");//添加,有经验
response.setContentType("text/html;charset=UTF-8");//响应时没有乱码 //对于get提交,也要解决??? (Tomcat高版本下对get提交已解决) //调包
MyHttpServletRequest myrequest = new MyHttpServletRequest(request);
chain.doFilter(myrequest, response);
} class MyHttpServletRequest extends HttpServletRequestWrapper{ public MyHttpServletRequest(HttpServletRequest request) {
super(request);
} @Override//改写你
public String getParameter(String name) {
String paramValue = super.getParameter(name);//取参数值
if(paramValue==null)
return null;
//要判断请求类型??? get 就要做乱码处理
String type = super.getMethod();//post get获取提交方式
if(type.equalsIgnoreCase("get")){
//说明要进行乱码处理
try {
paramValue = new String(paramValue.getBytes("iso-8859-1"),"UTF-8");//getBytes(“iso-8859-1”)是使用iso-8859-1编码表进行转换。
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} return paramValue;
}
} public void init(FilterConfig fConfig) throws ServletException { } }
 
05-11 22:08