user3610227
user3610227

Reputation:

Access running instances of Servlets

New to java, and this question is different from typical java workflows out there and will need some help from very experienced architect, coming from node.js background, trying to implement an architecture based on some design patterns that I already implemented in node.js and PHP and some frontend languages (including Java Swing) and I look forward to implement the same for Java Servlets.

I understand that within Servlets doGet, doPost etc. are the entry points of a request and a single instance of a servlet is instantiated and re-used concurrently for all incoming requests via multithreading.

for these design patterns (another topic, requires it's own thread) to work I need to have an uber level access to get hold of these servlet instances and set my actors on top of them as delegates/listeners.

I've read that servlets are either instantiated after the first request or loaded right with the container, so for this to be implemented I'll need them loaded at the startup so I can perform the operations on them.

Again this question is different and may require some discussion for a deep understanding, to help us to exchange things about each other worlds.

In short I need the following two things to make it work.

  1. A way to define my own class (let's say Uber) to have it loaded as container gets loaded.
  2. The Uber class then is able to get reference to loaded servlets or servlets can reach out to Uber

Upvotes: 1

Views: 883

Answers (1)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Solving point 1 is easy. Create a class that implements ServletContextListener and in method contextInitialized create your instance of Uber.

Point 2 is impossible. The server is not allowed to provide you info about the instances of Servlet it has created. The method to obtain these, ServletContext#getServlet, is deprecated. Also, the application server may use a single instance or multiple instances of a Servlet, the server will decide the behavior at runtime.

Since we don't exactly know your purposes on knowing the instances of Servlets and what to do with them, we cannot provide more help.

Still, in point 1, since you create the instance of Uber, you can store it as an attribute in ServletContext, and then retrieve it in each servlet using HttpServletRequest#getServletContext.


From your comment, it looks like you want/need to implement Front Controller. For that, it will be better to use a single servlet that does this work and create the classes that will do the real job of handling the request. Instead of reinventing the wheel, I suggest you to use a framework that already implements this like JSF or Spring MVC. If you still want/need to implement the pattern by yourself, please check here.

This is how your (odd and not recommendable) design will look like (based on Jozef's comment:

@WebListener
public class AppListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent e) {
        Uber uber = new Uber();
        /* configure uber */
        //store it in ServletContext
        e.getServletContext().setAttribute("uber", uber);
    }
    @Override
    public void contextDestroyed(ServletContextEvent e) {
        //...
    }
}

Then in a servlet, register it into the instance of Uber:

@WebServlet(value="/myServlet", name="myServlet")
public class MyServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) {
        Uber uber = (Uber)config.getServletContext().getAttribute("uber");
        uber.registerServlet("myServlet", this);
    }
    //more code...
}

Still, I don't like the idea of another class needing to know about your Servlet.

Upvotes: 5

Related Questions