繁体   English   中英

GetServletContext在扩展HttpServlet的类中返回null

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

因此,我有一个扩展HttpServlet的类,并且试图访问在已实现ServletContextListener的其他类中设置的变量。

但是,扩展HttpServlet的类中的以下代码行给了我NullPOinterException:

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

我不明白为什么会这样。 有任何想法吗?

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

暂无
暂无

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

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