代码之家  ›  专栏  ›  技术社区  ›  jdc0589

像.net对java/tomcat/JSP的“Application\u Start”和“Begin\u Request”这样的事件?

  •  4
  • jdc0589  · 技术社区  · 15 年前

    有没有一种方法可以附加到像asp.net在Java/Tomcat/jspweb项目中的“Application\u Start”和“Begin\u Request”?我真的不想使用JSF或额外的框架(Spring、Struts)。我不想在每页的基础上使用类似于“jspInit”的东西,目标是使用全局事件处理程序。

    谢谢。

    5 回复  |  直到 15 年前
        1
  •  6
  •   Vineet Reynolds    15 年前

    • ServletContextListener服务 ),
    • 应用程序提供的请求( ServletRequestListener ServletRequestAttributeListener服务器 )或者
    • 该委员会设立的会议( HttpSessionListener HttpSessionActivationListener ).

    有关这方面的更多信息,请参阅 Java EE 5 tutorial on the Servlet lifecycle . 接口继续保持良好的性能 Java EE 6

    筛选器与ServletRequestListener

    如果您已经阅读了注释,您会注意到通过实现 ServletRequestListener 或者 Filter .

    过滤器 过滤器

    其原因可以在上的javaeeapi文档中找到 ServletRequestListener

    接收通知的接口 关于请求进入和的事件 应用程序。

    ServletRequest定义为coming 当 它即将进入第一个servlet 或web应用程序的过滤器,以及 当它离开这个区域的时候就超出了范围 中的最后一个servlet或第一个筛选器

    当你使用 请求已销毁 每个请求只触发一次事件(与 过滤器 doFilter公司 方法在每次 过滤器 在处理管道中调用)。由于过滤器是在请求前后执行操作的常用方式(我没有看到很多人使用ServletRequestListeners),因此我建议您在这样的上下文中使用过滤器。

        2
  •  1
  •   Aaron Saunders    15 年前

    http://download.oracle.com/javaee/5/api/javax/servlet/Filter.html 用于处理请求

    我认为如果你研究可以为你解决很多问题的框架,最终你会有更好的运气和更易维护的代码

        3
  •  1
  •   Jim Tough    15 年前

    要在“应用程序启动”时执行某些操作,您需要实现 ServletContextListener . 它是标准servlet API的一部分。正如前面提到的,您可以在“过滤器链”中实现一个或多个过滤器,以便在servlet处理每个传入请求之前进行特殊处理。

        4
  •  1
  •   Paul Gregoire    15 年前

    ServletAPI中有“类似”事件(event不是最好的词)。对于应用程序启动,应该使用上下文侦听器 http://download.oracle.com/javaee/5/api/javax/servlet/ServletContextListener.html
    对于请求: http://download.oracle.com/javaee/5/api/javax/servlet/ServletRequestListener.html

        5
  •  1
  •   Truong Ha    15 年前

    void contextDestroyed(ServletContextEvent sce)

    void contextInitialized(ServletContextEvent sce) 当web应用程序准备好处理请求时调用。

    void attributeAdded(ServletContextAttributeEvent scae) 在向servlet上下文添加新属性时调用。

    void attributeRemoved(ServletContextAttributeEvent scae)

    void attributeReplaced(ServletContextAttributeEvent scae) 在替换servlet上下文上的属性时调用。

    javax.servlet.http.HttpSessionListener

    void sessionCreated(HttpSessionEvent se)

    void sessionDestroyed(HttpSessionEvent se) 会话无效时调用。

    void attributeAdded(HttpSessionBindingEvent se) 将属性添加到会话时调用。

    void attributeRemoved(HttpSessionBindingEvent se) 从会话中移除属性时调用。

    void attributeReplaced(HttpSessionBindingEvent se)