本文實(shí)例為大家分享了jsp實(shí)現(xiàn)簡單用戶7天內(nèi)免登錄的具體代碼,供大家參考,具體內(nèi)容如下
(1)登陸頁面: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)頁面: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;
//判斷語句條件小技巧
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);
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- jsp+dao+bean+servlet(MVC模式)實(shí)現(xiàn)簡單用戶登錄和注冊(cè)頁面
- 使用JSP實(shí)現(xiàn)簡單的用戶登錄注冊(cè)頁面示例代碼解析
- jsp實(shí)現(xiàn)用戶自動(dòng)登錄功能
- servlet+jsp實(shí)現(xià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)簡單的用戶登錄并顯示出用戶信息的方法
- 在jsp中用bean和servlet聯(lián)合實(shí)現(xiàn)用戶注冊(cè)、登錄
- 關(guān)于JSP用戶登錄連接數(shù)據(jù)庫詳情