java application session request 使用示例

来源:互联网 发布:html5程序员招聘 编辑:程序博客网 时间:2024/05/21 23:15


application:   容器级别,关闭或停止容器,配置的数据才会清除

@Overrideprotected 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"));}


session:    浏览器级别,在没有任何情况的情况下,机制会根据session-tiemout 时间,自动清除session中的数据; 

@Overrideprotected 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"));


0 0
原创粉丝点击