Servlet编程是通过javax.servlet和javax.servlet.http这两个包的类和接口来实现的,其中一个重要的就是javax.servlet.Servlet接口。所有的servlet必须实现或者继承该接口的类。
httpServer1:
多了两个类:StaticResourceProcessor、ServletProcessor1(代替了简单的response.sendStaticResource)
StaticResourceProcessor:
只有一个process类:
简单的呼叫Response对象的sendStaticResource方法
ServletProcessor1:
只有一个process类:
servlet向下转换为javax.servlet.Servlet做法:
servlet = (Servelt)myClass.newInstance();
调用service方法。
servlet.service((ServletRequest) request,(ServletResponse) response);
直接转换ServletRequest和ServeltResponse,并调用service方法是不安全的。(为什么???)
(可能因为这样容易被其他类调用到parse和sendStaticResource方法)
解决方法:
将ServletRequest和ServletResponse对象设为私有:
private ServletRequest request = null;
构造一个RequestFacade对象并把它传递给service方法:
public RequestFacade (Request request){
this.request = request;
}
RequestFacade requestFacade = RequestFacade(request);
ResponseFacade responseFacade = new ResponseFacade(response);
try{
servlet = (Servelt)myClass.newInstance();
servlet.service((ServletRequest) requestFacade,(ServletResponse) responseFacade);
}
httpServer1:
多了两个类:StaticResourceProcessor、ServletProcessor1(代替了简单的response.sendStaticResource)
StaticResourceProcessor:
只有一个process类:
简单的呼叫Response对象的sendStaticResource方法
ServletProcessor1:
只有一个process类:
servlet向下转换为javax.servlet.Servlet做法:
servlet = (Servelt)myClass.newInstance();
调用service方法。
servlet.service((ServletRequest) request,(ServletResponse) response);
直接转换ServletRequest和ServeltResponse,并调用service方法是不安全的。(为什么???)
(可能因为这样容易被其他类调用到parse和sendStaticResource方法)
解决方法:
将ServletRequest和ServletResponse对象设为私有:
private ServletRequest request = null;
构造一个RequestFacade对象并把它传递给service方法:
public RequestFacade (Request request){
this.request = request;
}
RequestFacade requestFacade = RequestFacade(request);
ResponseFacade responseFacade = new ResponseFacade(response);
try{
servlet = (Servelt)myClass.newInstance();
servlet.service((ServletRequest) requestFacade,(ServletResponse) responseFacade);
}