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

5
  • 1
    Which version of servlets you are using? (2.3, 3.0 etc)? Commented Aug 18, 2013 at 12:28
  • @Kᴇʏsᴇʀ yes i did try that doesn't work error is : The method getServletContext() is undefined for the type HttpServletRequest Commented Aug 18, 2013 at 12:30
  • @JavaStudent For future reference, that's highly relevant :) Put it in your question. Commented Aug 18, 2013 at 12:32
  • @JavaStudent, if you are using servlet 3.0, that method is available in ServletRequest. Commented Aug 18, 2013 at 13:01
  • @PradeepSimha answer of Nishant Shreshth was exactly what was the problem . Commented Aug 18, 2013 at 13:15

2 Answers 2

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(); 
Sign up to request clarification or add additional context in comments.

1 Comment

you mean my class IS-A HttpServlet , so just can use getServletContext() ? right
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

Comments