javaweb中的ServletContext对象

ServletContext:上下文对象,可用于多个servlet共享数据

public class ServletDemo01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        // 上下文对象,可用于多个servlet共享数据
        ServletContext context = this.getServletContext();
        context.setAttribute("username", "皮皮虾");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
posted @ 2021-01-30 21:03  皮皮龙喔  阅读(68)  评论(0)    收藏  举报