jsp實(shí)現(xiàn)簡(jiǎn)單用戶7天內(nèi)免登錄
本文實(shí)例為大家分享了jsp實(shí)現(xiàn)簡(jiǎn)單用戶7天內(nèi)免登錄的具體代碼,供大家參考,具體內(nèi)容如下
(1)登陸頁(yè)面:login.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html> <html> <head> <meta charset="utf-8"> <title>insert title here</title> </head> <body> <form action="loginservlet" method="post"> 用戶名:<input type="text" name="username" value="${cookie.cook_name.value }"/><br/> <input type="checkbox" name="chkcookie" value="7"/>記住我,7天內(nèi)免登錄<br/> <input type="submit" value="登錄"/><br/> <a href="sessionuser.jsp" rel="external nofollow" >驗(yàn)證session</a> <a href="cookieuser.jsp" rel="external nofollow" >驗(yàn)證cookie</a> </form> </body> </html>
(2)跳轉(zhuǎn)頁(yè)面:cookieuser.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <!doctype html> <html> <head> <meta charset="utf-8"> <title>insert title here</title> </head> <body> <% //從瀏覽器獲取cookie cookie[] cookies = request.getcookies(); string uname = null; //判斷語(yǔ)句條件小技巧 if(null != cookies){ for(cookie cookie:cookies){ //遍歷本地瀏覽器中的所有cookie if(cookie.getname().equals("cook_name")){ //把cookie中的用戶名取出 uname = cookie.getvalue(); } //out.print("<p>name: " + cookie.getname()+"</p>"); //out.print("<p>value: " + cookie.getvalue()+"</p>"); } } //判斷cookie有沒(méi)有失效 if(null != uname){ out.print("<p>用戶名:"+uname +"</p>"); } else{ response.sendredirect("login.jsp"); } %> </body> </html>
(3)servlet文件:loginservlet類
package com.servlet; import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.cookie; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession; /** * servlet implementation class loginservlet */ @webservlet("/loginservlet") public class loginservlet extends httpservlet { private static final long serialversionuid = 1l; /** * @see httpservlet#httpservlet() */ public loginservlet() { super(); // todo auto-generated constructor stub } /** * @see httpservlet#doget(httpservletrequest request, httpservletresponse response) */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub //獲取用戶登錄名 string username = request.getparameter("username"); //獲取7天內(nèi)是否免登錄 string day7 = request.getparameter("chkcookie"); int cookie_day = 1; if(null != day7 && day7.equals("")){ cookie_day = integer.valueof(day7); } //創(chuàng)建cookie cookie cookie = new cookie("cook_name",username); //設(shè)置cookie有限期 //cookie.setmaxage(6);//有效期6秒 cookie.setmaxage(3600 * 24 * cookie_day);//設(shè)置7天免登錄 //寫(xiě)入cookie到瀏覽器 response.addcookie(cookie); response.sendredirect("cookieuser.jsp"); } /** * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response) */ protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { // todo auto-generated method stub doget(request, response); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持碩編程。
相關(guān)文章
- jsp+servlet實(shí)現(xiàn)文件上傳與下載功能
- EJB3.0部署消息驅(qū)動(dòng)Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法
- 秒殺系統(tǒng)Web層設(shè)計(jì)的實(shí)現(xiàn)方法
- 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法
- JSP使用過(guò)濾器防止Xss漏洞
- 在JSP頁(yè)面中動(dòng)態(tài)生成圖片驗(yàn)證碼的方法實(shí)例
- 詳解JSP 內(nèi)置對(duì)象request常見(jiàn)用法
- 使用IDEA編寫(xiě)jsp時(shí)EL表達(dá)式不起作用的問(wèn)題及解決方法
- jsp實(shí)現(xiàn)局部刷新頁(yè)面、異步加載頁(yè)面的方法
- Jsp中request的3個(gè)基礎(chǔ)實(shí)踐
- JavaServlet的文件上傳和下載實(shí)現(xiàn)方法
- JSP頁(yè)面的靜態(tài)包含和動(dòng)態(tài)包含使用方法