Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to use a method which is called only once after deploying to Server [duplicate]

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()?

like image 988
mebada Avatar asked Mar 02 '10 15:03

mebada


People also ask

When the doGet () method is used which of the following indicates the maximum amount of data that can be passed from the UI?

What is the limit of data to be passed from HTML when doGet() method is used? The limit of data is 2k that needs to be passed from HTML when doGet() method is used. It uses the abstract class javax. servlet.

Which method of the servlet will be called when a servlet is undeployed removed in a server?

The web container calls the destroy method when it needs to remove the servlet such as at time of stopping server or undeploying the project.

What is the purpose of RequestDispatcher?

Interface RequestDispatcher. Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server.

Which method of the servlet will be called to process a client's request in a servlet?

The servlet is initialized by calling the init () method. The servlet calls service() method to process a client's request.


2 Answers

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>
like image 154
BalusC Avatar answered Sep 22 '22 02:09

BalusC


init() is only called upon creation of the servlet. This may happen multiple times during the life of the server. You use it to initialize any variables or logic required for regular use of the servlet.

Edit: After re-reading your post, it is not technically called with each request because the server is creating a new instance of the servlet for each request. Check your server settings as to whether it will get a new servlet of keep a single servlet for the life of the server.

like image 23
Poindexter Avatar answered Sep 25 '22 02:09

Poindexter