【WEB小工具】EncodingFilter—设置全局编码
1.我们知道,如果是POST请求,我们需要调用request.setCharacterEncoding("utf-8")
方法来设计编码。
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gbk"); //只适用于post提交,不适用于get
response.setCharacterEncoding("gbk");
}
2.如果是GET请求,我们需要自己手动来处理编码问题。
3.如果我们使用了EncodingFilter,那么就处理了POST和GET请求的编码问题。
修改web.xml:
<!-- 处理全站请求编码。无论是GET还是POST,默认是UTS-8 -->
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>cn.itcast.filter.EncodingFilter</filter-class>
<init-param>
<param-name>charset</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
EncodingFilter() 类源代码:
package cn.itcast.filter; import java.io.IOException; 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; public class EncodingFilter implements Filter {
private String charset = "UTF-8";
@Override
public void destroy() {} @Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
if(req.getMethod().equalsIgnoreCase("GET")) {
if(!(req instanceof GetRequest)) {
req = new GetRequest(req, charset);//澶勭悊get璇锋眰缂栫爜
}
} else {
req.setCharacterEncoding(charset);//澶勭悊post璇锋眰缂栫爜
}
chain.doFilter(req, response);
} @Override
public void init(FilterConfig fConfig) throws ServletException {
String charset = fConfig.getInitParameter("charset");
if(charset != null && !charset.isEmpty()) {
this.charset = charset;
}
}
}
【WEB小工具】EncodingFilter—设置全局编码的更多相关文章
- java_eclipse_设置全局编码_utf-8_编译class指定_运行jar乱码解决_不依赖环境
简述: javac时指定 编码 UTF-8 [ javac -encoding UTF-8 Test.java],运行时 java 指定编码 UTF-8 这样就不会出现乱码问题[ javac ...
- Java工具类-设置字符编码
package common; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.Filter ...
- 在web.xml中设置全局编码
在web.xml中配置 <filter> <filter-name>characterFilter</filter-name> <filter-class&g ...
- eclipse设置全局编码为UTF-8的方法
1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到general->Workspace,右侧Text file encoding ...
- Ecplise设置全局编码为UTF-8
简介 Eclipse工作空间(workspace)的缺省字符编码是操作系统缺省的编码,简体中文操作系统 (Windows XP.Windows 2000简体中文)的缺省编码是GB18030,Windo ...
- 【WEB小工具】jQuery函数
jQuery-API帮助文档:Click here jQuery简介 jQuery是JavaScript框架,jQuery也是JavaScript代码.使用jQuery要比直接使用JavaScript ...
- 【WEB小工具】BaseServlet—一个Servlet处理多个请求
package cn.itcast.test.web.servlet; import java.io.IOException; import java.io.PrintWriter; import j ...
- marked插件在线实时解析markdown的web小工具
访问地址: https://mdrush.herokuapp.com/ github项目: https://github.com/qcer/MDRush 实现简介: 1.动态数据绑定 借助Vuejs, ...
- MyEclipse2018.9.0设置全局编码
1.windows->Preferences打开"首选项"对话框,左侧导航,导航到general->Workspace 右侧Text file encoding,选择O ...
随机推荐
- 关于struts2如何去掉默认的后缀(.action)
struts2是可以配置默认的后缀名的,如http://localhost:8080/test.action,这个是默认的,但是也可以通过配置去修改这个.action为别的. 这里是通过一个常量配置改 ...
- uva 10912
dp 记忆化搜索 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...
- Unity3D脚本中文系列教程(七)
http://dong2008hong.blog.163.com/blog/static/4696882720140311445677/?suggestedreading&wumii Unit ...
- JSP图片上传 公共工具类
需要jsmartcom_zh_CN.jar支持. 下载地址: http://files.cnblogs.com/simpledev/jsmartcom_zh_CN.rar <%@page imp ...
- 【C++基础】 类中static private public protected
静态成员在一个类的所有实例间共享数据 “类属性”,是描述类的所有对象共同特征的一个数据项,对所有对象,它的值相同,static定义,为整个类所共有.相对于“实例属性” 如果static成员是私有类型, ...
- cojs QAQ的矩阵 题解报告
题目描述非常的清晰 首先我们考虑(A*B)^m的求法,这个部分可以参考BZOJ 杰杰的女性朋友 我们不难发现(A*B)^m=A*(B*A)^(m-1)*B A*B是n*n的矩阵,而B*A是k*k的矩阵 ...
- Project Euler 95:Amicable chains 亲和数链
Amicable chains The proper divisors of a number are all the divisors excluding the number itself. Fo ...
- lintcode :Partition List 链表划分
题目: 链表划分 给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前. 你应该保留两部分内链表节点原有的相对顺序. 样例 给定链表 1->4->3->2-& ...
- *[codility]Country network
https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...
- asp.net中几个网页跳转的方法及区别
1:注意:Response.Redirect("a.html")是不能跳出框架.IFRAME的. 可以使用 Response.Write("<script Lang ...