gpt4 book ai didi

java - Servlet 过滤器和响应包装器

转载 作者:行者123 更新时间:2023-12-01 13:17:13 24 4
gpt4 key购买 nike

试图搞乱我尝试过的响应内容 this filter tutorial

所以我将我的类定义为

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(..)时,通常是一个servlet,有时会分派(dispatch)到jsp页面。尝试了 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>

最佳答案

解决问题的方法是创建 pwriter 和 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)方法?

关于java - Servlet 过滤器和响应包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22368689/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com