• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Context

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()));
}
}
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()
...
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
K Kiran Kumar
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic