I have a plain Java class (POJO?) running inside a Tomcat application that needs access to ServletContext in order to get and set attributes. I've tried having the class extend HttpServlet but can't figure out how to make this work inside a POJO that utilizes its constructor for various chores. My next effort was to write a separate class, have it extend HttpServlet, and give it an init() method that uses -- getServletContext().getAttribute("keyname") -- to get attributes.
The question now is how to pass the object obtained from ServletContext back to the POJO? I did try writing a separate method in the HttpServlet extended class that will return an object, and it got through the compiler okay, but I can't figure out how to call this method from the POJO.
So, how does one obtain stuff from ServletContext when using a POJO? Thank you very much for your help with this question.
I understand about storing it in a reference (in this case the reference is named "context"), but how do I pass this back to the POJO class? Doesn't the POJO need to call a method in the servlet class that has a return value?
I'm not convinced that this so-called "POJO" class -- which I think of as a "Helper" class -- really needs to keep a permanent reference to the ServletContext. And in general objects shouldn't hold onto references if they don't actually need them. So just passing it a reference to the ServletContext when necessary (as in my example) might be better.
Or it might not, and yours might be better. We don't actually know anything about this helper class.
Looking at your (i.e. Jim Borland's) latest post I see I didn't quite understand it. Apparently the POJO class is trying to create an instance of the servlet and use that. No, that's wrong. It's backwards. The servlet should create an instance of the POJO class and use that.
I also notice that the original post said the POJO class was "running inside Tomcat". I didn't understand what that meant so I let it pass. But perhaps there is some relevant information about the life cycle of something there?
Joined: Oct 26, 2009
I very much appreciate all your comments, and I've read each one carefully (over-and-over), but I still don't see the solution to my problem. I have a large legacy code application and there are MANY calls to my POJO throughout the application. The POJO is instantiated using "new" and has a constructor that performs several operations, so I can't convert it to an HttpServlet or anything like that. To repair a bug in the program I need to add functionality to this POJO so it can access the servlet container (my application uses Apache Tomcat as its container).
I just don't see how I can insert a servlet in this chain and have it create the instance of the POJO class. Is there any other way to access the container in order to use ServletContext to get and set attributes? Once again, thank you for your help with this.
Presumably these objects are created by something which does have access to the ServletContext? Like a servlet, for example? Then just add a method where the caller can pass a ServletContext object into the POJO object. Like the example which I posted earlier.
And forget about the idea of making it a servlet. Obviously it isn't a servlet, so there's no point in thinking of that.
Joined: Oct 26, 2009
Having the caller pass a ServletContext object into the POJO object is a terrific idea, and would probably work in most cases, but I just counted and there are 152 classes that call my POJO and each of those Java files would need to be edited. I'm hoping to find something less laborious. Thanks for the feedback!
I think you need to keep one important principle of MVC pattern in mind:- Your service layer should be independent of the controller and view.For the most part, your services shouldn't even know that they are executing inside a webapp. By making POJO have knowledge of ServletContext, you are adding a dependency in the wrong direction.
If your service layer needs access to a data that is available in your servlet context, then your controller should pass the data to the service layer, not the entire Servlet Context.
Joined: Jan 08, 2013
Bear Bibeault wrote:But then, of course, the class is no longer a POJO.
The ServletContext is accessible by any location of the application.
But the solution appointed by Jayesh A Lalwani is the best way to solve this problem without adding a dependency.
One pattern that you can use is the Context pattern. It's not a pattern that I am terribly fond of, but it has it uses. I think the Context Pattern can be terribly misused. It's basically a way of introducing a global variable into a multi threaded application, and should definetly not be used for variables that are modified from many places. I think it is fine for cases where the global variable is being set from one place and used in many many places.
So, basically what the Context pattern is that you have a singleton that encapsulates a thread local variable. When your Servlet first gets the request, it initializes the Context and sets the thread local variable. After this, whoever needs to use the variable, can get it from the singleton. It's basically a thread safe global variable.
Again, I would not reccommend using this pattern for variables that change very often. And as far as possible, it's better to use pass parameters than to use global variables.