如何将Servlet筛选器仅应用于具有HTTP POST方法的请求 [英] How to apply a servlet filter only to requests with HTTP POST method

查看:32
本文介绍了如何将Servlet筛选器仅应用于具有HTTP POST方法的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想应用一个过滤器,但我不希望所有请求都必须转到该过滤器.

In my application I want to apply a filter, but I don't want all the requests to have to go to that filter.

这将是一个性能问题,因为我们已经有了其他一些过滤器.

It will be a performance issue, because already we have some other filters.

我希望我的过滤器仅适用于HTTP POST请求.有什么办法吗?

I want my filter to apply only for HTTP POST requests. Is there any way?

推荐答案

尚无此功能. Filter 应用于所有HTTP方法都没有开销.但是,如果您在 Filter 代码中包含一些逻辑,这会产生开销,那么您就不应该将该逻辑应用于不需要的HTTP方法.

There is no readily available feature for this. A Filter has no overhead in applying to all HTTP methods. But, if you have some logic inside the Filter code which has overhead, you should not be applying that logic to unwanted HTTP methods.

这是示例代码:

public class HttpMethodFilter implements Filter
{
   public void init(FilterConfig filterConfig) throws ServletException
   {

   }

   public void doFilter(ServletRequest request, ServletResponse response,
       FilterChain filterChain) throws IOException, ServletException
   {
       HttpServletRequest httpRequest = (HttpServletRequest) request;        
       if(httpRequest.getMethod().equalsIgnoreCase("POST")){

       }
       filterChain.doFilter(request, response);
   }

   public void destroy()
   {

   }
}

这篇关于如何将Servlet筛选器仅应用于具有HTTP POST方法的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆