filter 过滤出现 getOutputStream() has already been called for this response

funnyone 2011-04-16 10:16:11
number.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet"
href="/why/css/my.css"
type="text/css"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<div class="red"> set </div>
<c:set var="session" value="i am sseison" scope="session"></c:set>
<c:set var="application" value="i am application" scope="application"></c:set>
session${session}<br/>
context${applicationScope.application}
<c:import url="http://en.wikipedia.org/wiki/Google"></c:import>
</body>
</html>


web.xml

<filter-name>changeFilter</filter-name>
<filter-class>my.ChangeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>changeFilter</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping><!--changeFilter2 监视 all --><jsp-config>
<taglib>




ChangeFilter.java


package my;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

public class ChangeFilter implements Filter{
ResponseWrapper response;
String goal;
@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
response=new ResponseWrapper((HttpServletResponse )arg1);
arg2.doFilter(arg0, response );
change(arg0);
PrintWriter out=arg1.getWriter();
out.write(goal);
out.close();

}

@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub

}
private void change(ServletRequest arg0){
goal=(String)arg0.getServletContext().getInitParameter("newName");
if(goal!=null){
goal=response.toString().replaceAll("google", goal);
}else{
goal=response.toString();
}
}
}



运行后 出现

java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:636)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:205)
at my.ChangeFilter.doFilter(ChangeFilter.java:24)


而且没有将那个网页的 google替换掉

我怀疑是 <c:import url="http://en.wikipedia.org/wiki/Google"></c:import>

没有经过过滤

该如何修改是好
...全文
364 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
funnyone 2011-04-17
  • 打赏
  • 举报
回复
funnyone 2011-04-16
  • 打赏
  • 举报
回复
摆脱

二个问题一样吗
  • 打赏
  • 举报
回复
参考这个解决:


http://blog.csdn.net/ylz2007/archive/2008/08/19/2798520.aspx

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧