package cn.com.adtis.cd.saas.web.util;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
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.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.springframework.web.util.WebUtils;
/**
* @author cd_lisc
* @describe 参数去除空格并写回
* @date 2018-06-20
*/
public class TrimParametersFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest request1 = (HttpServletRequest)request;
/**
* 页面跳转的参数不作处理,主要是 jsp:param会被HttpServletRequestWrapper 过滤
*/
if(WebUtils.isIncludeRequest(request1)||isForwarded(request1)){
chain.doFilter(request1, response);
}else{
ModifyParametersWrapper mParametersWrapper = new ModifyParametersWrapper(request1);
chain.doFilter(mParametersWrapper, response);
}
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
public static boolean isForwarded(HttpServletRequest request) {
return !WebUtils.isIncludeRequest(request) &&
request.getAttribute("javax.servlet.forward.context_path") != null;
}
/**
* 继承HttpServletRequestWrapper,创建装饰类,以达到修改HttpServletRequest参数的目的
*/
private class ModifyParametersWrapper extends HttpServletRequestWrapper {
private HttpServletRequest request;
public ModifyParametersWrapper(HttpServletRequest request) {
super(request);
this.request = request;
}
// 重写几个HttpServletRequestWrapper中的方法
/**
* 获取所有参数名
*
* @return 返回所有参数名
*/
@Override
public Enumeration<String> getParameterNames() {
return super.getParameterNames();
}
/**
* 获取指定参数名的值,如果有重复的参数名,则返回第一个的值 接收一般变量 ,如text类型
*
* @param name
* 指定参数名
* @return 指定参数名的值
*/
@Override
public String getParameter(String name) {
String results = super.getParameter(name);
if (results == null){
return modify(request.getParameter(name));
}else {
return modify(results);
}
}
/**
* 获取指定参数名的所有值的数组,如:checkbox的所有数据
* 接收数组变量 ,如checkobx类型
*/
@Override
public String[] getParameterValues(String name) {
String[] results = super.getParameterValues(name);
if (results == null || results.length <= 0)
return null;
else {
int length = results.length;
for (int i = 0; i < length; i++) {
results[i] = modify(results[i]);
}
return results;
}
}
/**
* 自定义的一个简单修改原参数的方法,即:去除前后空格
*
* @param string
* 原参数值
* @return 修改之后的值
*/
private String modify(String string) {
if(string != null){
return string.trim();
}
return string;
}
}
}
李2牛
- 粉丝: 0
- 资源: 5
最新资源
- (源码)基于Spring Boot和WebSocket的毕业设计选题系统.zip
- (源码)基于C++的机器人与船舶管理系统.zip
- (源码)基于WPF和Entity Framework Core的智能货架管理系统.zip
- SAP Note 532932 FAQ Valuation logic with active material ledger
- (源码)基于Spring Boot和Redis的秒杀系统.zip
- (源码)基于C#的计算器系统.zip
- (源码)基于ESP32和ThingSpeak的牛舍环境监测系统.zip
- 西南科技大学数据库实验三
- Web开发全栈入门与进阶指南:从前端到后端
- TSP问题的概述及其在多领域的应用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈