Java纯手打web服务器(三)

概要:考虑到上一篇中的request和response对象的安全性,不允许servlt程序员在service方法中把servletRequest和servletResponse对象进行强转request和response对象,而访问到request和response对象中私有的方法,这里引入外观类:RequestFacade和ResponseFacade,只实现ServletResponse和ServletRequest中声明的方法

具体代码如下:

public class RequestFacade implements ServletRequest {

  private ServletRequest request = null;

  public RequestFacade(Request request) {
    this.request = request;
  }
。。。。。。
}


public class ResponseFacade implements ServletResponse {

  private ServletResponse response;
  public ResponseFacade(Response response) {
    this.response = response;
  }
。。。。。
}

将request和response对象作为私有变量在构造器是进行传入,用此方法进行保护。

分析器相应的代码修改如下:

Servlet servlet = null;
    RequestFacade requestFacade = new RequestFacade(request);
    ResponseFacade responseFacade = new ResponseFacade(response);
    try {
      servlet = (Servlet) myClass.newInstance();
      servlet.service((ServletRequest) requestFacade, (ServletResponse) responseFacade);
    }
    catch (Exception e) {
      System.out.println(e.toString());
    }
posted @ 2020-09-24 18:33  好奇成传奇  阅读(90)  评论(0编辑  收藏  举报