Servlet封装类
Servlet 提供了四个封装类:
public class ServletRequestWrapper extends java.lang.Object implements ServletRequest //
public class ServletResponseWrapper extends java.lang.Object implements ServletResponse //
public class HttpServletRequestWrapper extends ServletRequestWrapper implements HttpServletRequest //
public class HttpServletResponseWrapper extends ServletResponseWrapper implements HttpServletResponse //
直接扩展这些封装类就可以修饰相应的的 request 和 response 对象了。
package app10a.filter; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set; 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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper; @WebFilter(filterName = "AutoCorrectFilter", urlPatterns = { "/*" })
public class AutoCorrectFilter implements Filter { @Override
public void destroy() {
} @Override
public void init(FilterConfig filterConfig) throws ServletException {
} @Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
AutoCorrectHttpServletRequestWrapper wrapper = new AutoCorrectHttpServletRequestWrapper(httpServletRequest);
filterChain.doFilter(wrapper, response);
} class AutoCorrectHttpServletRequestWrapper extends HttpServletRequestWrapper{ // 内部类
private HttpServletRequest httpServletRequest;
public AutoCorrectHttpServletRequestWrapper(HttpServletRequest httpServletRequest) {
super(httpServletRequest);
this.httpServletRequest = httpServletRequest;
}
@Override
public String getParameter(String name) {
return autoCorrect(httpServletRequest.getParameter(name));
}
@Override
public String[] getParameterValues(String name) {
return autoCorrect(httpServletRequest.getParameterValues(name));
}
@Override
public Map<String, String[]> getParameterMap() {
final Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
Map<String, String[]> newMap = new Map<String, String[]>(){ @Override
public void clear() {
parameterMap.clear();
} @Override
public boolean containsKey(Object key) {
return parameterMap.containsKey(key);
} @Override
public boolean containsValue(Object value) {
return parameterMap.containsValue(value);
} @Override
public Set<Entry<String, String[]>> entrySet() {
return autoCorrect(parameterMap.entrySet());
} @Override
public String[] get(Object key) {
return autoCorrect(parameterMap.get(key));
} @Override
public boolean isEmpty() {
return parameterMap.isEmpty();
} @Override
public Set<String> keySet() {
return parameterMap.keySet();
} @Override
public String[] put(String key, String[] value) {
return parameterMap.put(key, value);
} @Override
public void putAll(Map<? extends String, ? extends String[]> map) {
parameterMap.putAll(map);
} @Override
public String[] remove(Object key) {
return parameterMap.remove(key);
} @Override
public int size() {
return parameterMap.size();
} @Override
public Collection<String[]> values() {
return autoCorrect(parameterMap.values());
} };
return newMap;
}
}
private String autoCorrect(String value) {
if (value == null) {
return null;
}
value = value.trim();
int length = value.length();
StringBuilder temp = new StringBuilder();
boolean lastCharWasSpace = false;
for (int i = 0; i < length; i++) {
char c = value.charAt(i);
if (c == ' ') {
if (!lastCharWasSpace) {
temp.append(c);
}
lastCharWasSpace = true;
} else {
temp.append(c);
lastCharWasSpace = false;
}
}
return temp.toString();
}
private String[] autoCorrect(String[] values) {
if (values != null) {
int length = values.length;
for (int i = 0; i < length; i++) {
values[i] = autoCorrect(values[i]);
}
return values;
}
return null;
}
private Collection<String[]> autoCorrect(Collection<String[]> valueCollection){
Collection<String[]> newCollection = new ArrayList<String[]>();
for (String[] values : newCollection) {
newCollection.add(autoCorrect(values));
}
return newCollection;
}
private Set<Map.Entry<String, String[]>> autoCorrect(Set<Map.Entry<String, String[]>> entrySet){
Set<Map.Entry<String, String[]>> newSet = new HashSet<Map.Entry<String, String[]>>();
for (final Map.Entry<String, String[]> entry : entrySet) {
Map.Entry<String, String[]> newEntry = new Map.Entry<String, String[]>(){ @Override
public String getKey() {
return entry.getKey();
} @Override
public String[] getValue() {
return autoCorrect(entry.getValue());
} @Override
public String[] setValue(String[] value) {
return entry.setValue(value);
}
};
newSet.add(newEntry);
}
return newSet;
}
}
<!-- test1.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User Form</title>
</head>
<body>
<form action="test2.jsp" method="post">
<table>
<tr>
<td>Name: </td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Address: </td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</body>
</html>
<!-- test2.jsp -->
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Form values</title>
</head>
<body>
<table>
<tr>
<td>Name: </td>
<td>${param.name } (length: ${fn:length(param.name) })</td>
</tr>
<tr>
<td>Address: </td>
<td>${param.address } (length:${fn:length(param.address) })</td>
</tr>
</table>
</body>
</html>
Servlet封装类的更多相关文章
- jsp 修饰 Request 及Response
Servlet API包含4个可修饰的类,用于改变Servlet Request以及Servlet Response.这种修饰允许修改 ServletRequest以及ServletResponse或 ...
- 最简单的jsp+servlet的增删改查代码
package ceet.ac.cn.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.s ...
- Servlet页面注册用户的小程序(一)
本实例实现用userreg.jsp页面中的表单提交注册请求,把注册信息提交给regservlet写入数据库并且查询新用户显示出来. 一.准备工作. 1.jdbc数据驱动开发包mysql-connect ...
- Servlet页面登录的数据库验证程序(一)
一.基本思想是MVC模式,一个登录页面login.jsp,一个服务器处理程序Servlet.java,一个MySql数据库userinfo. 另外还有相关的数据封装类User和数据库连接类GetDat ...
- java web分享ppt大纲 -- servlet包
概述 servlet相关代码所在包: javax.servlet javax.servlet.http 常用的功能模块 Servlet,filter,listener 常用的相关类: servlet ...
- JavaWeb基础-servlet
Servlet简介 Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向浏览器输出数据),需要完成以下2个步骤: 1.编写一个Java类,实现s ...
- jsp+servlet+jdbc实现对数据库的增删改查
一.JSP和Servlet的简单介绍: 1.Servlet和JSP简介: Java开发Web应用程序时用到的技术主要有两种,即Servlet和JSP,Servlet是在服务器端执行的Java程序,一个 ...
- servlet文件下载
创建web工程servlet,新建DownloadServlet.java package com.xmyself.servlet; import java.io.File; import java. ...
- java中servlet的各种路径
1. web.xml中<url-pattern>路径,(叫它Servlet路径!) > 要么以“*”开关,要么为“/”开头 2. 转发和包含路径 > *****以“/”开头:相 ...
随机推荐
- RGB图片取大于阈值部分
#include<opencv2\opencv.hpp> #include <iostream> using namespace cv; using namespace std ...
- ABAP IMPORT&EXPORT的用法
1含有事务码 1.1 不注入参数,直接调用 CALL TRANSACTION 'SUIM' AND SKIP FIRST SCREEN. 1.2 注入参数, SET PARAMETER ID: '屏幕 ...
- SpringBoot SpringSession redis SESSION
号称无缝整合httpsession 共享, 但注意如果存在第三方框架,例如SESSION并发控制,这个是需要自己重写session名单的. 关于redis session 共享 的session并发控 ...
- pinyin4j 中文转拼音
- STL-stack和顺序栈实现括号匹配
2018-11-11-14:28:31 1.顺序栈 下面是我用数组实现的顺序栈,包含的函数有出入栈,查看栈顶元素,栈的大小,栈是否空等函数,当栈空间不够用时,对应的数组会自动增长. /******** ...
- Unity2017五子棋大战_人机_双人_UNET联网
五子棋大战源码工程基于Unity2017.2进行开发,分为人机.双人.UNET网络三种对战方式,配有案例讲解视频, 其中人机五子棋AI有三种开发难度,欢迎有兴趣的同学加入学习! . 目录 000-展示 ...
- Unity4.6证书激活问题
第一次运行的激活问题 安装好Unity4.6之后首次启动会自动连网,但是会出现error loading page错误,SSL peer certificate or SSH remote key w ...
- 微信小程序——过滤器的模拟
>> 编写wxs文件——filter.wxs //1. 价格格式化 function getPriceFormat(value) { return parseFloat(isNaN(val ...
- js 定时执行
代码: 格式例子: setInterval(方法名,1000*60); setInterval("方法名()",1000*60); setInterval(function () ...
- [leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...