[英]Servlet filters and response wrapper
尝试弄乱响应内容,我尝试了此过滤器教程
所以我把我的班级定义为
public class FilterServletOutputStream extends ServletOutputStream
{
//=======================================================================
private DataOutputStream stream;
//=======================================================================
public FilterServletOutputStream(OutputStream output) {
stream = new DataOutputStream(output);
}
//=======================================================================
@Override
public void write(int b) throws IOException {
stream.write(b);
}
//=======================================================================
@Override
public void write(byte[] b) throws IOException {
stream.write(b);
}
//=======================================================================
@Override
public void write(byte[] b, int off, int len) throws IOException {
stream.write(b,off,len);
}
//=======================================================================
}
public class GenericResponseWrapper extends HttpServletResponseWrapper
{
private ByteArrayOutputStream output;
private int contentLength;
private String contentType;
public GenericResponseWrapper(HttpServletResponse response) {
super(response);
output=new ByteArrayOutputStream();
}
public byte[] getData() {
return output.toByteArray();
}
@Override
public ServletOutputStream getOutputStream()
{
return new FilterServletOutputStream(output);
}
@Override
public PrintWriter getWriter()
{
return new PrintWriter(getOutputStream(),true);
}
@Override
public void setContentLength(int length) {
this.contentLength = length;
super.setContentLength(length);
}
public int getContentLength() {
return contentLength;
}
@Override
public void setContentType(String type) {
this.contentType = type;
super.setContentType(type);
}
@Override
public String getContentType() {
return contentType;
}
}
然后是我的过滤器
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
//===================================================================
GenericResponseWrapper wrapper = new GenericResponseWrapper((HttpServletResponse) response);
chain.doFilter(request, wrapper);
OutputStream out = response.getOutputStream();
out.write(wrapper.getData());
out.write("test content".getBytes());
out.close();
//===================================================================
}
筛选器似乎还可以。 我可以读取“测试内容”,但似乎wrapper.getData()
返回0个字节。 当我调用chain.doFilter(..)
通常是一个有时分派到jsp页面的servlet。 尝试了JSP普通页面的URL,但似乎没有写数据。 过滤器配置为捕获所有请求,并且效果很好。
我怎么做错了? 唯一的区别是我添加了@override
。 但是我也尝试了没有它们。
添加了一个JSP。
<%@page contentType="text/html" pageEncoding="UTF-8" session="false"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
<%-- @include file="/static/css/divs.css" --%>
<%-- @include file="/static/css/links.css" --%>
</style>
<title>title</title>
</head>
<body style="margin-top: 0px; margin-left: 0px; margin-right: 0px">
<%--@include file="publicheader.jsp" --%>
<div class="desktopwebpagecontainer">
main page.
</div>
<%--@include file="publicfooter.jsp" --%>
</body>
</html>
解决此问题的原因是这使写手和outpstrm类成员(字段)成为了现实。
private PrintWriter pwriter = null;
private ServletOutputStream outpstrm = null;
然后改变获取它们的方法
@Override
public ServletOutputStream getOutputStream()
{
if (outpstrm == null) outpstrm = new AppServletOutputStream(output);
return outpstrm;
}
@Override
public PrintWriter getWriter()
{
if (pwriter == null) pwriter = new PrintWriter(getOutputStream(),true);
return pwriter;
}
这似乎是解决我的问题的原因。 由于此更改,包装程序运行良好。 顺便说说。 谁说您不能调用RequestDispatcher类的forward(req,res)方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.