过虑器应用之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 ...
随机推荐
- java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing,
缺少一个java包,然后我在这个网址找到了http://central.maven.org/maven2/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1. ...
- shell中$(( ))、$( )与${ }的区别
转载自:http://blog.sina.com.cn/s/blog_4da051a60102uwda.html 命令替换 在bash中,$( )与` `(反引号)都是用来作命令替换的. 命令替换与变 ...
- 如何使用masonry设计复合型cell[转]
前言 其实早在@sunnyxx同学发布UIView-FDCollapsibleConstraints的时候 我就说要写一下怎么用代码来稍微麻烦的实现复用的问题 但是一直各种没时间(主要是我的办法太复杂 ...
- 第二届普适计算和信号处理及应用国际会议论文2016年 The 2nd Conference on Pervasive Computing, Signal Processing and Applications(PCSPA, 2016)
A New Method for Mutual Coupling Correction of Array Output Signal 一种阵列输出信号互耦校正的新方法 Research of Robu ...
- (KMP 字符串处理)Substrings -- hdu -- 1238
http://acm.hdu.edu.cn/showproblem.php?pid=1238 Substrings Time Limit:1000MS Memory Limit:32768KB ...
- POJ1556 最短路 + 线段相交问题
POJ1556 题目大意:比较明显的题目,在一个房间中有几堵墙,直着走,问你从(0,5)到(10,5)的最短路是多少 求最短路问题,唯一变化的就是边的获取,需要我们获取边,这就需要判断我们想要走的这条 ...
- oracle 游标实现多重循环
declare -- Local variables here i integer; cursor c_province is select ds.swjg_dm from dm_swjg ds wh ...
- LINUX中关于SIGNAL的定义
/* Signals. */ #define SIGHUP 1 /* Hangup (POSIX). */ #define SIGINT 2 /* Interrupt (ANSI). */ #defi ...
- SQL笔记---分页
随用随想,随用随记. 通过实际应用掌握SQL语句. 一. SQL分页 1. 第一种方法:利用ID大于多少进行筛选 SELECT TOP 20 *FROM dbo.WMS_Stock ...
- 盘古分词+一元/二元分词Lucene
本文参考自:https://blog.csdn.net/mss359681091/article/details/52078147 http://www.cnblogs.com/top5/archiv ...