0

we develops a project name as FirstApplication for sending,consuming messages from Activemq and deployed in Tomcat7.After deploying,we have to trigger http://localhost:8080/FirstApplication/PackageName/SecondConsumer link only 1 time.So far it's fine.

My doubt,later If we restart the server,again first time we have to trigger that servlet corresponding link.Instead of doing like this,I want to configure.

Note: what I want,servlet should be evaluate automatically whenever Tomcat server start.servlet placed under WebApps/FirstApplication/WEB-INF/src/classes/PackageName/SecondConsumer.java.

For this, I tried with the following code using servletContextListenerclass.

 public class SecondConsumer extends HttpServlet implements ServletContextListener{
 @Override
 protected void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
   //my business code
 }
 @Override
 public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub

 }

 @Override
 public void contextInitialized(ServletContextEvent arg0) {
// TODO Auto-generated method stub

 }
}

and added <Listener>in my web.xml(which is placed under WebApps/FirstApplication/WEB-INF/web.xml) file in the following way.

    <listener>
    <listener-class>PackageName.SecondConsumer</listener-class>
   </listener>

If you observe my code, I didn't implement any code under contextInitialized and contextDestroyed methods.Just i want to evaluate this servlet code while server starting time.

I tried in the above way,it's not working.

please can anyone suggest me.

Thanks.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Hanumath
  • 1,117
  • 9
  • 23
  • 41

3 Answers3

4

You should do your initialization in a class which implements ServletContextListener, so you need something like this

@WebListener
public class AppServletContextListener implements ServletContextListener{

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("ServletContextListener destroyed");
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
         //do your init steps here  
    }
}

This piece of code will execute whenever your server starts or stops. Note that WebListener annotation works only in Java EE 6, prior to that you would need to define <listener> element in a web.xml.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
  • you mean Am I need to write one more java file.Basically I am not a `Java` developer so can you explain clearly.Am I need to define `Listener` element into my `WebApps/firstApplication/WEB-INF/web.xml`. – Hanumath Sep 16 '13 at 19:18
  • Yes, this will be a new class (file). No, you don't need to define `` element in web.xml, `@WebListener` annotation is enough (in case you use Java EE 6 - which you probably do). – Petr Mensik Sep 16 '13 at 19:22
  • I modified my question with what I tried based on your suggestion.it's not working.can you check it once whether is it right. – Hanumath Sep 16 '13 at 19:56
  • You really can't extend `HttpServlet` so remove that declaration from your listener and it should work then. Your listener is simply just not a servlet, it's just a listener which allows you to execute some code upon server startup or end. – Petr Mensik Sep 17 '13 at 06:48
  • Thanks you.. I got it it's working fine without servlet – Hanumath Sep 17 '13 at 12:29
1

Tomcat while running create an instance of servlet, so if you want to invoke servlet please use context listner class which actually triggers while running tomcat itself.

Thanks

geekIndiana
  • 87
  • 4
  • 14
  • Basically I am not a `Java` developer. If you know any tutorials about this suggest me or can you explain clearly with my names(What I mention in my question). – Hanumath Sep 16 '13 at 19:12
  • This link will help you to understand the basic of contextlistner - [link](http://www.mkyong.com/servlet/what-is-listener-servletcontextlistener-example/) – geekIndiana Sep 16 '13 at 19:20
0

I am not quite sure, what you want. If you simply want to execute code while starting up the tomcat, you con configure a ServletContextListener

Christian Kuetbach
  • 15,850
  • 5
  • 43
  • 79