Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how Init() access ServletContext

 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • 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 .
 
Paul Clapham
Sheriff
Posts: 21892
36
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.)
 
sreenivasarao mydukuri
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • 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.
 
Manish Kapasiya
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
  • 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!!
 
Crusading Chameleon likes the size of this ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!