javaweb中的Session

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">1. 什么是Session</span>

Session 是服务器端保存会话的一种技术。

2. 怎么用?

如何获取Session?

HttpSession session=requeset.getSession();

如何操作Session?

session.setAttribute();
session.getAttribute();
sesson.removeAttribute();
session.getAttributeName();

3.Session 原理

浏览器第一次访问服务器的时候,服务器会开辟一片空间,用于存储Session。为了区别不同的浏览器,每一个Session都会对分配一个唯一的SessionID,这个SessionID 会保存在Cookie中发送到浏览器端。下次浏览器再次访问服务器的时候,拿着Cookie中的SessionID在服务器中找对对应的SessionID,并可以访问到对应的Session中保存的信息。注意,cookie是浏览器端保存信息的一种技术,Session是服务器端保存会话的一种技术。

4. Session中的细节问题。

一、服务器让浏览器记录SessionID的Cookie的默认时间是-1。就是浏览器关闭,Cookie就会销毁,对应的SessionID就会过期。

二、Session中除了操作Map外的其他操作。

<span style="white-space:pre">	</span>long getCreationTime()   获得创建时间
	String getId()    获得sessionID
	long getLastAccessedTime()   获得最后一次访问时间
	int  getMaxInactiveInterval()  获得session的寿命
	void setMaxInactiveInterval(int interval)   设置session的过期时间 
	void invalidate()   让session立即失效
	boolean isNew()  
三、关于设置Session的有效时间。

默认是30分钟,在Tomcat中的<session-config> 中配置.

如何修改Session的过期时间?

1、修改Tomcat中web.xml 中的<session-config>,会影响所有的项目

2、修改本项目中的web.xml  会影响这个项目中的所有session

3、通过session.setMaxInactiveInterval(); 只影响当前的Session。

5、例子。验证码。

登陆过程中的验证码问题。服务器通过随机生成四个字符,并用这四个字符生成一张图片。这四个字符保存在Session中。图片显示在浏览器端,用户识别验证码输入验证码,验证码和Session中的验证码比对,相同的话验证码通过。这里只是监测验证码是否正确。只是一个简单的小应用。

登陆jsp页面。

<html>
  <head>
    <title>My JSP 'login.jsp' starting page</title>

	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  <script type="text/javascript">
  	function fun1(){
  		
  		var img=document.getElementById("one");
  		img.src="/Home/AServlet?refresh"+new Date();
  	}
  </script>
  <body>
   <form action="/Home/BServlet" method="post">
	    用户名:	 <input type="text" name="username" /><br/>
	    密 码: <span style="white-space:pre">	</span> <input type="password" name="pwd"/><br/>
	    记住用户名	 <input type="checkbox" value="yes" name="remeber"/><br/>
	    验证码:	 <input type="text" name="code"/><img src="/Home/AServlet"  width="80" height="30" id="one" />
	    <span style="white-space:pre">		</span> <a href="jvascript:void(0)" οnclick=fun1()>看不清,刷新</a>
	   <span style="white-space:pre">		</span> <font  color="red"><%=request.getAttribute("error")==null?"":request.getAttribute("error") %></font><br/>
	   <span style="white-space:pre">		</span> <input type="submit" value="登陸 "/><br/>
  </form>
  
  </body>
</html>
Aservlet中代码

public class AServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获得验证码
		ValidateCode code = new ValidateCode(100,40,4,100);
		//将验证码存储在session域中
		System.out.println(code.getCode());
		request.getSession().setAttribute("code", code.getCode());
		//将验证码显示到浏览器中
		response.setContentType("image/jpeg"); //设置显示头
		code.write(response.getOutputStream());
	}

}

BServlet中

public class BServlet extends HttpServlet {

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//1 获得表单提交的验证码
		String code1 = request.getParameter("code");
		//2 获得session中的正确验证码
		String code2 = 	(String) request.getSession().getAttribute("code");
		//3 比对是否一致
		if(code1!=null && code2!=null && code1.equals(code2)){
			//正确==> 成功页面
			response.sendRedirect("/Home/index.jsp");
		}else{	
			//不正确==> 回到表单 页面 ,提示错误
			request.setAttribute("error", "验证码错误!请重新输入");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
		}
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值