过虑器应用之1-设置request编码
一:设置Post编码
post请求接收中文,需要在Servlet里写上 request.setCharacterEncoding("UTF-8"); 否则默认以iso-8859-1编码,中文显示乱码:webèé¢.doc,在每个Servlet里都写这句话,有点麻烦。
通过过滤器,统一设置post编码:
写一个过虑器,对所有url全部过虑,/*.在doFilter方法中,设置request的编码为utf-8。
一般情况下,这个过虑器永远是第一个要执行的过虑器。
最好是通过配置设置编码。这样修改方便<filter><init-param>…
第一步:实现Filter接口,在doFIlter中接收初始化参数,设置编码
java代码
public class CharFilter implements Filter {
//声明编码的成员变量
private String encoding;
public void init(FilterConfig config) throws ServletException {
encoding = config.getInitParameter("bm");
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
//设置响应编码,否则,默认为iso-8859-1,jsp展示中文乱码
response.setContentType("text/html;charset=" + encoding);
//放行,必须要放行。
chain.doFilter(request, response);
}
public void destroy() {
}
}
第二步:将上面的类配置到web.xml
<!-- 编码过滤器 -->
<filter>
<filter-name>char</filter-name>
<filter-class>com.lhy.filter.CharFilter</filter-class>
<init-param>
<!-- 设置编码 -->
<param-name>bm</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>char</filter-name>
<!-- 对所有url过滤 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
测试:request接收参数: web考题.doc ,中文正常显示。
二:Get设置编码
在CharFilter中对reuqest进行包装。
目的:修改增强getParameter方法,如果是get转码。
第一步:声明包装类:
/**
* 对get可以处理中文
* 声明包装类
* 在CharFilter中对reuqest进行包装。
* 目的:修改增强getParameter方法,如果是get转码。
*/
public class MyRequest extends HttpServletRequestWrapper { public MyRequest(HttpServletRequest request) {
super(request);
}
//增强getParamter
@Override
public String getParameter(String name) {
String val = super.getParameter(name);
if(super.getMethod().equals("GET")){
try {
val = new String(val.getBytes("ISO-8859-1"),super.getCharacterEncoding());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return val;
}
}
第二步:在doFilter方法中,声明包装类的实例
public class CharFilter implements Filter{
//声明编码的成员变量
private String encoding;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
encoding = filterConfig.getInitParameter("bm");
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
request.setCharacterEncoding(encoding);
//设置响应编码,否则,默认为iso-8859-1,jsp展示中文乱码
response.setContentType("text/html;charset=" + encoding);
// 判断是否需要包装
HttpServletRequest req = (HttpServletRequest)request;
if(req.getMethod().equals("GET")){
request = new MyRequest(req);//实例化包装类。
}
//放行,必须要放行。
chain.doFilter(request, response);
}
}
test:
<a href="CharServlet?pwd=阿斯达">get</a> req.getParameter("pwd");--阿斯达

过虑器应用之1-设置request编码的更多相关文章
- 过虑器 ThreadLocal 权限 监听器 观察者模式
数据的压缩 GzipOutputStream - > > ByteArrayOutputStream. 以下是在某个servlet中对指定的数据进行压缩 package cn.itcast ...
- 异步Servlet和异步过虑器
异步处理功能可以节约容器线程.此功能的作用是释放正在等待完成的线程,是该线程能够被另一请求所使用. 要编写支持异步处理的 Servlet 或者过虑器,需要设置 asyncSupported 属性为 t ...
- Tomcat 中文乱码 设置UTF-8编码 问题解决办法
在Java Web开发中,http请求带有中文字符的URI如果不处理容易出现乱码问题:这是因为Tomcat容器默认编码是iso-8859-1引起的,因此要避免出现乱码就要需要做相应的处理.解决办法如下 ...
- java—过虑器基础(47)
在web项目中就只有三大组件: Filter过虑器 监听器. Servlet 在web中过虑器就是一个类javax.servlet.Filter. 过虑器是用于在执行时,过虑用户的请求(request ...
- spring设置字符编码过滤器
一.在web.xml中的配置 <!-- characterEncodingFilter字符编码过滤器 --> <filter> <filter-name>chara ...
- 阶段5 3.微服务项目【学成在线】_day17 用户认证 Zuul_16-网关-过虑器
4.5 过虑器 Zuul的核心就是过虑器,通过过虑器实现请求过虑,身份校验等. 4.5.1 ZuulFilter 自定义过虑器需要继承 ZuulFilter,ZuulFilter是一个抽象类,需要覆盖 ...
- response ,request编码
request.setCharacterEncoding()是你设置获得数据的编码方式.response.setCharacterEncoding()是你响应时设置的编码.response.setCo ...
- Java基于Servlet过虑器
- Response ServletContext 中文乱码 Request 编码 请求行 共享数据 转发重定向
Day35 Response 1.1.1 ServletContext概念 u 项目的管理者(上下文对象),服务器启动时,会为每一个项目创建一个对应的ServletContext对象. 1.1.2 ...
随机推荐
- 手动安装jar到maven
mvn install:install-file -DgroupId=com.chinacloud.mir.common -DartifactId=one-aa-sdk -Dversion=1.0-S ...
- Codeforces805D. Minimum number of steps 2017-05-05 08:46 240人阅读 评论(0) 收藏
D. Minimum number of steps time limit per test 1 second memory limit per test 256 megabytes input st ...
- hdu 1284 钱币兑换
题目 我们用dp[n]表示用这些硬币组成n的方法总数.... 然后随着硬币种类的增加来更新dp[]的值,也就是最外面的一层循环for(i :1-->3)开始初始化的时候没有硬币,然后新来了面值为 ...
- follow me
IM InfoSphere Information Server for Data Integration Fundamentals Bootcamp 7月15日 5 北京 IM InfoSphere ...
- Mysql工作記錄之修改默認存儲引擎及重設root用戶密碼
1>修改默認存儲引擎方法 修改配置文件,然後重啟mysql服務: [root@CHW mysql]# cat /etc/my.cnf [my ...
- Unity3d ugui 实现image代码换图
核心脚本代码 Image IMGE = transform.Find("IMGE").GetComponent<Image>();Sprite sprite1 = Re ...
- ORM-Dapper快速学习
轻量级ORM框架——第一篇:Dapper快速学习 转载地址:http://www.cnblogs.com/huangxincheng/p/5828470.html 我们都知道ORM全称叫做Objec ...
- 【cocos2d-x 手游研发小技巧(6)聊天系统+字体高亮】
转载请注明出处:http://www.cnblogs.com/zisou/p/cocos2dxJQ-6.html 聊天系统在手机网游中是最常见的交互工具,大家在一起边玩游戏边聊天岂不乐哉: 废话不多了 ...
- Binary Search Tree-530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- vue 学前班001(基础概念)
1 学习目标 通过这一节,你会学会: 1.目前前端技术使用的趋势 2.什么是MVVM 3.Vue.js的两大核心 4.Vue.js的适用场景 诞生背景 近几年来,得益于手机设备的普及和性能的提升, ...