This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes how do I access object populated in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "how do I access object populated in servlet" Watch "how do I access object populated in servlet" New topic
Author

how do I access object populated in servlet

dhriti joshi
Ranch Hand

Joined: Aug 13, 2002
Posts: 82
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

Joined: Feb 07, 2004
Posts: 472

Maybe you can assign it to a singleton variable or store as servlet context attribute.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
jQuery in Action, 2nd edition
 
subject: how do I access object populated in servlet
 
Similar Threads
Loading a cache in servlet init method
Application Session and global variable, either static or hashtable object
Flow of data from Init Servlet to jsp
reference Bean from Servlet
database connection in init() of servlet