Event of type .net "Application_Start" and "Begin_Request" for java / tomcat / JSP?

Is there a way to connect to events like asp.net "Application_Start" and "Begin_Request" in Java / Tomcat / JSP web projects? I would prefer not to use JSF or an additional framework (Spring, Struts). I don't want to do this on a per-page basis with something like "jspInit", the goal is a global event handler.

In case I am stuck in the .net way of doing something, I need to have a central place to initialize the IoC containers (Application_Start) and implement the workflow "One database transaction per request" (Begin_Request).

Thanks.

+4
source share
5 answers

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

The equivalent of an application concept is a Web Context or Servlet. Sessions and queries are the same concept in Java EE as .Net. There are corresponding classes of listeners that must be implemented in order to connect to the relevant events in

  • application life cycle (ServletContextListener and ServletContextAttributeListener),
  • requests served by the application (ServletRequestListener and ServletRequestAttributeListener) or
  • set by the same (HttpSessionListener and HttpSessionActivationListener).

See the Java EE 5 Servlet Lifecycle Tutorial for more information . Interfaces continue to be useful for Java EE 6 .

Filters vs ServletRequestListener

If you read the comments, you would notice that you can pre-process and post-process requests by implementing ServletRequestListener or Filter .

I would suggest that you use Filter (like BalusC). This is because Filter will be called every time a request is sent to a specific URL and is often the most efficient way to ensure that all requests to the URL receive the same β€œhit”.

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

The interface for receiving notification events about entry and exit from the scope of the network application.

The value of ServletRequest is defined as entering the scope of the web application when it is about to enter the first servlet or filter of the web application, and when it goes out of scope, when it leaves the last servlet or first filter in the chain.

When you use ServletRequestListener , you should notice that requestInitialized and requestDestroyed events are fired only once for each request (unlike Filter , where the doFilter method is called every time the Filter is called in the processing pipeline), since filters are the usual way to perform actions before and after the requests (I have not seen many people use ServletRequestListeners), I would suggest you use filters in this context.

+6
source

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

I think in the end you will be more lucky and the code will be more convenient if you explore the framework that could solve many of your problems for you.

+1
source

To do something when starting the application, you need to implement ServletContextListener . It is part of the standard servlet API. As mentioned earlier, you can implement one or more filters in the "filter chain" to perform special processing before each incoming request is processed by servlets.

+1
source

There are β€œsimilar” events in the Servlet API (an event is not the best for this). To start the application, you must use the 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

+1
source

Servlets give you more when it comes to this problem. And in the Java world, events are called listeners. There are several helpful listeners:

javax.servlet.ServletContextListener

void contextDestroyed(ServletContextEvent sce) Called when the servlet context should be destroyed.

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

javax.servlet.ServletContextAttributeListener

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

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

void attributeReplaced(ServletContextAttributeEvent scae) Called when the servlet context attribute is replaced.

javax.servlet.http.HttpSessionListener

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

void sessionDestroyed(HttpSessionEvent se) Called when the session is invalid.

javax.servlet.http.HttpSessionAttributeListener

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

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

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

+1
source

All Articles