好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > jsp實現用戶自動登錄功能

jsp實現用戶自動登錄功能

熱門標簽:vue 地圖標注拖拽 鎮江云外呼系統怎么樣 成都銷售外呼系統公司 客服外呼系統呼叫中心 土地證宗地圖標注符號 保定電銷機器人軟件 自動外呼系統怎么防止封卡 電話機器人案例 電話機器人銷售公司嗎

理解并掌握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;itimes.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;itime.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+dao+bean+servlet(MVC模式)實現簡單用戶登錄和注冊頁面
  • 使用JSP實現簡單的用戶登錄注冊頁面示例代碼解析
  • jsp實現簡單用戶7天內免登錄
  • servlet+jsp實現過濾器 防止用戶未登錄訪問
  • JSP Spring防止用戶重復登錄的實現方法
  • JavaWeb實現用戶登錄注冊功能實例代碼(基于Servlet+JSP+JavaBean模式)
  • JSP實現用戶登錄、注冊和退出功能
  • jsp基于XML實現用戶登錄與注冊的實例解析(附源碼)
  • JSP實現簡單的用戶登錄并顯示出用戶信息的方法
  • 在jsp中用bean和servlet聯合實現用戶注冊、登錄
  • 關于JSP用戶登錄連接數據庫詳情

標簽:重慶 麗江 內江 臺灣 成都 天津 公主嶺 懷化

巨人網絡通訊聲明:本文標題《jsp實現用戶自動登錄功能》,本文關鍵詞  jsp,實現,用戶,自動,登錄,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《jsp實現用戶自動登錄功能》相關的同類信息!
  • 本頁收集關于jsp實現用戶自動登錄功能的相關信息資訊供網民參考!
  • 推薦文章