你弄错了虚拟机和web容器的概念,你的测试程序是在虚拟机下运行的,不存在SERVLET容器,也不会自动执行init(),更不存在ServletContext了,你应该在将你的SERVLET布署在WEB容器里测试。

解决方案 »

  1.   

    java.lang.Object
      |
      +--javax.servlet.GenericServletpublic void init(ServletConfig config)
              throws ServletException
    Called by the servlet container to indicate to a servlet that the servlet is being placed into service. See Servlet.init(javax.servlet.ServletConfig). 
    This implementation stores the ServletConfig object it receives from the servlet container for later use. When overriding this form of the method, call super.init(config).Specified by: 
    init in interface Servlet
    Parameters:
    config - the ServletConfig object that contains configutation information for this servletjavax.servlet 
    Interface ServletConfig
    getServletContext
    public ServletContext getServletContext()
    Returns a reference to the ServletContext in which the caller is executing.