简体   繁体   English

GetServletContext在扩展HttpServlet的类中返回null

[英]GetServletContext returning null in a class that extends HttpServlet

So I have a class that extends HttpServlet and I was trying to access variables set in different class that has implemented ServletContextListener. 因此,我有一个扩展HttpServlet的类,并且试图访问在已实现ServletContextListener的其他类中设置的变量。

However the following line of code in the class that extends HttpServlet is giving me the NullPOinterException: 但是,扩展HttpServlet的类中的以下代码行给了我NullPOinterException:

SomeObject x =  getServletContext().getAttribute("x");

I don't understand why this is happening. 我不明白为什么会这样。 Any ideas? 有任何想法吗?

可能是您在Servlet类中重写了init(ServletConfig cfg)方法,并且没有调用super.init(cfg)进行标准设置,从而导致上下文为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM