关闭

Filter 统一全站编码

标签: 过滤器互联网servletJSPmvc
251人阅读 评论(0) 收藏 举报
分类:


2014年9月8日10:30:46 




//界面输出中文及中文请求参数(POST方式有效)编码过滤器        
package cn.itcast.filter.example;
 
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;
 
/*
web。xml中的配置:
<filter>
<filter-name>CharacterSetFilter</filter-name>
<filter-class>cn.itcast.filter.example.CharacterSetFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterSetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
*/
//界面输出中文及中文请求参数(POST方式有效)编码过滤器
public class CharacterSetFilter implements Filter {
private FilterConfig filterConfig;
public void destroy() {
 
}
 
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String encodingValue = filterConfig.getInitParameter("encoding");//获取过滤器配置中的参数
if(encodingValue==null)//如果没有配置该参数
encodingValue="UTF-8";//给定默认值UTF-8
request.setCharacterEncoding(encodingValue);//只对POST请求方式有用
response.setContentType("text/html;charset="+encodingValue);
chain.doFilter(request, response);
}
 
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
 
}




//数据回显 Servlet        
package cn.itcast.servlet;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class ServletDemo2 extends HttpServlet {
 
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//拿到请求参数
String name = request.getParameter("name");
//把拿到的请求参数打印到界面上
PrintWriter out = response.getWriter();
out.write(name);
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
doGet(request, response);
}
 
}


​# PS:以上方法是将编码的配置封装到了 web.xml 配置文件中。用户只要通过在 .xml 文件中进行相关参数配置,就可以实现统一全站的编码问题了。倘若用户未配置相关参数,则默认编码为"UTF-8".
            ·
            另外,若只是想进行简单的 统一全站编码,而不需要用户对编码进行设置,则可以直接在 过滤器 Filter 类中设置:request.setCharacterEncoding(”UTF-8“);//只对POST请求方式有用
response.setContentType("text/html;charset=UTF-8");
即可. 免去了再在个各类中编码同样代码的 冗杂性缺陷!

2014年9月8日10:30:46 




//界面输出中文及中文请求参数(POST方式有效)编码过滤器        
package cn.itcast.filter.example;
 
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;
 
/*
web。xml中的配置:
<filter>
<filter-name>CharacterSetFilter</filter-name>
<filter-class>cn.itcast.filter.example.CharacterSetFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterSetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
*/
//界面输出中文及中文请求参数(POST方式有效)编码过滤器
public class CharacterSetFilter implements Filter {
private FilterConfig filterConfig;
public void destroy() {
 
}
 
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
String encodingValue = filterConfig.getInitParameter("encoding");//获取过滤器配置中的参数
if(encodingValue==null)//如果没有配置该参数
encodingValue="UTF-8";//给定默认值UTF-8
request.setCharacterEncoding(encodingValue);//只对POST请求方式有用
response.setContentType("text/html;charset="+encodingValue);
chain.doFilter(request, response);
}
 
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
 
}




//数据回显 Servlet        
package cn.itcast.servlet;
 
import java.io.IOException;
import java.io.PrintWriter;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class ServletDemo2 extends HttpServlet {
 
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//拿到请求参数
String name = request.getParameter("name");
//把拿到的请求参数打印到界面上
PrintWriter out = response.getWriter();
out.write(name);
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
 
doGet(request, response);
}
 
}


​# PS:以上方法是将编码的配置封装到了 web.xml 配置文件中。用户只要通过在 .xml 文件中进行相关参数配置,就可以实现统一全站的编码问题了。倘若用户未配置相关参数,则默认编码为"UTF-8".
            ·
            另外,若只是想进行简单的 统一全站编码,而不需要用户对编码进行设置,则可以直接在 过滤器 Filter 类中设置:request.setCharacterEncoding(”UTF-8“);//只对POST请求方式有用
response.setContentType("text/html;charset=UTF-8");
即可. 免去了再在个各类中编码同样代码的 冗杂性缺陷!

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:8115次
    • 积分:367
    • 等级:
    • 排名:千里之外
    • 原创:30篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章存档