Execute code after Glassfish Web Deployment [dupli

2019-02-20 07:33发布

问题:

This question already has an answer here:

  • Using special auto start servlet to initialize on startup and share application data 1 answer

I'm trying to run a java web service on Glassfish. There is some initialization code that sets a few variables and retrieves some information from the Glassfish environment itself. I have that code in a static initializer inside the @WebService class, however this code appears to be called too early, it gets run as soon as the WebService endpoint is deployed, whereas I need it to run once the whole web service is successfully deployed.

I tried moving the code into the constructor of the WebService class, however then that code was only run when I went into the Tester web page and sent some data to make the web methods run.

Is there any way to set up some initialization code to be run as soon as the whole web service deployment is completed?

回答1:

Option 1: In Glassfish you have the Lifecycle modules

Option 2: You also have the ability to code a ServletContextListener to be triggered when the context is loaded:

public class MyServlet implements ServletContextListener {

  public void contextInitialized(ServletContextEvent e) {
         // implementation code
  }

  public void contextDestroyed(ServletContextEvent e) {
         // implementation code
  }
}

Reference:

  • About Life Cycle Modules
  • Example of ServletContextListener


回答2:

Yes @Jaynathan Leung, with soapUI can you deployment the web services and testing your programming with input and output data. I hope help you. :)