利用JSP session对象保持住登录状态

 更新时间:2017年05月20日 16:44:50   作者:sunny1996  
这篇文章主要为大家详细介绍了如何利用JSP session对象保持住登录状态,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

http协议本身是一种无状态的协议,也就是客户端连续发送的多个请求之间没有联系,下一次请求不关心上一次请求的状态。

而实际运用中却希望服务器能记住客户端请求的状态,比如在网上购物系统中,服务器端应该能够识别并跟踪每个登录到系统中的用户挑选并购买商品的整个流程 。为此,web服务器必须采用一种机制来唯一地标识一个用户,同时记录该用户的状态,这就要用到会话跟踪技术。

Java Web使用Session来跟踪会话和管理会话内的状态。

Session对象是一个jsp内置对象,它在第一个jsp页面被装载时自动创建,完成会话期管理。

从一个客户打开浏览器并连接到服务器开始,到客户关闭浏览器离开这个服务器结束,被称为一个会话。当一个客户访问一个服务器时,可能会在这个服务器的几个页面之间反复连接,反复刷新一个页面,服务器应当通过某种办法知道这是同一个客户,这就需要session对象。

当发生以下四种情形其中之一时,session对象中的数据便会清空 :
用户关闭目前正在使用的浏览器程序。
关闭网页服务器。
用户未向服务器提出请求超过预设的时间,Tomcat服务器预设为30分钟。
运行程序结束session。

1.建立session变量

在JSP中不需要特别设置程序代码来建立用户session,当程序使用了session对象时,便会自动建立session,而下面这行语句便是在session中新增变量数据的方式:
session.setAttribute(“变量名称”,变量内容)
变量内容可为字符串或者其他对象类型,接着让我们来看看如何使用这个方法在session中设置变量数据:

<% 
session.setAttribute(“id”,”编号”); //设置字符串 
session.setAttribute(“expire”,new Date(86400*10)); //设置日期 
session.setAttribute(“level”,new Integer(3)); //设置整数 
%> 

2.返回session中的变量

在session中设置了变量数据后,在其他的各个网页中便可使用getAttribute读取其中的内容,此方法所返回的数据类型为对象(Object)类型,语法如下:
session.getAttribute(“变量名称”)

3.返回所有session中的变量名称

getAttributeNames()方法可以取出session中所有变量的名称,其结果为一个枚举类的实例。语法为:
session.getAttributeNames()

4.清除session中的变量
removeAttribute()方法可以清除session中的变量数据,使用语法如下:
session.removeAttribute(“变量名称”)

5.结束session

对于已经建立的session,可使用invalidate()方法将其结束,使用语法为:
session.invalidate()

其他的一些可能会用到的方法:

现在写一个实例:通过session来记录客户的登录状态:
index.jsp登录界面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
 <head>
  <title>$Title$</title>
 </head>
 <body>
 <form action="process_login.jsp" method="post">
  用户名:<input type="text" name="username">
  密码:<input type="text" name="password">
  <input type="submit" value="submit">
  <input type="reset" value="reset">
 </form>
 <a href="page1.jsp" rel="external nofollow" rel="external nofollow" >1</a>
 <a href="page2.jsp" rel="external nofollow" rel="external nofollow" >2</a>
 <a href="page3.jsp" rel="external nofollow" rel="external nofollow" >3</a>
 </body>
</html>

process_login.jsp处理登录数据,这里知道输入密码是123都可以登录成功:

session.getAttribute()将会告诉page1.jsp文件这个用户是否登录成功了

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  String username=request.getParameter("username");
  String password=request.getParameter("password");
  if (password.equals("123")){
    session.setAttribute("username",username);
  }
  response.sendRedirect("page1.jsp");
%>

logout.jsp登出

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
  session.invalidate();
  response.sendRedirect("index.jsp");
%>

page1.jsp, page2.jsp, page3.jsp大同小异显示不同的页面,用于验证登录状态的记录(这里仅以page1.jsp为例):

注意这里的判断逻辑是一种很有趣的写法,把jsp代码和html代码完全融合起来了,不过我觉得这样写还是比较乱,宁愿只用一个jsp代码段,里面用out.println()在html中来实现显示不同的内容

这里主要是靠判断session.username是否为空来判断是否登录过了,并且传递相关的参数信息

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>Title</title>
</head>
<body>
  这里是Page1<br>
  <%if (session.getAttribute("username")==null){%>
  用户还没有登录
  <%}else {%>
  已登录,用户名:<%= session.getAttribute("username")%>
  <%}%>
  <br>
  <a href="page1.jsp" rel="external nofollow" rel="external nofollow" >page1</a>
  <a href="page2.jsp" rel="external nofollow" rel="external nofollow" >page2</a>
  <a href="page3.jsp" rel="external nofollow" rel="external nofollow" >page3</a>
  <a href="index.jsp" rel="external nofollow" >login</a>
  <a href="logout.jsp" rel="external nofollow" >logout </a>
</body>
</html>

可以看到,登录以后,不管跳转到哪个页面,用户的登录状态都没有丢失

一旦logout登出以后,session.invalidate()方法被调用,session被销毁,就跟踪不到用户的登录信息了

同时,如果我采用另一个浏览器访问同样的页面,以“2号用户”为username登录,也会一直记录到这个用户的登录信息(不过如果是同一种浏览器的话就不行了)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

最新评论