方法一:通过GenericServlet提供的 getServletContext()
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext3 = getServletContext();
}
方法二:通过ServletConfig提供的getServletContext()
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext2 = getServletConfig().getServletContext();
}
方法三:通过HttpServletRequest获取
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext1 = req.getServletContext();
}
方法四:通过HttpSession获取。
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = req.getSession().getServletContext();
}