응답 내용으로 주위를 엉망으로 시도 나는서블릿 필터 및 응답 래퍼
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();
//===================================================================
}
필터가 괜찮은 것 같습니다. "test content"를 읽을 수는 있지만 wrapper.getData()
은 0 바이트를 반환합니다. 으로 전화를 걸면 보통 서블릿으로 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">
<%[email protected] file="publicheader.jsp" --%>
<div class="desktopwebpagecontainer">
main page.
</div>
<%[email protected] file="publicfooter.jsp" --%>
</body>
</html>
'wrapper.getData()'가 0 바이트를 반환한다는 것을 어떻게 알 수 있습니까? –
나는 왜 그것이 "보인다"고 말할 수 없다. 내가 말할 수있는 것은 그것이 널이 아니라는 것입니다. – mdev
'getData(). length'를 출력하십시오. –