Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext object?

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

How can we access the servletcontext object from the HttpServlet??
 
Hendy Setyo Mulyo
Ranch Hand
Posts: 219
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just simply call getServletContext().XXX
 
Mahesh Desai
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javax.servlet.http.HttpServlet extends from javax.servlet.GenericServlet.
javax.servlet.GenericServlet implements the ServletConfig interface.
ServletConfig interface has getServletContext() method which returns a reference to the ServletContext in which the caller is executing.

So as Hendy said, you just need to call getServletContext() method.


Thanks,
Mahesh

-----------
SCJP 1.4 | SCWCD 1.4 | SCBCD 1.3 | SCEA Part I - In Progress
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even this is possible..

getServletConfig().getServletContext().getXXXX();
 
Neo Phesus
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Jothi Sankar...I am just curious to know what was the cause of that question or which immediate action made you ask the question?

Just that your thought process may kindle some more questions in my mind As it usually does with other questions...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic