zoukankan      html  css  js  c++  java
  • 处理全站请求编码,无论是GET还是POST,默认是UTF-8

    1.java类:

    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";

    public void destroy()
    {
    }

    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, this.charset);
    }
    else {
    req.setCharacterEncoding(this.charset);
    }
    chain.doFilter(req, response);
    }

    public void init(FilterConfig fConfig) throws ServletException
    {
    String charset = fConfig.getInitParameter("charset");
    if ((charset != null) && (!(charset.isEmpty())))
    this.charset = charset;
    }
    }

    2.在web.xml中添加

    <!-- 处理全站请求编码,无论是GET还是POST,默认是UTF-8 -->
    <filter>
    <filter-name>EncodingFilter</filter-name>
    <filter-class>cn.servlet.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>

  • 相关阅读:
    vue2.0 keep-alive最佳实践
    Vue解决安卓4.4不兼容的问题
    体验异步的终极解决方案-ES7的Async/Await
    axios在vue中的简单配置与使用
    AngularJS-UI-Router
    SignalR 填坑记
    小心C# 5.0 中的await and async模式造成的死锁
    使用BCP导出导入数据
    关闭正在执行的事务 Kill
    C# 正则表达式
  • 原文地址:https://www.cnblogs.com/qiyc/p/5886958.html
Copyright © 2011-2022 走看看