Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke a Servlet (doGet) in a web application on startup?

I need to invoke a Servlet on application startup since it contains some application initialization logic.

I know I can set load-on-startup configuration, but this will only invoke Servlet’s init method. I need to invoke a doGet method and pass some Url parameters to it. Servlet doGet method expects ServletRequest and ServletResponse objects.

Also, since this is clustered application, I need to know exactly what node I am accessing (since one option is just to open a socket and invoke a Servlet).

What is the best option to perform this?

EDIT: As a clarification, Servlet already exist and can not be modified. Until now, someone would manually invoke the Servlet from the browser. I need to automatize this.

like image 521
Dan Avatar asked Dec 14 '25 09:12

Dan


1 Answers

Normally, bootstrup initialization / shutdown cleanup is achieved with ServletContextListener - did you consider this option?

Alternatively, as an ugly hack, you can implement a servlet superclass with the initializtion logics, that will be called just once.

like image 78
Vasil Remeniuk Avatar answered Dec 16 '25 22:12

Vasil Remeniuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!