我们经常会自定义servlet然后重写doget和dopost方法
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); ServletInputStream in = request.getInputStream(); String s = IOUtils.toString(in, "utf-8"); System.out.println(s); }
参数中有HttpServletRequest和HttpServletResponse他们都是接口,那么实现类是什么?
public class HttpServletResponseWrapper extends ServletResponseWrapper implements HttpServletResponse {
实现类是ServletResponseWrapper,这个类中有一个ServletResponse成员,但是他是接口,实现类是ResponseFacade。
public class ServletResponseWrapper implements ServletResponse { private ServletResponse response;
在自定义的servlet中我们经常会设置字符编码。
response.setContentType("text/html;charset=utf-8");
他调用了ServletResponseWrapper的方法,这个方法又调用了成员的方法。
@Override public void setContentType(String type) { this.response.setContentType(type); }
@Override public void setContentType(String type) { if (isCommitted()) { return; } if (SecurityUtil.isPackageProtectionEnabled()){ AccessController.doPrivileged(new SetContentTypePrivilegedAction(type)); } else { response.setContentType(type); } }
终于知道了自定义servlet中response的实现类。。。