java application session request 使用示例


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

	}


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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值