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

  • SEARCH
  • Home
  • 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 649325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:55:55+00:00 2026-05-13T21:55:55+00:00

I am new to Servlets. I want to use a method which is called

  • 0

I am new to Servlets. I want to use a method which is called only once after deploying to server. I looked at HttpServlet#init(). But I figured out it is called with each request. Did I misunderstand it? What are the alternatives to init()?

  • 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-13T21:55:56+00:00Added an answer on May 13, 2026 at 9:55 pm

    No, it is not called in each request. It is only called during initialization of the servlet which usually happens only once in webapp’s lifetime. Also see this answer for a bit more detail how servlets are created and executed.

    If you actually want to do some global/applicationwide initialization (which is thus not per se tied to only the particular servlet), then you would normally use the ServletContextListener for this. You can do the initialization stuff in the contextInitialized() method.

    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    @WebListener
    public class Config implements ServletContextListener {
        public void contextInitialized(ServletContextEvent event) {
            // Do stuff during webapp's startup.
        }
        public void contextDestroyed(ServletContextEvent event) {
            // Do stuff during webapp's shutdown.
        }
    }
    

    If you’re not on Servlet 3.0 yet and can’t upgrade, and thus can’t use @WebListener annotation, then you need to manually register it in /WEB-INF/web.xml like below:

    <listener>
        <listener-class>com.example.Config</listener-class>
    </listener>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a component which can be used like: <mc:chart data=#{bean.data} width=200
I want to make template engine available in servlets. The concrete template engine implementation
I am using Twitter4J API on GAE/J. I want to use the request token
I have a form/calculator, which posts to itself some data, this data is then
I play for a while (couple of weeks) with this HttpClient lib. I want
Here's what I'm trying to do: public void init(ServletConfig config) { // ... URL
Where should I locate the code for validating an employee ID (badge) that will
Okay, I'm NOT a Java noob, it just so happens that I've forgotten a
In struts2 upload methods, can I choose where the uploaded file must be saved.

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.