使用过滤器过滤设置的相关敏感词汇
功能分析:
- 一个搜索框一个提交按钮
- 当输入相关敏感词汇时搜索失败(无法搜索,会给出提示)
- 搜索其他内容的时候会显示你搜索的内容(可以搜索,能够搜索成功)
效果图演示
搜索框和提交按钮
填入敏感词汇时即admin之后点击搜索(我设置的敏感词汇是admin)
搜索其他与敏感词汇不相关的内容时
看了展示,相信大家已经迫不及待的想要知道程序代码了。
先看目录结构
代码演示
- 搜索页面 即login.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'login.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 style="text-align: center;">
<form action="show.jsp" method="post">
<input type="text" name="name">
<input type="submit" value="搜狗搜索">
</form>
</body>
</html>
- filter.java代码
package zsh.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 filter implements Filter {
private String word = "";
public void init(FilterConfig config) throws ServletException{
word = config.getInitParameter("word");
}
public void doFilter(ServletRequest request, ServletResponse reponse, FilterChain filterChain) throws IOException, ServletException{
//转换编码
request.setCharacterEncoding("utf-8");
reponse.setCharacterEncoding("utf-8");
reponse.setContentType("text/html;charset=utf-8");
//获取参数(内容)
String name = request.getParameter("name");
if(name != null && name.indexOf(word) != -1){
PrintWriter out = reponse.getWriter();
out.println("<font color='red'>根据相关法律法规,你所搜索的" + name +"内容不予显示!</font>");
}else{
filterChain.doFilter(request, reponse);
}
}
public void destroy() {
// TODO Auto-generated method stub
}
}
- web.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- filter用来定义一个过滤器 -->
<filter>
<filter-name>filter</filter-name>
<filter-class>zsh.servlet.filter</filter-class>
<init-param>
<param-name>word</param-name>
<param-value>admin</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>filter</filter-name>
<url-pattern>/show.jsp</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
- show.jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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 'show.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>
查询到的内容是 ${param.name }
</body>
</html>
到此为止程序已经结束,赶快去试试吧。
了解更多关注我呦!!!