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);
}
}

浙公网安备 33010602011771号