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>

本文作者:



相关阅读:
IE和火狐中模仿Click事件及提交到新窗口总结(asp.net)
CSS教程:认真学习haslayout
如何有效利用系统中自带的记事本功能
关于 ASP 应用程序
javascript实现网页跳转的方法
vbs中实现启动两个应用程序,一直等到其中一个程序结束,然后关闭另一个?
JavaScript blog式日历控件新算法
php抓即时股票信息
SqlServer 序号列的实现方法
Optimizer与Debugger兼容性问题的解决方法
CSS网页布局:div水平居中的各种方法
mysql 10w级别的mysql数据插入
PHP 操作文件的一些FAQ总结
ASP.NET下母版页和内容页中的事件发生顺序整理
快速导航

Copyright © 2016 phpStudy |