I have seen an example of Singleton class object where it has a map containing user-id and password retrieved from database. This will help not to hit DB everytime during user login.
Now i am thinking, can’t this be done through ServletContext attribute.
Basically on both the cases we will have application wide object.
So, Can we use ServletContext attribute in this case.
If you need to access the
Mapfrom anywhere in your application then yes it is actually the best place to place it.You can register a Servlet Context Listener and on
contextInitializedyou can get a singleton and store it as an attribute ofServletContextThen access it using by anywhere in your web application by getting the corresponding attribute
getServletContext().getAttribute("UsersMap");