添加时间:[2008-4-28 9:19:42]
过滤器
</title>
</head>
<bodybgcolor="#ffffff">
<h1>
准备过滤
</h1>
<h1>${pageScope.user}</h1>
<formmethod="post"action="Example1.jsp">
<br>
<inputtype="text"name="user"/>
<br>
<inputtype="submit"name="Submit"value="Submit">
<inputtype="reset"value="Reset">
</form>
</body>
</html>
文字
<%@pagecontentType="text/html;charset=GBK"%>
<%@tagliburi="
<%
Objectstr=request.getAttribute("user");
pageContext.setAttribute("user",str);
%>
<html>
<head>
<title>
过滤器
</title>
</head>
<bodybgcolor="#ffffff">
<h1>
准备过滤
packagecom.lgx.filtertext.dao;
importjavax.servlet.*;
importjavax.servlet.http.*;
importjava.io.*;
importjava.util.*;
publicclassFilter1extendsHttpServletimplementsFilter{
privateFilterConfigfilterConfig;
//Handlethepassed-inFilterConfig
publicvoidinit(FilterConfigfilterConfig)throwsServletException{
this.filterConfig=filterConfig;
}
//Processtherequest/responsepair
publicvoiddoFilter(ServletRequestrequest,ServletResponseresponse,
FilterChainfilterChain){
String[]strUser={"江泽民同志","胡锦涛同志","奶奶的","我拷","他XX的"};
Stringstr="";
try{
request.setCharacterEncoding("GBK");
HttpServletRequesthsrq=(HttpServletRequest)request;
Stringuser=hsrq.getParameter("user");
if(user==null||user.equals("")){
}else{
StringBuffersb=newStringBuffer(user);
for(inti=0;i<strUser.length;i++){
str=strUser[i];
if(sb.indexOf(str)==-1){
hsrq.setAttribute("user",sb.toString());
}else{
sb.replace(sb.indexOf(str),sb.indexOf(str)+3,"xxx");
hsrq.setAttribute("user",sb.toString());
}
}
}
filterChain.doFilter(request,response);
}catch(ServletExceptionsx){
filterConfig.getServletContext().log(sx.getMessage());
}catch(IOExceptioniox){
filterConfig.getServletContext().log(iox.getMessage());
}
}
//Cleanupresources
publicvoiddestroy(){
}
}
注:此“XML”代码是在运行时出现的,
注:这一处代码是配置过滤器的,不过JBuilder2005里面Tomcat容器已自动配置