点击这里给我发消息 点击这里给我发消息

JSP过滤器实现---论坛不文明语过滤功能

添加时间:2010-1-5
    相关阅读: JSP

说明:
以下代码运行环境:jdk1.4,工具JBuilder2005,Servlet2.4,JSP2.0
红色标记为关键代码.由于工程太大,我在这里只放有XML文件、JSP文件、过滤器
具体内容用论坛回复。 顺序为:JSP文件---过滤器---XML文件.

 <%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
Object str= request.getAttribute("user");
pageContext.setAttribute("user",str);
%>
<html>
<head>
<title>


过滤器

 </title>
</head>
<body bgcolor="#ffffff">
<h1>


准备过滤

 </h1>
<h1>${pageScope.user}</h1>
<form method="post" action="Example1.jsp">
<br>
 <input type="text" name="user" />
 <br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" value="Reset">
</form>
</body>
</html>


文字

 <%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
Object str= request.getAttribute("user");
pageContext.setAttribute("user",str);
%>
<html>
<head>
<title>
过滤器
</title>
</head>
<body bgcolor="#ffffff">
<h1>
准备过滤
package com.lgx.filtertext.dao;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Filter1 extends HttpServlet implements Filter {
   private FilterConfig filterConfig;
   //Handle the passed-in FilterConfig
   public void init(FilterConfig filterConfig) throws ServletException {
       this.filterConfig = filterConfig;
   }

   //Process the request/response pair
   public void doFilter(ServletRequest request, ServletResponse response,
                        FilterChain filterChain) {
       String[] strUser = {"江泽民同志", "胡锦涛同志", "奶奶的", "我拷", "他XX的"};
       String str = "";

       try {
           request.setCharacterEncoding("GBK");
           HttpServletRequest hsrq = (HttpServletRequest) request;
           String user = hsrq.getParameter("user");

           if (user == null || user.equals("")) {

           } else {
               StringBuffer sb = new StringBuffer(user);
               for (int i = 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 (ServletException sx) {
           filterConfig.getServletContext().log(sx.getMessage());
       } catch (IOException iox) {
           filterConfig.getServletContext().log(iox.getMessage());
       }
   }

   //Clean up resources
   public void destroy() {
   }
}
 

注:此“XML”代码是在运行时出现的,

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
 <display-name>Web</display-name>
 <filter>
   <filter-name>filter1</filter-name>
   <filter-class>com.lgx.filtertext.dao.Filter1</filter-class>
 </filter>
 <filter-mapping>
   <filter-name>filter1</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>
 <servlet>
   <description>Added by JBuilder to compile JSPs with debug info</description>
   <servlet-name>debugjsp</servlet-name>
   <servlet-class>org.apache.jasper.servlet.JspServlet</servlet-class>
   <init-param>
     <param-name>classdebuginfo</param-name>
     <param-value>true</param-value>
   </init-param>
   <load-on-startup>3</load-on-startup>
 </servlet>
 <servlet-mapping>
   <servlet-name>debugjsp</servlet-name>
   <url-pattern>*.jsp</url-pattern>
 </servlet-mapping>
</web-app>

注:这一处代码是配置过滤器的,不过JBuilder2005里面Tomcat容器已自动配置

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
 <display-name>Web</display-name>
 <filter>
   <filter-name>filter1</filter-name>
   <filter-class>com.lgx.filtertext.dao.Filter1</filter-class>
 </filter>
 <filter-mapping>
   <filter-name>filter1</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

本文作者:
咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册