思考怎么灵活过滤禁言关键字
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>SafeFilter</servlet-name>
<servlet-class>com.xing.filter.SafeFilter</servlet-class>
</servlet>
<!-- 重新部署Myfilter -->
<filter>
<filter-name>Myfilter</filter-name>
<filter-class>com.xing.filter.Myfilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter>
<filter-name>SafeFilter</filter-name>
<filter-class>com.xing.filter.SafeFilter</filter-class>
<init-param>
<param-name>keywords</param-name>
<param-value>mmp;shabi;妈卖批;傻逼;垃圾;辣鸡;我操;wocao;laji;shit;wori;caonima</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Myfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>SafeFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="userForm" type="com.xing.struts.form.UserForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="userForm"
input="/register.jsp"
name="userForm"
path="/reg"
scope="request"
type="com.xing.struts.action.RegAction"
validate="false">
<forward name="ok" path="/ok.jsp" />
<forward name="err" path="/register.jsp" />
<forward name="userErr" path="/info.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.xing.struts.ApplicationResources" />
</struts-config>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<jsp:forward page="/register.jsp"></jsp:forward>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'register.jsp' starting page</title>
</head>
<body>
<html:form action="/reg.do" method="post">
用户账号<html:text property="userid"></html:text><br/>
用户姓名<html:text property="username"></html:text><br/>
电子邮件<html:text property="email"></html:text><br/>
<html:submit value="注册"></html:submit>
<html:cancel value="重新填写"></html:cancel>
</html:form>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'ok.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
Register OK!<br>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'info.jsp' starting page</title>
</head>
<body>
用户名设置有问题 <%=request.getAttribute("err2") %><br/>
<a href="/strutstag/register.jsp">返回重新注册</a>
</body>
</html>
package com.xing.filter;
import java.io.IOException;
import javax.servlet.http.HttpServlet;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class Myfilter extends HttpServlet implements Filter {
private String encoding;
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
arg0.setCharacterEncoding(encoding);
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
encoding=arg0.getInitParameter("encoding");
}
}
package com.xing.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
public class SafeFilter extends HttpServlet implements Filter {
private String keywords[];
public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
String name=arg0.getParameter("username");
System.out.println("管理安全的过滤器接收到的名字:"+name);
if(name!=null){
//判断
for(String key:keywords){
if(key.equals(name)){
arg0.setAttribute("err2", "第二个过滤器拦截,用户名不能设置为<font color='red'>"+name+"</font>");
arg0.getRequestDispatcher("/info.jsp").forward(arg0, arg1);
return;
}
}
}
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
//得到配置的过滤器中的关键字数组
keywords=arg0.getInitParameter("keywords").split(";");
}
}
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.xing.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.xing.struts.form.UserForm;
/**
* MyEclipse Struts
* Creation date: 02-22-2020
*
* XDoclet definition:
* @struts.action path="/login" name="userForm" scope="request"
*/
public class RegAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;// TODO Auto-generated method stub
System.out.println("userId: "+userForm.getUserid()+" "+userForm.getUsername()+" "+userForm.getEmail());
if(true){
return mapping.findForward("ok");
}else return mapping.findForward("err");
}
}
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.xing.struts.form;
import org.apache.struts.action.ActionForm;
/**
* MyEclipse Struts
* Creation date: 02-22-2020
*
* XDoclet definition:
* @struts.form name="userForm"
*/
public class UserForm extends ActionForm {
private String userid;
private String username;
private String email;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}