JSP編程實(shí)現(xiàn)用戶自動(dòng)登錄功能示例代碼
文章主要介紹了jsp實(shí)現(xiàn)用戶自動(dòng)登錄功能,文中示例代碼介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下.理解并掌握Cookie的作用以及利用cookie實(shí)現(xiàn)用戶的自動(dòng)登錄功能,實(shí)現(xiàn)下圖效...
文章主要介紹了jsp實(shí)現(xiàn)用戶自動(dòng)登錄功能,文中示例代碼介紹的非常詳細(xì),感興趣的小伙伴們可以參考一下.
理解并掌握Cookie的作用以及利用cookie實(shí)現(xiàn)用戶的自動(dòng)登錄功能,實(shí)現(xiàn)下圖效果
當(dāng)服務(wù)器判斷出該用戶是首次登錄的時(shí)候,會(huì)自動(dòng)跳轉(zhuǎn)到登錄界面等待用戶登錄,并填入相關(guān)信息。通過設(shè)置Cookie的有效期限來保存用戶的信息,關(guān)閉瀏覽器后,驗(yàn)證是否能夠自動(dòng)登錄,若能登錄,則打印歡迎信息;否則跳轉(zhuǎn)到登錄頁(yè)面。
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("只能選擇一個(gè)選項(xiàng)");
return false;
}
};
};
</script>
</head>
<body>
<!-- 設(shè)置html頁(yè)面 -->
<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("當(dāng)前\""+val+"\"用戶可自動(dòng)登錄");
%>
</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頁(yè)面讀取
session.setAttribute("name", name);
//設(shè)置Cookie
Cookie Cookie = new Cookie("name",name);
//根據(jù)提交選項(xiàng)設(shè)置cookie保存時(shí)間
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{%>
<%--用戶名不存在則進(jìn)行判斷是否已有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("您還沒有注冊(cè),2秒后轉(zhuǎn)到注冊(cè)界面!");
response.setHeader("refresh","2;url=login.jsp");
%>
如果沒有自動(dòng)跳轉(zhuǎn),請(qǐng)點(diǎn)擊<a href="login.jsp" rel="external nofollow" >此處</a>進(jìn)行跳轉(zhuǎn)
<%
//response.sendRedirect("login.jsp");
}
%>
</body>
</html>
實(shí)現(xiàn)效果:
1.
2.
3.
4.
5.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
ASP編程中Session對(duì)象失效的客戶端解決方法
雖然利用Timeout屬性釋放資源的策略是出于保護(hù)服務(wù)器的目的,但是Session對(duì)象不可預(yù)知的失效性,卻成為開發(fā)應(yīng)用程序的一個(gè)弊病。因而在實(shí)際應(yīng)用程序的開發(fā)中,必須解決Session對(duì)象失效的問題。...
Sphero推出編程教學(xué)機(jī)器人Bolt 配備紅外交互+LED點(diǎn)陣顯示屏
Sphero 剛剛推出了名叫 Bolt 的新款教學(xué)機(jī)器人,與此前推出的 Spark+ 相比,Bolt 最大的特色,就是配備了紅外傳感器和可編程的 LED 點(diǎn)陣顯示屏。在完成任務(wù)的時(shí)候,Bolt 可以顯示一...
哪本書才是編程領(lǐng)域的“九陰真經(jīng)”
前幾天在公眾號(hào)調(diào)查了下“對(duì)自己編程影響最大的一本書是什么”,答案基本不出我所預(yù)料:SICP、HTDP、 K & R C、Hackers and Painters 占據(jù)上風(fēng)。還有幾位說是譚老師的《C語(yǔ)言程序設(shè)計(jì)》,仔細(xì)想想,也算是入門必讀書籍...
極限黑客機(jī)械鍵盤 分離式外觀支持開源編程
國(guó)外眾籌網(wǎng)站Indiegogo上架了一款神奇的機(jī)械鍵盤,其最大的特點(diǎn)就是支持開源編程,而且能夠直接從中間“撕成兩半”。...
優(yōu)必選發(fā)Alpha 1P教育機(jī)器人:人形可編程
10月12日,國(guó)內(nèi)知名機(jī)器人公司優(yōu)必選(UBTECH)舉辦新品發(fā)布會(huì),正式發(fā)布全新Alpha1P人形可編程教育機(jī)器人。據(jù)悉,Alpha1P將于今日在天貓正式啟動(dòng)...
喬布斯:每個(gè)人都應(yīng)該花1年時(shí)間學(xué)習(xí)編程
當(dāng)人們談到蘋果前CEO喬布斯時(shí),總是會(huì)聯(lián)想到文青、嬉皮、鮑勃·迪倫和披頭士等等這些。但喬布斯也有另一面,比方說喬布斯曾經(jīng)認(rèn)真學(xué)習(xí)過編程,并曾在訪談中表示,每個(gè)人都...
編程語(yǔ)言那么多,為什么Google獨(dú)愛JS?
我從事軟件開發(fā)的相關(guān)工作已經(jīng)有15年了。目前的工作主要集中于Web和移動(dòng)應(yīng)用方面。在這么多年里,我對(duì)JavaScript的態(tài)度可能比較能代表一大群程序員的看法:從一開始對(duì)js的...
Java誕生二十周年:回顧編程世界主宰的成長(zhǎng)歷程
早在上世紀(jì)九十年代初就業(yè)已誕生的消費(fèi)級(jí)電子實(shí)驗(yàn)成果已經(jīng)于本周迎來了其作為企業(yè)級(jí)計(jì)算領(lǐng)域主要解決方案的第二十個(gè)年頭。毫無疑問,Java已經(jīng)成為一款占據(jù)主導(dǎo)地位的平臺(tái)...