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.
Related
contextInitialized method of servletcontextlistener is called when the web application initialization process starts. Is ServletContextListener is related to servlet in any ways(as per its name). Because this is called even if do not write any servlet in my web application.
No, it's just an interface for receiving notification events about ServletContext lifecycle changes.
There is the javax.servlet.ServletRequestListener Interface for receiving notification events about requests coming into and going out of scope of a web application servlets.
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'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.
Servlet is also java program but there is no main method in servlet.Who will take role of main method on servet.
Servlets are designed to run inside a servlet container (eg. Apache Tomcat). Execution of a servlet happens in the following manner: The servlet container calls the GenericServlet.service() method on a servlet which typically calls the appropriate doXxx() method, eg. doGet(), doPost(), etc. The doXxx() method is responsible for interpreting the HTTP request and serving an appropriate response. GenericServlet.service() is roughly analagous to main() in a plain old java class.
Servlet runs inside a container(eg:tomcat). This container perform its work under jvm.
Here container takes "the absence of main method". In simlple java program main method tells
the starting control flow of the execution. But in case of servlet base web application jvm dose not need to search the main method. Servlet container tells jvm about the starting
control flow.
Servlet are deployed on Java application server (servlet container). They are kind of 'passive'. When you write servlet, your servlet code is called by the container whenever there's request or need. So you don't see 'main' in your servlet (the whole thing is not started from servlet), which is inside application server (you could imagine the startup of application server starts from some kind of main).
If you're looking for an area in a servlet to place code that's run on startup (similar to main()), take a look at implementing the ServletContextListener interface.
Its two methods are called on application startup and shutdown.
There is no main method in a Java servlet any more than than an ActionListener on a Swing JButton has a main method. What they both do have are methods that you can hook into when a certain event happens (a click on the JButton for example, or an HTTP PUT request on a HttpServlet). And in both cases, you are provided information about the event that triggered the call - the ActionEvent for the JButton and the ServletRequest for a servlet.
Thinking of servlets in terms of event handlers is probably more useful than trying to think of them like a standalone Java application where you are responsible for the entire control flow.
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.