aspose file tools*
The moose likes Servlets and the fly likes Context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Context" Watch "Context" New topic
Author

Context

K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
I have a doubt in init() method. It's not mandatory to override init() method. In the absense of this method, how can we get the context instance? Can we get in doGet() or doPost() methods??

Any help on this question will be appreciated.

Thanks for your time,
Kiran.
Ravinder Rana
Greenhorn

Joined: Dec 07, 2006
Posts: 19
There are two signatures of init() method: init(ServletContext) and init().
The container creates an instance of ServletContext and passes it's reference for our use. We need to store this instance as a reference. The HttpServelt class does this for us and then it calls init() from that method[init(ServletCOntext)] itself. So we don't need to override init(ServletContext) or init() method to get the instance of ServletContext object.
Raghavendra Nittur
Greenhorn

Joined: Feb 19, 2007
Posts: 29
Originally posted by K Kiran Kumar:
It's not mandatory to override init() method.

Overriding of any method is not method is not mendatory unless the method is not implemented in super class ( abstract class)
Originally posted by K Kiran Kumar:

In the absense of this method, how can we get the context instance? Can we get in doGet() or doPost() methods??


If are not overriding, then it doesn't mean that method will be absent. Container calls the methods defined in the super class Heirarchy.
Container takes care of creating instance of Context.

Your Self find which class you extend in your servlet and where exactly the Life cycle functions are defined ( init(), Service() and destory() )

Regards, Raghav
Jaime M. Tovar
Ranch Hand

Joined: Mar 28, 2005
Posts: 133
You can use the getServletContext() method in the doPost, doGet methods to get access to the ServletContext.

public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
Enumeration e = this.getServletContext().getInitParameterNames();
while(e.hasMoreElements()) {
Object o = e.nextElement();
System.out.println(o + " " + getServletContext().getInitParameter(o.toString()));
}
}


She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
Damodara Reddy
Ranch Hand

Joined: Feb 09, 2007
Posts: 44
Originally posted by Ravinder Rana

There are two signatures of init() method: init(ServletContext) and init().
The container creates an instance of ServletContext and passes it's reference for our use. We need to store this instance as a reference. The HttpServelt class does this for us and then it calls init() from that method[init(ServletCOntext)] itself. So we don't need to override init(ServletContext) or init() method to get the instance of ServletContext object.


There is no init(ServletContext).... there is init(ServletConfig) and init(),
we can override init() which will be called by init(ServletConfig).
in the Servlet interface we have getServletConfig method.
we can get init params by
getServletConfig().getInitParameterNames()
...


Damu
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Originally posted by Raghavendra Nittur:

find which class you extend in your servlet and where exactly the Life cycle functions are defined ( init(), Service() and destory() )



I got it.. The javax.servlet.Servlet Interface
and Refer you this article with clarification of Life cycle..


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
K Kiran Kumar
Ranch Hand

Joined: Jan 04, 2006
Posts: 109
Thanks very much Ravinder and Jaime Tovar. I have slight confusion between
the following methods and their significance.

.getServletContext();
.getContextPath();

As per my understanding the former gets the instance for a particular single request from the client.
The latter gets the path of the application that we have deployed in any of the web servers(Take Apache Tomcat as example) i.e., we get the path of the web application stored under webapps directory(parallel to ROOT). This context we set in server.xml file like
<Context path="/....." docBase="/.." relodable="true" />

Had I perceived in the correct way? Please correct me if I am wrong in understanding the above two methods. What exactly we use the method

request.getServletPath()

Regards,
Kiran.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Context