3

I am trying to use ServletContext in my Servlet project as follows

ServletContext context  =request.getServletContext();

problem is that when i try to use it i dont find getServletContext(); for request object .

what i get is see in attachement

enter image description here

i am new to Servlets and just got it from video tutorial series , please guide me how do i get ServletContext(); for my applocation

2 Answers2

6

getServletContext() is available from HttpServlet class that your servlet extended. You can invoke the method as if it were defined in your own servlet class:

ServletContext context = getServletContext();
  • you mean my class IS-A HttpServlet , so just can use getServletContext() ? right –  Aug 18 '13 at 12:33
1

getServletContext() method is not defined for HttpServletRequest, you need to get it from HttpSession

OR

by simply calling getServletContext() within your Servlet

Please see this

Community
  • 1
  • 1
sanbhat
  • 17,522
  • 6
  • 48
  • 64