jsp中:
<form action="comment" method="post">
<fieldset style="width:250px;">
<legend>comment:</legend>
<textarea rows="5" cols="20" name="content"></textarea>
<input type="submit" value="confirm"/>
</fieldset>
</form>
提交之后,因为web.xml中时这样配置的:
<filter>
<filter-name>commentFilter</filter-name>
<filter-class>web.CommentFilter</filter-class>
<init-param>
<param-name>illegalStr</param-name>
<param-value>dog</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>commentFilter</filter-name>
<url-pattern>/comment</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>CommentServlet</servlet-name>
<servlet-class>web.CommentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CommentServlet</servlet-name>
<url-pattern>/comment</url-pattern>
</servlet-mapping>
所以要先经过web.CommentFilter过滤,把dog给过滤掉
web.CommentFilter是这么写的:
public class CommentFilter implements Filter{
private FilterConfig config;
/**
* 释放资源的方法,在servlet容器删除Filter对象之前执行。
*
*/
public void destroy() {
}
/**
* servlet容器在调用servlet之前,会先执行该
* 方法。servlet容器会将构造好的HttpSevletRequest,
* HttpServletResponse对象作为参数传递给该方法。
* FilterChain.doFilter(ServletRequest,ServletResponse)
* 其作用是调用后续的过滤器或者servlet。
*/
public void doFilter(ServletRequest arg0,
ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request =
(HttpServletRequest)arg0;
HttpServletResponse response =
(HttpServletResponse)arg1;
PrintWriter out = response.getWriter();
String content = request.getParameter("content");
String illegalStr = config.getInitParameter("illegalStr");
if(content.indexOf(illegalStr)!=-1){
String content2 = content.replaceAll(
illegalStr, "cat");
out.println("illegal comment!");
return;
}
out.println("<h1>hello</h1>");
out.close();
}
/**
* 初始化方法,servlet容器在创建好Filter实例之后,
* 会调用该方法,执行初始化操作(获得资源)。
* 可以通过FilterConfig对象(也是由servlet容器创建的)
* 获得初始化参数。
*/
public void init(FilterConfig arg0) throws ServletException {
config = arg0;
}
}
经过上面的过滤器,因为这段代码
String illegalStr = config.getInitParameter("illegalStr");
if(content.indexOf(illegalStr)!=-1){
String content2 = content.replaceAll(
illegalStr, "cat");
out.println("illegal comment!");
return;
}
所以不再进入接下来的servlet了,直接return出来,返回给客户一个限制字眼的页面
如果想要将限制文字替换成别的字符或者文字,那CommentFilter就要像下面这么写:
public class CommentFilter implements Filter{
private FilterConfig config;
/**
* 释放资源的方法,在servlet容器删除Filter对象之前执行。
*
*/
public void destroy() {
}
/**
* servlet容器在调用servlet之前,会先执行该
* 方法。servlet容器会将构造好的HttpSevletRequest,
* HttpServletResponse对象作为参数传递给该方法。
* FilterChain.doFilter(ServletRequest,ServletResponse)
* 其作用是调用后续的过滤器或者servlet。
*/
public void doFilter(ServletRequest arg0,
ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest request =
(HttpServletRequest)arg0;
HttpServletResponse response =
(HttpServletResponse)arg1;
PrintWriter out = response.getWriter();
String content = request.getParameter("content");
String illegalStr = config.getInitParameter("illegalStr");
arg2.doFilter(new MyRequest(request), response);
out.println("<h1>hello</h1>");
out.close();
}
/**
* 初始化方法,servlet容器在创建好Filter实例之后,
* 会调用该方法,执行初始化操作(获得资源)。
* 可以通过FilterConfig对象(也是由servlet容器创建的)
* 获得初始化参数。
*/
public void init(FilterConfig arg0) throws ServletException {
config = arg0;
}
}
doFilter方法中调用了另一个筛选器MyRequest:
public class MyRequest extends HttpServletRequestWrapper{
public MyRequest(HttpServletRequest arg0) {
super(arg0);
}
@Override
public String getParameter(String arg0) {
String info = super.getParameter(arg0);
String info2 = info.replaceAll("dog", "cat");
return info2;
}
}
筛选器中将dog替换成cat,接着转入到servlet中,serlet中的相关service方法运行完之后又回到了过滤器中的doFilter方法中,继续执行完剩下的代码。