@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("初始化一些东西");
//super.init(config);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = request.getServletContext();//正常,不会出现异常
ServletContext servletContext1 = this.getServletContext();//出现空指针
}
原因很简单,就是重写 public void init(ServletConfig config)方法的时候没有调用父类,使得ServletCofig对象为null.