Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java : ServletContext not avaliable in Eclipse (IDE FOR EE)

Tags:

java

jsp

servlets

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 Answers

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();

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

like image 24
sanbhat Avatar answered Dec 15 '25 07:12

sanbhat