HttpServletRequestWrapper 类&过滤指定文字
HttpServletWrapper 和 HttpServletResponseWrapper
1). Servlet API 中提供了一个 HttpServletRequestWrapper 类来包装原始的 request 对象,
HttpServletRequestWrapper 类实现了 HttpServletRequest 接口中的所有方法,
这些方法的内部实现都是仅仅调用了一下所包装的的 request 对象的对应方法
//包装类实现 ServletRequest 接口.
public class ServletRequestWrapper implements ServletRequest { //被包装的那个 ServletRequest 对象
private ServletRequest request; //构造器传入 ServletRequest 实现类对象
public ServletRequestWrapper(ServletRequest request) {
if (request == null) {
throw new IllegalArgumentException("Request cannot be null");
}
this.request = request;
} //具体实现 ServletRequest 的方法: 调用被包装的那个成员变量的方法实现。
public Object getAttribute(String name) {
return this.request.getAttribute(name);
} public Enumeration getAttributeNames() {
return this.request.getAttributeNames();
} //...
}
相类似 Servlet API 也提供了一个 HttpServletResponseWrapper 类来包装原始的 response 对象
2). 作用: 用于对 HttpServletRequest 或 HttpServletResponse 的某一个方法进行修改或增强.
3). 使用: 在 Filter 中, 利用 MyHttpServletRequest 替换传入的 HttpServletRequest
HttpServletRequest req = new MyHttpServletRequest(request);
chain.doFilter(req, response);
此时到达目标 Servlet 或 JSP 的 HttpServletRequest 实际上是 MyHttpServletRequest
应用5:过滤不雅文字
content.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="bbs.jsp" method="post">
content: <textarea rows="5" cols="21" name="content"></textarea>
<input type="submit" value="Submit"/> </form>
</body>
</html>
bbs.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
content: ${param.content }
<br><br>
method: <%= request.getMethod() %>
<br><br>
<%= request %>
</body>
</html>
ContentFilter.java
package com.aff.javaweb; import java.io.IOException; import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @WebFilter("/bbs.jsp")
public class ContentFilter extends HttpFilter { @Override
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws IOException, ServletException {
//1. 获取请求参数的值
String content = request.getParameter("content");
HttpServletRequest req = new MyHttpServletRequest(request);
//2. 把其中fuck shit等字符串替换为***
if (content.contains(" fuck ")) { // 装饰目前的 HttpServletRequest 对象: 装饰其 getParameter 方法,而其他方法还和其实现相同.
// 创建一个类, 该类实现 HttpServletRequest 接口, 把当前 doFilter 中的 request 传入到该类中,
// 作为其成员变量, 使用该成员变量去实现接口的全部方法.
}
// 3.转到目标页面
chain.doFilter(req, response); }
}
MyHttpServletRequest.java
package com.aff.javaweb;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
//用于对 HttpServletRequest 或 HttpServletResponse 的某一个方法进行修改或增强.
public class MyHttpServletRequest extends HttpServletRequestWrapper{ public MyHttpServletRequest(HttpServletRequest request) {
super(request);
} @Override
public String getParameter(String name) {
String val = super.getParameter(name);
if(val != null && val.contains(" fuck ")){
val = val.replace("fuck", "****");
}
return val;
}
}
HttpServletRequestWrapper 类&过滤指定文字的更多相关文章
- C++ 不能在类体外指定关键字static
C++ static 函数的问题 近日读 C++ primer 中static 一章 , 有这么一句话, “静态成员函数的声明除了在类体中的函数声明前加上关键字static 以及不能声明为const ...
- Fiddler过滤指定域名
Fiddler过滤指定域名的方法一 切换到fiddler右侧窗口的Filters选项卡,勾选顶部的“Use Filters”,找到Hosts区域,设置以下三个选项: 1.第一项有三个选项,不做更改: ...
- 利用RandomAccessFile类在指定文件指定位置插入内容
package File; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...
- charles 过滤指定域名
本文参考:charles 过滤指定域名 当使用"序列视图"的时候 请求多了有些时候会看不过来,Charles 提供了一个简单的 Filter 功能,可以输入关键字来快速筛选出 UR ...
- 吴裕雄--天生自然python学习笔记:python文档操作自动查找替换 Word 文件中的指定文字
Win32com 组件提供了自动替换 Word 文件中指定文字 的功能 .在使用“查找” 功能替换文字之前,可先清除源文字及目标文字的格式,以免影响替换效果,语法为 : 替换 Word 文件特定文字的 ...
- java 面向对象(四十二):反射(六)反射应用三:调用运行时类的指定结构
调用指定的属性: @Test public void testField1() throws Exception { Class clazz = Person.class; //创建运行时类的对象 P ...
- 使用反射类、Class类获取指定的构造器并实例化对象
package com.test; public class Car { private String brand; private String color; private int maxSpee ...
- SpringMVC(二):RequestMapping修饰类、指定请求方式、请求参数或请求头、支持Ant路径
@RequestMapping用来映射请求:RequestMapping可以修饰方法外,还可以修饰类 1)SpringMVC使用@RequestMapping注解为控制指定可以处理哪些URL请求: 2 ...
- 在压缩话单中过滤指定IP的一个小脚本
工作需要,需要过滤出含有指定的IP段的话单,编写的脚本名字叫 filter.sh #!/bin/bash TARGET_PATH=/data/flume/flume_exec_log/Dst_for_ ...
随机推荐
- checked 完整版全选,单选,反选
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel= ...
- 支付宝小程序serverless---插入数据后获取数据的主键_id(mongodb)
支付宝小程序serverless---插入数据后获取数据的主键_id(mongodb) 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除, ...
- 在web项目中使用shiro(认证、授权)
一.在web项目中实现认证 第一步,在web项目中导入shiro依赖的包 第二步,在web.xml中声明shiro拦截权限的过滤器 <filter> <filter-name> ...
- shell命令之巧用cut
需求:取出日志中ip字段,并进行统计排序 .一般用用awk命令 假如ip地址为第一个字段 那么 awk ‘{print $1}’ 文件名 |sort |uniq -c|sort-nr 那如果不是第一个 ...
- Maxim实时时钟芯片设计指南5413-二进制编码十进制(BCD)格式实时时钟中的状态机逻辑
网上DS12C887的文章涉及到时间的存储格式使用的都是二进制代码,究竟使用BCD码该如何操作?正好美信官网上有一篇文章.美信官网不稳定,先贴到这里,有时间再翻译. 原文链接 State Machin ...
- 解决anaconda与pycharm冲突导致import无法使用
解决annacode与python冲突: 一.File->Setting-> 点击Add-> 然后就完美解决 二.记得重启,检查创建的项目是右键python package-> ...
- js Date对象日期格式化
dateFormat(d, fmt) { var o = { 'M+': d.getMonth() + 1, 'd+': d.getDate(), 'h+': d.getHours(), 'm+': ...
- linux-Curl error (37): Couldn't read a file:// file for file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-x86_64 [Couldn't open file /e tc/pki/rpm-gpg/RPM-GPG-KEY-fedora-x86_64]
在安装Python3-pip 的时候遇到 [root@localhost rpm-gpg]# yum install python3-pipFedora 31 - x86_64 - Updates - ...
- SpringBoot 整合SpringBatch实际项目改造
SpringBoot整合SpringBatch项目,已将代码开源至github,访问地址:https://github.com/cmlbeliever/SpringBatch 欢迎star or fo ...
- 基于Memcached的Nginx服务器集群session共享
原料:jdk1.8,tomcat7,nginx1.16,memcached-1.2.6,Mem-Tomcat需要的jar包,基于windows7.所有的点击以下链接可下载 链接:https://pan ...