Servlet中的getServletContext的使用注意事项
Servlet中的getServletContext的使用注意事项 在doget或dopost中直接获取ServletContext对象 如果Servlet重写了带有ServletConfig参数的init方法,那么调用getServletContext()时会引发空指针异常 在doget或dopost中直接获取ServletContext对象 getServletContext()方法是ServletConfig接口定义的方法,在servlet中可以直接调用getServletContext()来获取ServletContext对象 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("上下文参数:"+getServletContext().getInitParameter("age")); } 如果Servlet重写了带有ServletConfig参数的init方法,那么调用getServletContext()时会引发空指针异常 @Override public void init(ServletConfig config) throws