Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Best way to get object from ServletContext via a request  RSS feed

 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I've set up:
I have a controller servlet that, based on a certain parameter, passes all requests/responses to the appropriate handler class. These classes then do what they need to, and forward the request/response to jsp files for viewing (classic MVC, adapted from Professional JSP, just in case you've read it).
Now, I have a page where a user selects a person, clicks submit, and then should see a list of fields associated with that person for editting. Here's the problem.
I have a List containing all of the people that you can view being saved at the ServletContext level, because there may be many people at one time modifying this list. However, I'd like to have my Handler class pull out the information from the List, put it into an object, and associate the object in the request. I'd then forward this to a class that dynamically generates a jsp page to show the information.
The only way I have been able to do this is through creating a custom tag lib, where I have access to the pageContext object, from which I can eventually pull the List. However, I'm wondering if there is a way I can have my handler class do this, and not have to rely on a taglib (not that I think they are bad).
Any suggestions?
 
Adam Hardy
Ranch Hand
Posts: 567
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand your problem, what I would do in my handler class is: get the session from the request and use session.getServletContext()
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looking at the API's, I don't see what you are referring to. There is a getSessionContext(), which doesn't provide the ServletContext, and is deprecated anyway. Where are you seeing a getServletContext() method associated with the session?
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just wondering as to why can't we have a singleton or a factory class instantiated from the controller servlet's init method holding
on to the list you are talking about.
In that case your handler class w'd'nt need a handle to the ServletContext.
class UserList{
//singleton...

getUserList() {
return userList;
}
}
handler classes can just do
UserList.getInstance().getUserList()
regards,
karthik
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, getServletContext() is in the GenericServlet class so you should be able to use it anywhere in your servlet. (provided that you either use the servlet init() method or use the init( ServletConfig config ) make an immediate call to super.init( config ))
Bill
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, that I have done (using getServletContext() ) already to set the list into the context, my problem is getting the list from the context from a class that only has access to the request object. You pretty much get halted at the session, and as far as I can tell session does not have any methods that are able to eventually get the context (I'd love to know the reasoning behind this).
However, I like karthik's suggestion, create a singleton "wrapper" class for my list, and access it via that. Does away with the whole need for placing the List in the context.
I went ahead and just used a tag lib for now, but I'll have to play with the singleton idea and let ya know how it works.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just some thoughts...
since you are using a MVC pattern with a controller servlet @ the heart of the application, initialising the factory/singleton in the init of that servlet will work fine.
In case you have lots of servlets w/o a controller, then you might
even consider using the new listener interface javax.servlet.ServletContextListener
added to the servlet spec 2.3.
You can actually receive notification events when the servlet context is destroyed or initialized. You might want to put your initialisation code in those callbacks, so that you can be sure that the resource is available to all the servlets.
de-allocation of the resource can be done in the other callback method.
In case of MVC, i guess you must be writing that part of the code in the destroy() ?
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, my servlet never uses the list itself, it's all the action model handlers that deal with the request and lists, so I'm not sure what you are asking.
I have a handler that deals with deallocating resources. When the user clicks a "Finish" link, that handler's process() method eventually gets called, and it removes things from the session (have two strings in there) and any other object floating around.
I implemented your idea about the singleton, works great, a lot less headache.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!