国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

jsp實現(xiàn)用戶自動登錄功能

瀏覽:9日期:2022-06-07 13:25:37

理解并掌握Cookie的作用以及利用cookie實現(xiàn)用戶的自動登錄功能,實現(xiàn)下圖效果

當服務(wù)器判斷出該用戶是首次登錄的時候,會自動跳轉(zhuǎn)到登錄界面等待用戶登錄,并填入相關(guān)信息。通過設(shè)置Cookie的有效期限來保存用戶的信息,關(guān)閉瀏覽器后,驗證是否能夠自動登錄,若能登錄,則打印歡迎信息;否則跳轉(zhuǎn)到登錄頁面。

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綁定單擊響應(yīng)函數(shù)  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> <!-- 設(shè)置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"); //設(shè)置session值,便于login頁面讀取 session.setAttribute("name", name); //設(shè)置Cookie Cookie Cookie = new Cookie("name",name); //根據(jù)提交選項設(shè)置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,設(shè)置為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秒后轉(zhuǎn)到注冊界面!"); response.setHeader("refresh","2;url=login.jsp"); %> 如果沒有自動跳轉(zhuǎn),請點擊<a href="login.jsp" rel="external nofollow" >此處</a>進行跳轉(zhuǎn) <% //response.sendRedirect("login.jsp"); }%>  </body></html>

實現(xiàn)效果:

1.

2.

3.

4.

5.

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持。

標簽: JSP
相關(guān)文章:
主站蜘蛛池模板: 两性午夜视频 | 特黄aa级毛片免费视频播放 | 亚洲在线中文字幕 | 久久精品国产亚洲欧美 | 亚洲 欧美 国产 中文 | 中文字幕 日韩在线 | 色综合久久一本首久久 | 亚洲福利影院 | 玖玖爱zh综合伊人久久 | 成人久久视频 | 日韩国产欧美视频 | 久久一二 | a级黄色毛片免费播放视频 a级精品九九九大片免费看 | 欧美日韩一级片在线观看 | 国产一区二区三区手机在线观看 | 色综合久久88色综合天天提莫 | 欧美高清性色生活 | 国产男女爽爽爽爽爽视频 | 黄色三级视频在线 | 男人av的天堂| 国产亚洲欧美日韩在线观看不卡 | 一区二区三区亚洲视频 | 可以看毛片的网站 | 在线播放免费一级毛片欧美 | 在线观看99 | 一本三道a无线码一区v小说 | 岬奈一区二区中文字幕 | 美美女高清毛片视频黄的一免费 | 亚洲国产精品成人精品软件 | 国产手机视频 | 三级视频在线播放线观看 | 欧美成人三级网站在线观看 | 亚洲国产成人91精品 | 久久99国产精品免费观看 | 动漫一级毛片 | 国产99久久亚洲综合精品 | 亚洲国产精品一区二区九九 | 日韩三级黄 | 一级国产精品一级国产精品片 | 一级毛片视频播放 | 亚洲理论片在线中文字幕 |