• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do I access object populated in servlet

 
dhriti joshi
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am populating data from database in an object inside the init method os the servlet.
then I am configuring this servelet in web.xml to load on start up using<load-on-startup>

public class TestServlet extends HttpServlet {

TreeMap tm =null;
public void init() throws ServletException {
tm= call to database(not posting actual code in this line)
}
}

I am not much familiar with servlets,my question is how do I access this object- tm in other classes.

Thanks in advance.
Dhriti.
 
D Rog
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you can assign it to a singleton variable or store as servlet context attribute.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A servlet is a poor choice for this.

If you need to do something at startup that is not specific to a particular servlet, use a context listener instead. And then, as D pointed out, store the object in application scope (servlet context) for later retrieval by other components.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic