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有沒有失效 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天免登錄 //寫入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í)有所幫助,也希望大家多多支持腳本之家。
- jsp+dao+bean+servlet(MVC模式)實(shí)現(xiàn)簡(jiǎn)單用戶登錄和注冊(cè)頁(yè)面
- 使用JSP實(shí)現(xiàn)簡(jiǎn)單的用戶登錄注冊(cè)頁(yè)面示例代碼解析
- jsp實(shí)現(xiàn)用戶自動(dòng)登錄功能
- servlet+jsp實(shí)現(xiàn)過(guò)濾器 防止用戶未登錄訪問(wèn)
- JSP Spring防止用戶重復(fù)登錄的實(shí)現(xiàn)方法
- JavaWeb實(shí)現(xiàn)用戶登錄注冊(cè)功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
- JSP實(shí)現(xiàn)用戶登錄、注冊(cè)和退出功能
- jsp基于XML實(shí)現(xiàn)用戶登錄與注冊(cè)的實(shí)例解析(附源碼)
- JSP實(shí)現(xiàn)簡(jiǎn)單的用戶登錄并顯示出用戶信息的方法
- 在jsp中用bean和servlet聯(lián)合實(shí)現(xiàn)用戶注冊(cè)、登錄
- 關(guān)于JSP用戶登錄連接數(shù)據(jù)庫(kù)詳情
相關(guān)文章
jsp 對(duì)request.getSession(false)的理解(附程序員常疏忽的一個(gè)漏洞)
在網(wǎng)上經(jīng)??吹接腥藢?duì)request.getSession(false)提出疑問(wèn),我第一次也很迷惑,看了一下J2EE1.3 API,看一下官網(wǎng)是怎么解釋的。2009-07-07JSP使用Common FileUpload組件實(shí)現(xiàn)文件上傳及限制上傳類型實(shí)例代碼
這篇文章主要介紹了JSP使用Common FileUpload組件實(shí)現(xiàn)文件上傳及限制上傳類型實(shí)例代碼,需要的朋友可以參考下2017-09-09關(guān)于jsp中cookie丟失問(wèn)題(詳解)
下面小編就為大家?guī)?lái)一篇關(guān)于jsp中cookie丟失問(wèn)題(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05JavaScript實(shí)現(xiàn)城市選擇控件的效果
本文介紹了JavaScript實(shí)現(xiàn)城市選擇控件的效果,支持直接輸入城市名稱,拼音首字母,全拼,有需要的朋友可以了解一下。2016-10-10實(shí)戰(zhàn) J2EE 開發(fā)購(gòu)物網(wǎng)站 二
實(shí)戰(zhàn) J2EE 開發(fā)購(gòu)物網(wǎng)站 二...2006-10-10建立JSP操作以提高數(shù)據(jù)庫(kù)訪問(wèn)的效率
建立JSP操作以提高數(shù)據(jù)庫(kù)訪問(wèn)的效率...2006-10-10