創(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)容如下
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)