如何覆盖(重载)jsp页面中的out方法 目前需要对已有的jsp页面out方法进行重载实现,比如最简单的过滤关键词,或者转繁体,这样.如何实现覆盖out方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在out之前做过滤不行吗,比如我回帖的时候输入了一些不好的我文字可以在后台进行过滤,然后再out到页面上 Jsp 中out的类型是 JspWriterImpl 实现了JspWriter的接口你要重写只有继承这个JspWriterImpl 类实现你自己的方法 out是 jsp的隐式对象。 "out方法" 好像没有这个方法吧。对象也没有听说过可以覆盖啊。 如果这样的话,怎么保证编译的时候用的是他重写的,而不是原来的,感觉难度蛮大的,在重写了接口后,要先编译成java文件,类文件 我怀疑那个方法是final,所以不能重载调查一下是不是final的。呵呵 楼主的意思应该只是想自己重写一个out,然后自己调用自己的out,网页面上输出一些东西吧如果那样的话就应该可以继承JspWriter,实现自己的print方法,不过似乎里面也是那么回事还是相当于在后台进行过滤,然后再out到页面上 可以考虑重写一个类 然后在jsp里面 把局部变量out 替换成你的类不过 我估计这样做 会出现一些类加载的错误你要重写这个类必须要当前的classloader能加载到 JspWriterImpl 但是这个类应该是tomcat的核心类,在更高的类加载层次已经被加载了 这样太混乱了... lz 有兴趣可以试一下看看 要对输出流操作,最好还是在servlet中进行,可以避免jsp out的问题 1 自己重写一个,2 动态嵌入AOP到容器的里面3 其实可以在返回到用户前,用Filer 拦截的,使用 HttpResponseWrapper 就行了。 参考这个看看class ReduceWrapper extends HttpServletResponseWrapper { public static final int OT_NONE = 0, OT_WRITER = 1, OT_STREAM = 2; private int outputType = OT_NONE; private ServletOutputStream output = null; private PrintWriter writer = null; private ByteArrayOutputStream buffer = null; public ReduceWrapper(HttpServletResponse resp) throws IOException { super(resp); buffer = new ByteArrayOutputStream(); } @Override public PrintWriter getWriter() throws IOException { if (outputType == OT_STREAM) throw new IllegalStateException(); else if (outputType == OT_WRITER) return writer; else { outputType = OT_WRITER; writer = new PrintWriter(new OutputStreamWriter(buffer, getCharacterEncoding())); return writer; } } @Override public ServletOutputStream getOutputStream() throws IOException { if (outputType == OT_WRITER) throw new IllegalStateException(); else if (outputType == OT_STREAM) return output; else { outputType = OT_STREAM; output = new WrappedOutputStream(buffer); return output; } } @Override public void flushBuffer() throws IOException { if (outputType == OT_WRITER) writer.flush(); if (outputType == OT_STREAM) output.flush(); } @Override public void reset() { outputType = OT_NONE; buffer.reset(); } public byte[] getResponseData() throws IOException { flushBuffer(); return buffer.toByteArray(); } class WrappedOutputStream extends ServletOutputStream { private ByteArrayOutputStream buffer; public WrappedOutputStream(ByteArrayOutputStream buffer) { this.buffer = buffer; } @Override public void write(int b) throws IOException { buffer.write(b); } public byte[] toByteArray() { return buffer.toByteArray(); } }} ReduceWrapper wrapper = new ReduceWrapper(resp); try { chain.doFilter(request, wrapper); } catch (Exception ex) { ex.printStackTrace(); } 这样使用就行原文 http://www.java2000.net/viewthread.jsp?tid=4486 不过需要会员 访问google天气预报API出现乱码 【吼吼】收集整理tomcat各种异常报错及解决方案。。。 html标签过滤问题 stuts中如何使用frame Myeclipse 5.1中启动websphere6.1 错误? jsf页面包含问题 不解 求救。中文问题 如何配置JDK+WebLogic环境 求救:servlet编译问题 idea tomcat有问题 webapp问题。。。。。。 请问Spring中的重点是什么啊?
你要重写只有继承这个JspWriterImpl 类
实现你自己的方法
"out方法" 好像没有这个方法吧。
对象也没有听说过可以覆盖啊。
楼主的意思应该只是想自己重写一个out,然后自己调用自己的out,网页面上输出一些东西吧如果那样的话就应该可以继承JspWriter,实现自己的print方法,不过似乎里面也是那么回事还是相当于在后台进行过滤,然后再out到页面上
可以考虑重写一个类 然后在jsp里面 把局部变量out 替换成你的类不过 我估计这样做 会出现一些类加载的错误你要重写这个类必须要当前的classloader能加载到 JspWriterImpl 但是这个类应该是tomcat的核心类,在更高的类加载层次已经被加载了
这样太混乱了... lz 有兴趣可以试一下看看
1 自己重写一个,
2 动态嵌入AOP到容器的里面
3 其实可以在返回到用户前,用Filer 拦截的,使用 HttpResponseWrapper 就行了。 参考这个看看
class ReduceWrapper extends HttpServletResponseWrapper {
public static final int OT_NONE = 0, OT_WRITER = 1, OT_STREAM = 2; private int outputType = OT_NONE; private ServletOutputStream output = null; private PrintWriter writer = null; private ByteArrayOutputStream buffer = null; public ReduceWrapper(HttpServletResponse resp) throws IOException {
super(resp);
buffer = new ByteArrayOutputStream();
} @Override
public PrintWriter getWriter() throws IOException {
if (outputType == OT_STREAM)
throw new IllegalStateException();
else if (outputType == OT_WRITER)
return writer;
else {
outputType = OT_WRITER;
writer = new PrintWriter(new OutputStreamWriter(buffer, getCharacterEncoding()));
return writer;
}
} @Override
public ServletOutputStream getOutputStream() throws IOException {
if (outputType == OT_WRITER)
throw new IllegalStateException();
else if (outputType == OT_STREAM)
return output;
else {
outputType = OT_STREAM;
output = new WrappedOutputStream(buffer);
return output;
}
} @Override
public void flushBuffer() throws IOException {
if (outputType == OT_WRITER)
writer.flush();
if (outputType == OT_STREAM)
output.flush();
} @Override
public void reset() {
outputType = OT_NONE;
buffer.reset();
} public byte[] getResponseData() throws IOException {
flushBuffer();
return buffer.toByteArray();
} class WrappedOutputStream extends ServletOutputStream {
private ByteArrayOutputStream buffer; public WrappedOutputStream(ByteArrayOutputStream buffer) {
this.buffer = buffer;
} @Override
public void write(int b) throws IOException {
buffer.write(b);
} public byte[] toByteArray() {
return buffer.toByteArray();
}
}
}
try {
chain.doFilter(request, wrapper);
} catch (Exception ex) {
ex.printStackTrace();
} 这样使用就行原文 http://www.java2000.net/viewthread.jsp?tid=4486 不过需要会员