如果您查看生成的jsp类的顶部,您将看到以下行
private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
现在,自定义
out对象的一种可能解决方案是具有自定义实现
JspFactory。
脚步
创建一个自定义的JspFactory实现
public class MyJspFactory extends JspFactory { private static JspFactory _myFactory = null; public MyJspFactory(JspFactory factory) { _myFactory = factory; } //All abstract methods which looks up _myFactory and does the same thing public PageContext getPageContext(Servlet servlet, ServletRequest request, ServletResponse response, String errorPageURL, boolean needsSession, int bufferSize, boolean autoflush) { PageContext myCtxt = _myFactory.getPageContext(....) //Create a customPageContext and wrap myCtxt in it and return }}创建一个CutsomPageContext类
public class MyPageContext extends PageContext { private PageContext _ctxt = null; public void setPageContext(PageContext ctxt) { _ctxt = ctxt; } //Implement all abstract methods using _ctxt object @override public JspWriter getOut() { JspWriter _out = _ctxt.getOut(); //Wrap _out object using MyJSPWriter as mentioned in question and return back; }}现在,在servlet的初始化过程中,添加以下行
JspFactory newFactory = new MyJspFactory(JspFactory.getDefaultFactory());JspFactory.setDefaultFactory(newFactory);
我还没有尝试过。但从概念上讲,它应该起作用。如果您可以通过此方法实现目标,请告诉我们。
祝好运!



