像 .net 的“Application_Start”这样的事件和“Begin_Request”对于java/tomcat/JSP?

发布于 2024-09-18 07:52:27 字数 287 浏览 10 评论 0原文

有没有办法在Java/Tomcat/JSP Web项目中附加到像asp.net的“Application_Start”和“Begin_Request”这样的事件?我真的不想使用 JSF 或额外的框架(Spring、Struts)。我不想在每个页面上使用“jspInit”之类的东西来执行此操作,目标是全局事件处理程序。

如果我陷入了 .net 的做事方式,那么重点是要有一个中心位置来初始化 IoC 容器 (Application_Start),并实现“每个请求一个数据库事务”工作流程 (Begin_Request)。

谢谢。

Is there a way to attach to events like asp.net's "Application_Start" and "Begin_Request" in Java/Tomcat/JSP web projects? I would really rather not use JSF or an extra framework(Spring, Struts). I do not want to do it on a per-page basis with anything like 'jspInit', a global event handler is the goal.

In the event that I am stuck in the .net way of doing things, the point is to have a central place to initialize IoC containers (Application_Start), and implement a 'One database transaction per request' workflow (Begin_Request).

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不喜欢何必死缠烂打 2024-09-25 07:52:27

在Java EE(Servlet+JSP)世界中,可以通过实现Java EE规范标准化的相关接口来获得等效的功能。

与应用程序概念等效的是Web 上下文或Servlet 上下文。 Java EE 中的会话和请求与 .Net 中的概念相同。需要实现相关的侦听器类,以便挂钩

  • 应用程序生命周期中的相关事件(ServletContextListenerServletContextAttributeListener)、
  • 应用程序提供的请求( ServletRequestListenerServletRequestAttributeListener)或
  • 由相同的会话(HttpSessionListenerHttpSessionActivationListener)建立。

有关这方面的更多信息,请参阅 Servlet 上的 Java EE 5 教程生命周期。这些界面也同样适用于 Java EE 6

过滤器与 ServletRequestListener

如果您阅读了评论,您会注意到可以通过实现 ServletRequestListenerFilter 来对请求进行预处理和后处理。

我建议您使用Filter(就像 BalusC 一样)。这是因为每次将请求发送到特定 URL 时都会调用Filter,并且通常是确保对某个 URL 的所有请求都得到相同“处理”的最有效方法。

其原因可在 上的 Java EE API 文档中找到ServletRequestListener

接收通知的接口
有关请求进入和的事件
超出网络范围
应用程序。

ServletRequest 被定义为即将到来
进入 Web 应用程序的范围时
即将进入第一个servlet
或 Web 应用程序的过滤器,以及
就像超出范围一样退出
最后一个 Servlet 或第一个过滤器
链条。

当您使用 ServletRequestListener 时,您必须注意 requestInitializedrequestDestroyed 事件每个请求仅触发一次(与 Filter< /code> 其中每次在处理管道中调用 Filter 时都会调用 doFilter 方法)。由于过滤器是在请求之前和之后执行操作的常用方式(我没有看到很多人使用 ServletRequestListener),因此我建议您在这样的上下文中使用过滤器。

In the Java EE (Servlets+JSPs) world, the equivalent functionality can be obtained by implementing the relevant interfaces standardized by the Java EE specification.

The equivalent of the Application concept is the Web Context or the Servlet context. Sessions and Requests are the same concept in Java EE as .Net. There are relevant listener classes that need to be implemented in order to hook onto the relevant events in

  • the lifecyle of an application (ServletContextListener and ServletContextAttributeListener),
  • requests served by the application (ServletRequestListener and ServletRequestAttributeListener) or
  • sessions established by the same (HttpSessionListener and HttpSessionActivationListener).

More information on this can be found in the Java EE 5 tutorial on the Servlet lifecycle. The interfaces continue to hold good for Java EE 6 as well.

Filters vs ServletRequestListener

If you've read the comments, you would have noticed that it is possible to do preprocessing and postprocessing of requests by implementing a ServletRequestListener or a Filter.

I would suggest that you utilize Filters (as did BalusC). This is because the Filter will be invoked everytime a request is sent to a particular URL, and is often the most effective way of ensuring that all requests to a URL receive the same 'treatment'.

The reasons for this are found in the Java EE API documentation on the ServletRequestListener:

Interface for receiving notification
events about requests coming into and
going out of scope of a web
application.

A ServletRequest is defined as coming
into scope of a web application when
it is about to enter the first servlet
or filter of the web application, and
as going out of scope as it exits the
last servlet or the first filter in
the chain.

When you use a ServletRequestListener, you must note that the requestInitialized and requestDestroyed events are fired only once per request (unlike the Filter where the doFilter method is invoked everytime the Filter is invoked in a processing pipeline). Since Filters are the usual way of performing actions before and after requests (I haven't seen a lot of people use ServletRequestListeners), I would suggest that you utlize filters in such a context.

久夏青 2024-09-25 07:52:27

Servlet 可以帮助您解决这个问题。在 Java 世界中,事件被称为侦听器。有一些有用的侦听器:

javax.servlet.ServletContextListener

void contextDestroyed(ServletContextEvent sce) 当 servlet 上下文即将被销毁时调用。

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

javax.servlet.ServletContextAttributeListener

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

void attributeRemoved(ServletContextAttributeEvent scae) 从 servlet 上下文中删除属性时调用。

void attributeReplaced(ServletContextAttributeEvent scae) 当 Servlet 上下文中的属性被替换时调用。

javax.servlet.http.HttpSessionListener

void sessionCreated(HttpSessionEvent se) 创建会话时调用。

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

javax.servlet.http.HttpSessionAttributeListener

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

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

void attributeReplaced(HttpSessionBindingEvent se) 在会话中替换属性时调用。

Servlets give you more when it comes to this issue. And in Java world, events are called listeners. There are some useful listeners:

javax.servlet.ServletContextListener

void contextDestroyed(ServletContextEvent sce) Called when the servlet context is about to be destroyed.

void contextInitialized(ServletContextEvent sce) Called when the web application is ready to process requests.

javax.servlet.ServletContextAttributeListener

void attributeAdded(ServletContextAttributeEvent scae) Called when a new attribute is added to the servlet context.

void attributeRemoved(ServletContextAttributeEvent scae) Called when an attribute is removed from the servlet context.

void attributeReplaced(ServletContextAttributeEvent scae) Called when an attribute on the servlet contextis replaced.

javax.servlet.http.HttpSessionListener

void sessionCreated(HttpSessionEvent se) Called when a session is created.

void sessionDestroyed(HttpSessionEvent se) Called when a session is invalidated.

javax.servlet.http.HttpSessionAttributeListener

void attributeAdded(HttpSessionBindingEvent se) Called when an attribute is added to a session.

void attributeRemoved(HttpSessionBindingEvent se) Called when an attribute is removed from a session.

void attributeReplaced(HttpSessionBindingEvent se) Called when an attribute is replaced in a session.

贪恋 2024-09-25 07:52:27

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

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

http://download.oracle.com/javaee/5/api/javax/servlet/Filter.html for handling requests

I think in the end you will have better luck and code that is more maintainable if you investigate frameworks that could solve lots of your problems for you

很糊涂小朋友 2024-09-25 07:52:27

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

To do something at "application start" you need to implement a ServletContextListener. It is part of the standard servlet API. As someone else already mentioned, you can implement one or more filters in a "filter chain" to do special processing before each incoming request is handled by the servlets.

三生殊途 2024-09-25 07:52:27

Servlet API 中提供了“类似”的事件(事件并不是最恰当的词)。对于应用程序启动,您应该使用上下文侦听器
http://download.oracle.com/javaee/5/ api/javax/servlet/ServletContextListener.html

对于请求:
http://download.oracle.com/javaee/5/ api/javax/servlet/ServletRequestListener.html

There are "similar" events (event is not the best word for this) available in the Servlet API. For application start you should use a Context listener
http://download.oracle.com/javaee/5/api/javax/servlet/ServletContextListener.html

and for requests:
http://download.oracle.com/javaee/5/api/javax/servlet/ServletRequestListener.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文