jsp实现用户自动登录功能

所属分类: 网络编程 / JSP编程 阅读数: 752
收藏 0 赞 0 分享

理解并掌握Cookie的作用以及利用cookie实现用户的自动登录功能,实现下图效果

当服务器判断出该用户是首次登录的时候,会自动跳转到登录界面等待用户登录,并填入相关信息。通过设置Cookie的有效期限来保存用户的信息,关闭浏览器后,验证是否能够自动登录,若能登录,则打印欢迎信息;否则跳转到登录页面。

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%request.setCharacterEncoding("GB2312"); %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >
 
 <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" rel="external nofollow" rel="external nofollow" >
 -->
 <script type="text/javascript">
 window.onload = function(){
  //获取submit
  var submit = document.getElementById("submit");
  var name = document.getElementById("name");
  //为submit绑定单击响应函数
  submit.onclick = function(){
  
  times = document.getElementsByName("time");
  var count=0;
  for(var i=0;i<times.length;i++){
   if(times[i].checked == true){
   count++;
   }
  }
  if(count>=2){
   alert("只能选择一个选项");
   return false;
  }
  
  }; 
  
 };
 
 </script>
 </head>
 
 <body>
 <!-- 设置html页面 -->
 <form action="sucess.jsp" method="post">
 用户名:<input name="username" /><br/>
  <input type="checkbox" name="time" value="notSave" />不保存
  <input type="checkbox" name="time" value="aDay" />一天
  <input type="checkbox" name="time" value="aWeek" />一周
  <input type="checkbox" name="time" value="forever" />永久
  <br/><br/>
  <input type="submit" name="submit" id="submit" value="登录"/>
 </form>
 <% 
 //读取session值
 String val= (String)session.getAttribute("name");
 //如果session不存在
 if(val==null){
  val ="不存在";
 }
 out.print("当前\""+val+"\"用户可自动登录");
 %>

 </body>
</html>

sucess.jsp

%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >
 
 <title>My JSP 'show.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" rel="external nofollow" rel="external nofollow" >
 -->

 </head> 
 <body> 
<%
 //获取username
 String name = request.getParameter("username");
 //判断用户名是否存在
 if(name != null && !name.trim().equals("")){ 
 String[] time = request.getParameterValues("time");
 //设置session值,便于login页面读取
 session.setAttribute("name", name);
 //设置Cookie
 Cookie Cookie = new Cookie("name",name);
 //根据提交选项设置cookie保存时间
 if(time != null){
  for(int i=0;i<time.length;i++){
  //不保存Cookie
  if(time[i].equals("notSave")){
   Cookie.setMaxAge(0); 
  }
  //保存一天Cookie
  if(time[i].equals("aDay")){
   Cookie.setMaxAge(60*60*24);
  }
  //保存一周Cookie
  if(time[i].equals("aWeek")){
   Cookie.setMaxAge(60*60*24*7);
  }
  //永久保存Cookie,设置为100年
  if(time[i].equals("forever")){
   Cookie.setMaxAge(60*60*24*365*100);
  }
  }
 }  
 
 //在客户端保存Cookie
 response.addCookie(Cookie);
 } 
 else{%>
  <%--用户名不存在则进行判断是否已有cookie --%>
 <%
 //获取cookie
 Cookie[] cookies = request.getCookies();
 
 //cookie存在
 if(cookies != null && cookies.length > 0){
  for(Cookie cookie:cookies){
  //获取cookie的名字
  String cookieName = cookie.getName();
  //判断是否与name相等
  if(cookieName.equals("name")){
   //获取cookie的值
   String value = cookie.getValue();
   name = value;
   }
  }
  }
 }
 if(name != null && !name.trim().equals("")){
 out.print("您好: " + name+"欢迎登录");
 }
 else{//否则重定向到登录界面
  out.print("您还没有注册,2秒后转到注册界面!");
 response.setHeader("refresh","2;url=login.jsp");
 %>
 如果没有自动跳转,请点击<a href="login.jsp" rel="external nofollow" >此处</a>进行跳转
 <%
 //response.sendRedirect("login.jsp");
 }
%>
 
 </body>
</html>

实现效果:

1.

2.

3.

4.

5.

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

更多精彩内容其他人还在看

jsp 使用jstl实现翻页实例代码

这篇文章主要介绍了jsp 使用jstl实现翻页实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Jsp中的table多表头导出excel文件具体实现

这篇文章主要介绍了Jsp中的table多表头导出excel文件具体实现,有需要的朋友可以参考一下
收藏 0 赞 0 分享

java(jsp)整合discuz同步登录功能详解

jsp整合discuz同步登录功能详解,Uenter是Comsenz旗下各个产品之间信息直接传递的一个桥梁,通过UCenter站长可以无缝整合Comsenz系列产品,Center拥有机制完善的接口,经过简单修改便可以挂接其它任何平台的第三方的网络应用程序
收藏 0 赞 0 分享

jsp页面传参乱码的解决方法

本篇文章主要是对jsp页面传参乱码的解决方法进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助
收藏 0 赞 0 分享

jsp分页显示的实现代码

这篇文章主要介绍了jsp分页显示的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Linux和Windows中tomcat修改内存大小的方法

Linux和Windows中tomcat修改内存大小的方法,可以利用JVM提供的-Xmn -Xms -Xmx等选项可进行设置,大家参考使用吧
收藏 0 赞 0 分享

使用maven+eclipse搭建struts2开发环境

Struts 2是Apache基金会的明星级产品,提供了对MVC的一个清晰的实现,下面就为大家介绍一下使用maven+eclipse搭建struts2开发环境的方法
收藏 0 赞 0 分享

jsp网页计数器实现示例

网页计数器想必大家都有见到过吧,记录每一个访问者,下面有个不错的示例,感兴趣的朋友可以参考下
收藏 0 赞 0 分享

jsp页面间传中文参数示例(页面传参数编码)

在url地址栏使用中文传参数可能会是乱码了,下面我们来看看正确的jsp中页面间传中文参数转码的方法
收藏 0 赞 0 分享

servlet分页代码示例

本文介绍了servlet分页代码实现,采用Oracle数据库,获取SCOTT用户EMP表中的数据,分页实现步骤看下面代码
收藏 0 赞 0 分享
查看更多