文章主要詳細介紹了jsp實現簡單使用者7天內免登入,文中示例程式碼介紹的非常詳細,感興趣的小夥伴們可以參考一下。
本文例項為大家分享了jsp實現簡單使用者7天內免登入的具體程式碼,供大家參考,具體內容如下
(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天內免登入<br/>
<input type="submit" value="登入"/><br/>
<a href="sessionUser.jsp" rel="external nofollow" >驗證session</a>
<a href="cookieUser.jsp" rel="external nofollow" >驗證cookie</a>
</form>
</body>
</html>
(2)跳轉頁面: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天內是否免登入
String day7 = request.getParameter("chkCookie");
int cookie_day = 1;
if(null != day7 && day7.equals("")){
cookie_day = Integer.valueOf(day7);
}
//建立cookie
Cookie cookie = new Cookie("cook_name",username);
//設定Cookie有限期
//cookie.setMaxAge(6);//有效期6秒
cookie.setMaxAge(3600 * 24 * cookie_day);//設定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);
}
}
以上就是JSP程式設計實現簡單使用者7天內免登入及示例程式碼的全部內容,希望對大家的學習有所幫助。