过滤器doFilter----支持中文...

//EncodeFilter.java

package org.rockie;

import javax.servlet.*;

import java.io.IOException;

public class EncodeFilter implements Filter {

protected String encoding = null;

protected FilterConfig filterConfig = null;

protected boolean ignore = true;

public void destroy() {

    this.encoding = null;

    this.filterConfig = null;

}

public void doFilter(ServletRequest request, ServletResponse response,

                      FilterChain chain)

throws IOException, ServletException {

   if (ignore || (request.getCharacterEncoding() == null)) {

     String encoding = selectEncoding(request);

     if (encoding != null)

       request.setCharacterEncoding(encoding);

   }

   chain.doFilter(request, response);

}

public void init(FilterConfig filterConfig) throws ServletException {

    this.filterConfig = filterConfig;

    this.encoding = filterConfig.getInitParameter("encoding");

    String value = filterConfig.getInitParameter("ignore");

    if (value == null)

      this.ignore = true;

    else if (value.equalsIgnoreCase("true"))

      this.ignore = true;

    else if (value.equalsIgnoreCase("yes"))

      this.ignore = true;

    else

      this.ignore = false;

}

}

 


web.xml文件配置一个过滤器,给出encodingignore设置过滤*.do

<filter>

           <filter-name>Set Character Encoding</filter-name>

           <filter-class>org.rockie. EncodeFilter </filter-class>

           <init-param>

                  <param-name>encoding</param-name>

                  <param-value>GB2312</param-value>

           </init-param>

           <init-param>

                  <param-name>ignore</param-name>

                  <param-value>true</param-value>

           </init-param>

    </filter>

    <!-- Filter Mapping -->

    <filter-mapping>

           <filter-name>Set Character Encoding</filter-name>

           <url-pattern>*.do</url-pattern>

    </filter-mapping>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用过滤器可以通过以下步骤实现: 1. 在自启动类中添加注解`@ServletComponentScan("com.congge.filter")`,这样Spring Boot会自动扫描并注册过滤器。 2. 创建一个过滤器类,实现`javax.servlet.Filter`接口,并重写`doFilter`方法来实现过滤逻辑。 3. 在过滤器类上添加`@WebFilter`注解,指定过滤器的URL模式和顺序等配置。 4. 运行Spring Boot应用程序,过滤器将会在请求到达时被调用。 关于User-Agent,它是HTTP请求头的一部分,用于标识客户端的软件、操作系统、版本等信息。在过滤器中,你可以通过`HttpServletRequest`对象的`getHeader`方法获取User-Agent的值,然后进行相应的处理。 总结起来,Spring Boot中的过滤器使用方式类似于Spring MVC中的拦截器,但是过滤器的适用范围更广,可以在Servlet容器中使用。过滤器可以深入到请求的前后,而拦截器只能在方法的前后起作用。因此,在Spring框架中,应该优先使用拦截器。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* *3* [springboot拦截器与过滤器详解](https://blog.csdn.net/zhangcongyi420/article/details/111455137)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值