Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6774883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:50:26+00:00 2026-05-26T15:50:26+00:00

I have a bare bones servlet application. Do I store config related information in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:50:27+00:00Added an answer on May 26, 2026 at 3:50 pm
    application_startup
    application_shutdown
    

    Implement ServletContextListener.

    @WebListener
    public class ApplicationListener implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent event) {
            // Do your job here on application startup.
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent event) {
            // Do your job here on application shutdown.
        }
    
    }
    

    You can store application wide variables as an attribute of the ServletContext.

    event.getServletContext().setAttribute("foo" new Foo());
    

    It’s available in servlets by the inherited getServletContext() method.

    Foo foo = (Foo) getServletContext().getAttribute("foo");
    

    And in JSPs by just EL.

    ${foo.someProperty}
    

    application_beginRequest
    application_endRequest
    

    Implement ServletRequestListener:

    @WebListener
    public class RequestListener implements ServletRequestListener {
    
        @Override
        public void requestInitialized(ServletRequestEvent event) {
            // Do your job here on request begin.
        }
    
        @Override
        public void requestDestroyed(ServletRequestEvent event) {
            // Do your job here on request end.
        }
    
    }
    

    You can store request wide variables as an attribute of the ServletRequest.

    event.getServletRequest().setAttribute("foo" new Foo());
    

    It’s available in servlets by the passed-in HttpServletRequest argument.

    Foo foo = (Foo) request.getAttribute("foo");
    

    And in JSPs by just EL.

    ${foo.someProperty}
    

    You can even implement the both interfaces in a single class:

    @WebListener
    public class GlobalListener implements ServletContextListener, ServletRequestListener {
        // ...
    }
    

    Or, more common, for sure if you want to be able to modify/control requests more globally, a Filter:

    @WebFilter(urlPatterns={"/some/*"})
    public class SomeFilter implements Filter {
    
        @Override
        public void init(FilterConfig config) throws ServletException {
            // Do here your application startup job.
    
            // If you have any <init-param> in web.xml, then you could get them
            // here by config.getInitParameter("name") and assign it as field.
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            // Do here your request/response preprocessing job.
    
            // Continue the request/response (if you haven't already forwarded/redirected the request/response beforehand).
            chain.doFilter(request, response);
    
            // Do here your request/response postprocessing job.
        }
    
        @Override
        public void destroy() {
            // Do here your application shutdown job.
    
            // If you have assigned any expensive resources as field of
            // this Filter class, then you could clean/close them here.
        }
    
    }
    

    See also https://stackoverflow.com/tags/servlet-filters/info.

    All other listeners of the Servlet API can be found as interfaces in the javax.servlet package. Learn to find your way in the Javadocs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

broken to bare-bones scene: I have a program in c# that calls a .exe
I have been trying to write a bare-bones ping scanner using Perl for internal
I'm writing a bare bones Python wsgi application and am getting stumped by module
I have a WinForms application that uses XNA to animate 3D models in a
I have a bare bones ORM implementation, consisting of data mappers which load and
I am trying a write a program that implements a bare bones POSIX cat
I'm trying to get a bare-bones example of logging going in my ASP.NET application.
I'm creating a web site that I think must have a client side database.
EDIT The bare-bones version of this question is, if I have some object o
See code below. I've tried to strip it to its bare bones. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.