运行特定方法的bedore流程请求

时间:2014-07-18 10:56:33

标签: java-ee servlets request

我写过以下课程:

public class GreetChecker implements java.io.Serializable {

    public GreetChecker() {
    }

    public static boolean greetSessionChecker(HttpSession session, String attribute) {
        Enumeration<String> attr = session.getAttributeNames();
        while(attr.hasMoreElements())
        {
            String elem = attr.nextElement();
            if (elem.equals(attribute))
                return true;
        }
        return false;
    }
}

我希望每个servlet在处理传入请求之前调用该方法。我必须在任何servlet中显式调用该方法,或者有一个合适的配置吗?

1 个答案:

答案 0 :(得分:1)

使用ServletRequestListener

import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class ServletRequestListenerDemo  implements ServletRequestListener
{

          public void requestDestroyed(ServletRequestEvent arg0)
          {
            System.out.print("The Request object destroyed at  :"+new java.util.Date());
          }
          public void requestInitialized(ServletRequestEvent req)
          {


             HttpServletRequest request = (HttpServletRequest) req;
             HttpSession session = request.getSession(false);
             yourmethod(session);
          }
}

对于更灵活的解决方案,您可能希望使用@WebFilter注释查看Servlet过滤器。

要进入会话,请参阅:Session variables in ServletRequest