In my Controller servlet (Tomcat) I have instantiated an object and have assigned it to a property(p) of the class, like this :
public class Controller extends HttpServlet {
String xmlFile = "/tmp/page.xml";
private Pager p = new Pager(xmlFile);
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
.............
The reason I have done it that way is, there is a lot of cpu intensive hence time consuming tasks done by that instantiation which needs to be done only once (basically it creates all the html page structure of the application).
Now, I use the persistent object(p) and access some methods of it like this :
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
.................
.................
String name = "xxxxx";
Structure str = p.doAction(name);
..................
}
My question is, will the objects, primitives etc created by the methods like getPageName() and doAction() be cleaned up after Tomcat has done with that particular request ? or will it continue to use the memory (same like the persistent object p of Pager) until next restart/shutdown of Tomcat ?
Another important question is, since multiple requests will be handled by the servlet, will there be any issues with the objects and primitives it uses (most of them are local to the method, but some of these variables use properties of the Class) ? like this :
public Structure doAction(String name) {
if ( pages.containsKey(name) ) {// Here pages is a property of this class (a HashMap)
return( new Structure( (Structure)pages.get(name) )); //this creates a "deep copy of this object and sends it back...
}
return( new Structure() );
}//
As the above method indicates, pages HashMap will be of the form :
page1 => its Structure Object,
page2 => its Structure Ojbect,
..... => .....................,
pageN => its Structure Object
So if the Servlet receives many requests at the same time, for eg: say, there are N number of requests to page2 at a particular time t, will there be any issues accessing the pages HashMap from the doAction() method ? (since its a property of the Class and all these N number of requests will access it (read only, no writing to it) at the same time). I mean, will there be a "read lock" or something ?
Thanks in advance.