内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

java使用Filter實現(xiàn)自動登錄的方法-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務(wù)器提供商,新人活動買多久送多久,劃算不套路!

10年積累的成都做網(wǎng)站、網(wǎng)站設(shè)計經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計制作后付款的網(wǎng)站建設(shè)流程,更有龍巖免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

本文實例為大家分享了java實現(xiàn)自動登錄的具體代碼,供大家參考,具體內(nèi)容如下

  • 當(dāng)你勾選(記住登錄狀態(tài)),用cookie保存用戶名和密碼。不勾選,cookie失效。
  • 所有的頁面都要經(jīng)過autoLoginFilter.java 的過濾器,在這類中,必須要判斷cookies不為null,獲得所有的cookie,得到name為user的cookie,進行用戶名和密碼的驗證,如果不為null,則將user存入session。
  • 在LoginServlet.java中,獲得username和password參數(shù),進行dao驗證,如果不為空,放入seesion中,進行頁面跳轉(zhuǎn)。
  • 創(chuàng)建cookie對象。setpath("/"),表示本應(yīng)用下的所有路徑都能訪問此cookie。
  • 對于已經(jīng)正確登錄的用戶,再次訪問其他頁面必定會再次經(jīng)過autoLoginFilter,這時,判斷當(dāng)前session中的user是否為null,不為null,直接通過。
  • 對于**login.jsp的有關(guān)頁面,不需要經(jīng)過autoLoginFilter。
package com.learning.web.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 com.learning.domain.User;
import com.learning.service.UserService;

@WebServlet("/servlet/loginServlet")
public class LoginServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  String username = request.getParameter("username");
  String password = request.getParameter("password");
  String autologin = request.getParameter("autologin");
  
  UserService userService=new UserService();
  User user = userService.findUser(username, password);
  //user不為null,則登錄成功
  if (user!=null) {
  //創(chuàng)建cookie來保存用戶信息
  Cookie cookie=new Cookie("user", user.getUsername()+"&"+user.getPassword());
  cookie.setPath("/");
  //autologin不為null,則記住了登錄狀態(tài)
  if (autologin!=null) {
   cookie.setMaxAge(1*60*60*24);//一天的有效時間
  }
  else {
   cookie.setMaxAge(0);
  }
  response.addCookie(cookie);
  request.getSession().setAttribute("user", user);
  request.getRequestDispatcher("/home.jsp").forward(request, response);
  }else {
  response.sendRedirect(request.getContextPath()+"/homeLogin.jsp");
  }
  
 }

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 doGet(request, response);
 }

}

文章題目:java使用Filter實現(xiàn)自動登錄的方法-創(chuàng)新互聯(lián)
標題路徑:http://www.rwnh.cn/article0/esgoo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、外貿(mào)建站、ChatGPT、App設(shè)計網(wǎng)站改版、手機網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)
长顺县| 东台市| 肇庆市| 新干县| 凯里市| 武清区| 繁峙县| 淮滨县| 安图县| 乌苏市| 靖边县| 离岛区| 萍乡市| 西藏| 湘潭县| 奉新县| 公主岭市| 清涧县| 镇宁| 怀化市| 长沙市| 漾濞| 渑池县| 闸北区| 班玛县| 灌阳县| 佛冈县| 宜丰县| 大化| 嘉善县| 庆云县| 南城县| 永平县| 北流市| 文化| 聂拉木县| 霍邱县| 丹江口市| 巴里| 车致| 清水县|