• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

how Init() access ServletContext

 
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 .
 
Marshal
Posts: 25677
69
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.)
 
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.
 
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!!
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic