过滤器

本文介绍了如何使用过滤器来过滤搜索设置中的敏感词汇。当输入敏感词如'admin'时,搜索将失败并给出提示;而其他内容则能正常搜索并显示结果。文章提供了目录结构、效果图以及涉及的jsp、java和xml代码示例。
摘要由CSDN通过智能技术生成

使用过滤器过滤设置的相关敏感词汇

功能分析:

  1. 一个搜索框一个提交按钮
  2. 当输入相关敏感词汇时搜索失败(无法搜索,会给出提示)
  3. 搜索其他内容的时候会显示你搜索的内容(可以搜索,能够搜索成功)

效果图演示

搜索框和提交按钮
在这里插入图片描述
填入敏感词汇时即admin之后点击搜索(我设置的敏感词汇是admin在这里插入图片描述
搜索其他与敏感词汇不相关的内容时
在这里插入图片描述
看了展示,相信大家已经迫不及待的想要知道程序代码了。

先看目录结构

在这里插入图片描述

代码演示

  1. 搜索页面 即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>

  1. 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
		
	}
	
}

  1. 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>

  1. 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>

到此为止程序已经结束,赶快去试试吧。

了解更多关注我呦!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值