application: 容器级别,关闭或停止容器,配置的数据才会清除
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ServletContext application = this.getServletContext();
application.setAttribute("username", "yuanzhong");
application.setAttribute("login", "老头");
System.out.println(this.getServletContext().getInitParameter("user"));
System.out.println("appliaction=username====="+this.getServletContext(). getAttribute("username"));
System.out.println("appliaction=login====="+this.getServletContext(). getAttribute("login"));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter outPrintWriter = resp.getWriter();
HttpSession session = req.getSession();
session.setAttribute("s_name", "明天还要上班");
System.out.println("session==========="+req.getSession().getAttribute("s_name"));
outPrintWriter.write("<br />");
outPrintWriter.write("session==========="+req.getSession().getAttribute("s_name"));
}
request: 请求级别,请求过程中有效,请求结束失效;
/*request*/
req.setAttribute("r_name", "乐凡子");
System.out.println("request==============="+req.getAttribute("r_name"));
outPrintWriter.write("<br />request==============="+req.getAttribute("r_name"));