ServletContext(Servlet上下文对象),是web应用全局对象。
一个Web应用只会创建一个ServletContext对象
Java Web 三大作用域对象
HttpServletRequest - 请求对象
HttpSession - 用户会话对象
ServletContext - web应用全局对象
ServletContext context = request.getServletContext();
context.setAttribute("content", "© 2020 imooc.com 京ICP备 12003892号-11");
context.setAttribute("title", "<h1>幕课网</h1>");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("ServletContext 学习");
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ServletContext context = request.getServletContext();
String name = (String)context.getAttribute("content");
String title = (String)context.getAttribute("title");
response.setContentType("text/html;charset=utf-8");
response.getWriter().println("<h1>"+name+"</h1>"+title);