像 .net 的“Application_Start”这样的事件和“Begin_Request”对于java/tomcat/JSP?
有没有办法在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在Java EE(Servlet+JSP)世界中,可以通过实现Java EE规范标准化的相关接口来获得等效的功能。
与应用程序概念等效的是Web 上下文或Servlet 上下文。 Java EE 中的会话和请求与 .Net 中的概念相同。需要实现相关的侦听器类,以便挂钩
有关这方面的更多信息,请参阅 Servlet 上的 Java EE 5 教程生命周期。这些界面也同样适用于 Java EE 6 。
过滤器与 ServletRequestListener
如果您阅读了评论,您会注意到可以通过实现
ServletRequestListener
或Filter
来对请求进行预处理和后处理。我建议您使用
Filter
(就像 BalusC 一样)。这是因为每次将请求发送到特定 URL 时都会调用Filter
,并且通常是确保对某个 URL 的所有请求都得到相同“处理”的最有效方法。其原因可在 上的 Java EE API 文档中找到
ServletRequestListener
:当您使用
ServletRequestListener
时,您必须注意 requestInitialized 和 requestDestroyed 事件每个请求仅触发一次(与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
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 aFilter
.I would suggest that you utilize
Filter
s (as did BalusC). This is because theFilter
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
:When you use a
ServletRequestListener
, you must note that the requestInitialized and requestDestroyed events are fired only once per request (unlike theFilter
where the doFilter method is invoked everytime theFilter
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.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.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
要在“应用程序启动”时执行某些操作,您需要实现 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.
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