• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how Init() access ServletContext

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How does the init() method in servlets have access to the servletContext . in its signature i can only see

public void init(ServletConfig config) and init () . then how does it access servlet context .
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've got a reference to a ServletConfig object (because it's a parameter of the init() method). A ServletConfig object has a getServletContext() method; if you call that method, what do you suppose it returns?

(Hint: Notice how "ServletConfig" is a link in your post? Click on that link and you'll see the API documentation for it.)
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You've got a reference to a ServletConfig object (because it's a parameter of the init() method). A ServletConfig object has a getServletContext() method; if you call that method, what do you suppose it returns?

(Hint: Notice how "ServletConfig" is a link in your post? Click on that link and you'll see the API documentation for it.)



Clapham you are right ,

init(ServletConfig config) is a life cycle method of SERVLET, and it was executed by web container at the time of servlet object creation. Remember this init() method will be executed only onceā€¦,
ServletConfig instantiated by web container.

An object of ServletContext is created by the web container at time of deploying the project. This object can be used to get configuration information from web.xml file. There is only one ServletContext object per web application. in the fallowing ways we can get servleet context object.

1. getServletContext() method of ServletConfig interface returns the object of ServletContext.
2. getServletContext() method of GenericServlet class returns the object of ServletContext.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Public void init()
{
ServletContext ctx=getServletContext();
}
Servlet context is one per web app.
Container makes a servlet context object when the web app is deployed on to the server... This object reference is provided throughout the web app using a method getServletContext()..
When container calls init(ServletConfig) the servlet context object is already there for you to use in your overloaded version init().....
 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:You've got a reference to a ServletConfig object (because it's a parameter of the init() method). A ServletConfig object has a getServletContext() method; if you call that method, what do you suppose it returns?

(Hint: Notice how "ServletConfig" is a link in your post? Click on that link and you'll see the API documentation for it.)




You are damn on target.. how could i forget that!!
 
All of the following truths are shameless lies. But what about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic