I have a 3rd party library that I use in my webapp. I need to call an init method from this library during axis2 startup.
I can't wait until the first request comes in - it must be done at startup.
in your web.xml:
<listener>
<listener-class>com.my.YourServletContextListener</listener-class>
</listener>
Where YourServletContextListener has to implement javax.servlet.ServletContextListener and provide your initialization code in the contextInitialized(..) method.
This method is called as soon as the servlet context is loaded.
Related
I have a standard Java 11 Web application, with multiple servlets in it. Some servlets are imported by libraries, others are written within the application itself.
I need to perform a task AFTER all web servlets have been initialized, preferably before any requests come in. The task requires access to ServletContext.
I have tried performing this by creating a ServletContextListener and it's corresponding contextInitialized method, but this performs the task BEFORE the servlets have been initialized.
I cannot add the task to the last servlet to load, as the last servlet loaded is imported from an external library.
It would be preferable if the solution does not involve creating a servlet with no mapping.
If you have access to your deployment descriptor (web.xml) and the servlets are eager loaded, then you could append your servlet in the servlet list and set a high number in load-on-startup element.
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.project.LastServlet</servlet-class>
<load-on-startup>99</load-on-startup>
</servlet>
I tried to find a workaround in the documentation, but I couldn't find it and the ServletContextListener is a no go:
"All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized."
I am utterly new to java web services and currently writing a RESTful web service in java. I want to call a method when the web service is deploying in order to initialize some values. How can I achieve this?..
I am using Axis2 as my deployment engine.
Thanks in advance
You can use a listener ServletContextListener.
When you will launch your application server, the "contextInitialized" method will be called and exactly do what you want like initialize a static configuration class ; depending on what you want to achieve.
EDIT :
You'll need to declare the listener into your web.xml like this :
<listener>
<listener-class>full.classpath.to.MyServletContextListenerImpl</listener-class>
</listener>
As an exemple, I use a listener in order to initialize my custom PoolManager which is a singleton.
I hope it can help you !
I have written a simple servlet with init() and doGet(), doPost() method. I have a requirement, that I have an API which i need to invoke an server startup.
Is it possible to do so. I tried with init method, and setting default values in web.xml, but i am still unable to do so.
Please tell if I am missing something.
Thanks
Have you set the load-on-startup attribute to be positive?
<servlet id=”servlet1”>
<load-on-startup>2</load-on-startup>
</servlet>
Alternatively, you might want to use a ServletContextListener to do initialisation work when the container comes up. This is the 'de facto' standard for having a callback to do some initialisation work when the servlet container comes online e.g. we use that to read in some XML files and populate a cache.
Use a listener class to invoke a method. For e.g....Define a listener in web.xml file. and give a class name in the listener. And now create a servlet class or java class to write a code to invoke the API.
<web-app>
<listener>
<listener-class>MyServlet</listener-class>
</listener>
</web-app>
hope this helps out.
I'm trying to implements initialization and shutdown of a webapp. That includes initialization and shutdown of:
Hibernate (v3.6);
C3P0 (v0.9.1.2);
EHCache (v2.3.0);
Quartz (1.8.4);
Other tasks specific to my webapp;
Using Tomcat 5.5.30 and Java 6. My idea is to avoid resource leaking, mostly because of the redeploy of the webapp in the development environment.
How should I implement this?
Usually for Web initialization and shutdown, you will write a ServletContextListener.
The steps to do this are:
Write a class that implements javax.Servlet.ServletContextListener
Add a tag to web.xml deployment descriptor to register the class you've just created
Deploy your application
When you deploy your application, contextInitialized method will be called. You can place all initialization you want here. On application shutdown contextDestroyed method will be called.
Its also possible to use the HTTP Servlet instead but the listener is a better option.
You have to extend a class with HttpServlet and setting the following stuff to your web.xml:
<servlet>
<servlet-name>StartupServlet</servlet-name>
<servlet-class>your.package.servlets.StartupServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
The class can overwrite the init and the destroy method.
But still you want to manage your resources in such a way that they do not leak if the application crashes and normal shutdown routines are not called.
I wanna know if there is a function that is called directly after the deployment of the server. This functions could be called as an initialisation function of the deployment. Thanks
If this is for a single app, you can implement a ServletContextListener, listening on the contextInitialized() event. You can register an implementation through the <listener> tag in your web.xml.