I have a bare bones servlet application.
Do I store config related information in my web.xml file?
Is there an event that fires in my servlet that I should save the config value to to a static readonly variable?
I have a single servlet right now, but is there another file where the lifecycle begins at a global level?
Like in .net, you have your pages, but there is a global.asax.cs class that fires at specific events like:
application_startup
application_shutdown
application_beginRequest
application_endRequest
Does servlets have this, or is it on a per-servlet basis?
Implement
ServletContextListener.You can store application wide variables as an attribute of the
ServletContext.It’s available in servlets by the inherited
getServletContext()method.And in JSPs by just EL.
Implement
ServletRequestListener:You can store request wide variables as an attribute of the
ServletRequest.It’s available in servlets by the passed-in
HttpServletRequestargument.And in JSPs by just EL.
You can even implement the both interfaces in a single class:
Or, more common, for sure if you want to be able to modify/control requests more globally, a
Filter:See also https://stackoverflow.com/tags/servlet-filters/info.
All other listeners of the Servlet API can be found as interfaces in the
javax.servletpackage. Learn to find your way in the Javadocs.