今天图老师小编给大家精心推荐个JSP过滤器实现---论坛不文明语过滤功能教程,一起来看看过程究竟如何进行吧!喜欢还请点个赞哦~
【 tulaoshi.com - Web开发 】
说明:
以下代码运行环境: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-nameWeb/display-name
filter
filter-namefilter1/filter-name
filter-classcom.lgx.filtertext.dao.Filter1/filter-class
/filter
filter-mapping
filter-namefilter1/filter-name
url-pattern/*/url-pattern
/filter-mapping
servlet
descriptionAdded by JBuilder to compile JSPs with debug info/description
servlet-namedebugjsp/servlet-name
servlet-classorg.apache.jasper.servlet.JspServlet/servlet-class
init-param
param-nameclassdebuginfo/param-name
param-valuetrue/param-value
/init-param
load-on-startup3/load-on-startup
/servlet
servlet-mapping
servlet-namedebugjsp/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-nameWeb/display-name
filter
filter-namefilter1/filter-name
filter-classcom.lgx.filtertext.dao.Filter1/filter-class
/filter
filter-mapping
filter-namefilter1/filter-name
url-pattern/*/url-pattern
/filter-mapping
/web-app
来源:http://www.tulaoshi.com/n/20160219/1609114.html
看过《JSP过滤器实现---论坛不文明语过滤功能》的人还看了以下文章 更多>>