0

I want to create an array of camera object, which I defined as a java class. This object is to be shared by all the servlets in my web app, and I want it to be initialized when the server is started, not when I load any servlets. How do I go about doing it?

My initializations:

public camera[] dummycams = new camera[5];
    dummycams[0] = new camera("pcam", 640, 480);
    dummycams[1] = new camera("icam", 641, 481);
    dummycams[2] = new camera("acam", 642, 482);
    dummycams[3] = new camera("hcam", 643, 483);
    dummycams[4] = new camera("mcam", 644, 484);

I am using a glassfish server, Java EE7.

Ivan Ling
  • 127
  • 2
  • 10

1 Answers1

2

You can add the array in ServletContext.

you can use ServletContextListener to listen when servlet context initialization event and retrieve servletContext and add array to it.

You can go through the API here ServletContextLitener

Sunil Rajashekar
  • 350
  • 2
  • 18