...
public class MyServlet extends HttpServlet {
      ServletConfig config;
      public void init(ServletConfig config) throws Exception {
            this.config = config;
      }      public void doPost(...)throws ...{
            ...
            this.getServletContext();//这里会报错 改为config.getServletContext()就可以了
            ...
      }
}我查过servlet的API,HttpServlet从GenericServlet继承了getServletContext(),init(),init(ServletConfig)方法 而我的MyServlet类继承至HttpServlet 但在MyServlet中从写了有参的init方法为什么就不能直接调用getServletContext()而要通过config才能取得ServletContext对象实例

解决方案 »

  1.   

    不应该覆盖init(ServletConfig config) ,而应覆盖init() 
    init(ServletConfig config) 中会自动调用init()
    或者在子类中这样写init(ServletConfig config) {super.init(config);)
      

  2.   

    type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception java.lang.NullPointerException
    javax.servlet.GenericServlet.getServletContext(GenericServlet.java:160)
    mvc.servlet.MvcServlet.doPost(MvcServlet.java:16)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.上面是出错信息 如果不重写有参数的init方法或重写有参的init并通过config取得ServletContext对象实例 则不会报错
      

  3.   

    此时this没完成初始化 为null
      

  4.   

    看不错您有什么必要覆盖有参数的init
    如果一定要这样,那改成下面的看一下
    ...
    public class MyServlet extends HttpServlet {
          ServletConfig config = getServletConfig();
          public void init(ServletConfig config) throws Exception {
                super.init(config);
    //            ..................你要初始化的处理
          }      public void doPost(...)throws ...{
                ...
                this.getServletContext();//这里会报错 改为config.getServletContext()就可以了
                ...
          }
      

  5.   

    ...
    public class MyServlet extends HttpServlet {
          ServletConfig config;
          public void init(ServletConfig config) throws Exception {
                this.config = config;
          }      public void doPost(...)throws ...{
                ...
                this.getServletContext();//这里会报错 改为config.getServletContext()就可以了
                ...
          }

    又看了一下,问题应该出现在this.config = config;这一句
    因为你覆盖了public void init(ServletConfig config) throws Exception ,原本GenericServlet中定义了
    一个private ServletConfig config,GenericServlet的getServletContext()方法也是通过传入的config
    的getServletContext方法得到ServletContext对象     
    现在该方法被复写后没有给父类的config赋值,导致在执行继承来的getServletContext(){config.getServletContext}
    方法时config(父类的中声明的那个config)为null而抛异常NullPointerException 
    ...
    public class MyServlet extends HttpServlet {
          ServletConfig config;//虽然这里你声名的和父类中的变量名一样,但是不会覆盖父类的私有config变量,
                               //父类的那个config只是在子类不可见
          public void init(ServletConfig config) throws Exception {
                super.init(config);//由于GenericServlet没有定义public方法对config赋值,
                                   //所以只能这样对那个private的config赋值
                this.config = config;//其实没有必要在此保存config,随时可以调用继承来的getServletConfig()
                                     //方法获取config
          }      public void doPost(...)throws ...{
                ...
                this.getServletContext();//这里会报错 改为config.getServletContext()就可以了
                ...
          }