at javax.servlet.GenericServlet.getServletContext(GenericServlet.java:123)

Problem: 
When writing a servlet, when you want to get the ServletContext object in the doGet/doPost method, (for example: 
  ServletContext context=getServletContext(); 
       out.print(context.getServerInfo()); 
), the following exception will appear sometimes, and sometimes you can Sometimes it doesn't work. After searching for a long time, the problem can't be solved. 
java.lang.NullPointerException 
javax.servlet.GenericServlet.getServletContext(GenericServlet.java:159) 
servletdemo.FirstServlet.process(FirstServlet.java:51) 

searched the Internet for a long time and finally found it at http://forum.java.sun.com/ thread.jspa?threadID=558579&messageID=2742652 The problem was found under the prompt of the document. 
It turned out that I rewritten init(ServletConfig), but the first line did not call super.init(config); that's what caused the error! The init (ServletConfig) of the parent class handles the reference to obtain the ServletContext object, and the SeverletContext object can be obtained through the getServletContext() method only in methods such as doGet().
public void init(ServletConfig config) throws ServletException{ 
        super.init(config); 
        。。。。 
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326525414&siteId=291194637