内容:在上一篇中存在一些安全问题,因为我们需要将request和response想上转型为ServletRequest和ServletResponse,了解这个servlet容器内部工作原理的servlet程序员可以将ServletRequest和ServletResponse分别向下转型然后就能调用Request和Response的parse()和sendStaticResource()了。所以我们需要利用外观类来解决,添加两个RequestFacade和ResponseFacade,然后分别拥有request和response,再各自调用对应的方法。所以只要改动相应的代码即可。这里只给出差别代码。
HttpServer2:
if (request.getUri().startsWith("/servlet/")) { ServletProcessor2 processor = new ServletProcessor2(); processor.process(request, response); }
ServletProcessor2
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()); } catch (Throwable e) { System.out.println(e.toString()); }
时间: 2024-11-06 12:59:42