关于过滤的问题,100分请教高手。

whodsow 2003-02-26 11:40:34
我想在所有http://localhost/study/下的网页后面加下我的版权申明,我使用了过滤:
以下是相关文件:
D:\Program Files\Apache Tomcat 4.0\webapps\study\WEB-INF\web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>

<filter>
<filter-name>VisAudit Filter</filter-name>
<filter-class>filters.VisAuditFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>VisAudit Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

D:\Program Files\Apache Tomcat 4.0\webapps\study\WEB-INF\classes\filters\VisAuditFilter.java

package filters;

import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import java.io.IOException;

public final class VisAuditFilter implements Filter
{
private FilterConfig filterConfig=null;

public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException
{
if(filterConfig==null)
{
return;
}

String clientAddr=request.getRemoteAddr();
String clientHost=request.getRemoteHost();
filterConfig.getServletContext().log("in VisAuditFilter");

VisAuditResponseWrapper myWrappedResp=new VisAuditResponseWrapper(response,request,clientAddr,clientHost);

chain.doFilter(request,myWrappedResp);

myWrappedResp.getOutputStream().close();

filterConfig.getServletContext().log("go out of VisAuditFilter");

}

public void init(FilterConfig filterConfig)
{
this.filterConfig=filterConfig;
}

public void destroy()
{

}

public String toString()
{
if(filterConfig==null)
{
return ("VisAuditFilter");
}
StringBuffer sb=new StringBuffer("VisAuditFilter(");
sb.append(filterConfig);
sb.append(")");
return sb.toString();
}
}

D:\Program Files\Apache Tomcat 4.0\webapps\study\WEB-INF\classes\filters\VisAuditResponseWrapper.java

package filters;

import javax.servlet.ServletResponse;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponseWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletRequest;
import java.io.PrintWriter;
import java.io.IOException;

public class VisAuditResponseWrapper extends HttpServletResponseWrapper
{
private PrintWriter tpWriter;
private VisAuditOutStream tpStream;

public VisAuditResponseWrapper(ServletResponse response,ServletRequest request,String inAddr,String inHost)throws IOException
{
super((HttpServletResponse)response);
String path=request.getRealPath("/");
tpStream=new VisAuditOutStream(response.getOutputStream(),inAddr,inHost,path);
tpWriter=new PrintWriter(tpStream);
}

public ServletOutputStream getOutputStream()throws IOException
{
return tpStream;
}

public PrintWriter getWriter()throws IOException
{
return tpWriter;
}
}

D:\Program Files\Apache Tomcat 4.0\webapps\study\WEB-INF\classes\filters\VisAuditOutStream.java


package filters;

import whodsow.io.ReplaceContentOutputStream;
import java.io.OutputStream;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;

public class VisAuditOutStream extends ReplaceContentOutputStream
{
String addr;
String host;
String path;

public VisAuditOutStream(OutputStream outStream,String inAddr,String inHost,String path)
{
super(outStream);
addr=inAddr;
host=inHost;
this.path=path;
}

public byte[] replaceContent(byte[] inByte)
{
String retVal="";
String firstPart="";
String tpString=new String(inByte);
String srchString=(new String(inByte)).toLowerCase();
String fileContent="";
String fileName="";
try
{
fileName=path+"copyright.txt";
FileReader fr=new FileReader(fileName);
BufferedReader br=new BufferedReader(fr);
File file=new File(fileName);
char []buffer=new char[(int)file.length()];
br.read(buffer);
fileContent=new String(buffer);
br.close();
fr.close();
}
catch(Exception ignore)
{
System.err.println("Read file "+fileName+" Error.");
System.err.println(ignore.getMessage());
}
int endHead=srchString.indexOf("</head>");
if(endHead!=-1)
{
firstPart=tpString.substring(0,endHead);
retVal=firstPart+"<Link href=\"css/style.css\" rel=\"stylesheet\" type=\"text/css\">"+tpString.substring(endHead);
}
tpString=retVal;
srchString=retVal.toLowerCase();
int endBody=srchString.indexOf("</body>");
if(endBody!=-1)
{
firstPart=tpString.substring(0,endBody);
retVal=firstPart+"<br><small><i>Big Brother is watching you."+"You have accessed our page from "+addr+" and on a machine called "+host+"</i></small></br>"+fileContent+tpString.substring(endBody);
}
else
{
retVal=tpString;
}
System.out.println(retVal);
System.out.flush();
return retVal.getBytes();
}
}

D:\Program Files\Apache Tomcat 4.0\classes\whodsow\io\ReplaceContentOutputStream.java

package whodsow.io;
import java.io.IOException;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import javax.servlet.ServletOutputStream;

public abstract class ReplaceContentOutputStream extends ServletOutputStream
{
private OutputStream intStream;
private ByteArrayOutputStream baStream;
private boolean closed=false;
private boolean transformOnCloseOnly=false;

public ReplaceContentOutputStream(OutputStream outStream)
{
intStream=outStream;
baStream=new ByteArrayOutputStream();
}

public void write(int i)throws IOException
{
baStream.write(i);
}

public void close()throws IOException
{
if(!closed)
{
processStream();
intStream.close();
closed=true;
}
}

public void flush()throws IOException
{
if(baStream.size()!=0)
{
if(!transformOnCloseOnly)
{
processStream();
baStream=new ByteArrayOutputStream();
}
}
}

public abstract byte[] replaceContent(byte[] inBytes)throws IOException;

public void processStream()throws IOException
{
intStream.write(replaceContent(baStream.toByteArray()));
intStream.flush();
}

public void setTransformOnCloseOnly()
{
transformOnCloseOnly=true;
}
}

访问localhost/study下的一个文件时,速度很慢很慢,然后进度条到一半时就不动了,我看浏览器所得的源文件如下:
<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Hello World</title>
<Link href="css/style.css" rel="stylesheet" type="text/css"></head>

<body>


后面就没了。
日志记录如下:
2003-02-25 09:33:38 in VisAuditFilter
2003-02-25 09:33:38 go out of VisAuditFilter
2003-02-25 09:33:39 in VisAuditFilter
2003-02-25 09:33:38 go out of VisAuditFilter
2003-02-25 09:33:39 in VisAuditFilter
2003-02-25 09:33:38 go out of VisAuditFilter
也就什么都没了,高手请指点怎么回事,以上所有代码均无语法方面的问题,可真接copy。
...全文
87 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
whodsow 2003-05-06
  • 打赏
  • 举报
回复
哈经过三个月的努力,我自己解决了。
public void processStream()throws IOException
{
byte[] Buffer=replaceContent(baStream.toByteArray());
response.setContentLength(Buffer.length);
intStream.write(Buffer);
intStream.flush();
}
freedomcsdn 2003-02-27
  • 打赏
  • 举报
回复
up
whodsow 2003-02-27
  • 打赏
  • 举报
回复
日志中有2003-02-25 09:33:38 go out of VisAuditFilter,说明执行完了Filter
jspxnet 2003-02-27
  • 打赏
  • 举报
回复
看看是执行到什么位置
funpig 2003-02-27
  • 打赏
  • 举报
回复
不懂!
UP!
whodsow 2003-02-27
  • 打赏
  • 举报
回复
晕。

81,090

社区成员

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

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