欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用Java servlet實(shí)現(xiàn)自動(dòng)登錄退出功能

 更新時(shí)間:2019年11月05日 10:09:05   作者:專注地一哥  
這篇文章主要介紹了使用Java servlet實(shí)現(xiàn)自動(dòng)登錄退出功能,,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

UserDao.java從數(shù)據(jù)庫(kù)中查詢用戶名與密碼

//登錄
public User login(User user) throws SQLException {
QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select from user where username = ? and password = ?";
return qr.query(sql, new BeanHandler<User>(User.class),user.getUsername(),user.getPassword());
}
UserService.java
public User login(User user){
try {
return ud.login(user);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

UserServlet.java實(shí)現(xiàn)登錄功能

//登錄
public void login(HttpServletRequest request,
HttpServletResponse response) throws IOException, IllegalAccessException, InvocationTargetException, ServletException{
Map<String,String[]> map = request.getParameterMap();
User user = new User();
BeanUtils.populate(user,map);
if (map.get("autoLogin")!=null){
Cookie username = new Cookie("username", map.get("username")[0]);
username.setMaxAge(6060);
Cookie password = new Cookie("password", map.get("password")[0]);
password.setMaxAge(60*60);
response.addCookie(username);
response.addCookie(password);
}
user = us.login(user);
if (user != null){
request.getSession().setAttribute("user", user);
response.sendRedirect("/ShopStore/default.jsp");
}
else{
request.setAttribute("message", "用戶或密碼錯(cuò)誤!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}

工具類:AutoLoginFilter.java用來(lái)實(shí)現(xiàn)自動(dòng)登錄

package com.yinhe.web.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.yinhe.bean.User;
import com.yinhe.service.UserService;
public class AutoLoginFilter implements Filter{br/>@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse resp = (HttpServletResponse)response;
//是否已登錄
if (req.getSession().getAttribute("user") == null){//如果已登錄,則啥都不干
//判斷cookie中有沒(méi)有賬戶密碼
Cookie[] cookies = req.getCookies();
if (cookies != null){
String username = "";
String userpass = "";
for (Cookie cookie : cookies) {
if (cookie.getName().equals("username")){//找到感興趣的cookie
username = cookie.getValue();
}
if (cookie.getName().equals("password")){//找到感興趣的cookie
userpass = cookie.getValue();
}
}
UserService us = new UserService();
User user = new User();
user.setUsername(username);
user.setPassword(userpass);
if (us.login(user) != null){
req.getSession().setAttribute("user", user);
}
}
}
chain.doFilter(request, response);br/>}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stubbr/>}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}

前臺(tái):login.jsp此單選框被選中下次訪問(wèn)即為自動(dòng)登錄

<div class="checkbox">
<label> <input type="checkbox" name="autoLogin" > 自動(dòng)登錄
</label>   <label> <input
type="checkbox" > 記住用戶名
</label>
</div>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>系統(tǒng)首頁(yè)</title>
<style>
#nav{
height: 88px;
padding: 5px 200px;
background-color: aquamarine;
}
#logo{
float: left;
}
#userinfo{
float: right;
height: 50px;
line-height: 80px;
}
#container{
background-color: aqua;
height: 800px;
margin: 2px 200px;
font-size: xx-large;
text-align: center;
}
</style>
</head>
<body>
<div id="nav">
<div id="logo">
<img src="csdn-logo.png" width="180" height="88">
</div>
<div id="userinfo">
<%-- 不推薦使用
<%
String loginUser = (String) request.getAttribute("loginUser");
if (loginUser != null){
out.println(loginUser);
}else {
%>
<a href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" >登錄</a><a>/注冊(cè)</a>
<%
}
%>
--%>
<%
String loginUser = (String) session.getAttribute("loginUser");
if (loginUser != null){
out.println(loginUser);
out.println("<a href='logoutServlet'>退出</a>");
}else {
%>
<a href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" >登錄</a><a>/注冊(cè)</a>
<%
}
%>
</div>
</div>
<div id="container">

主頁(yè)內(nèi)容

<a href="info.jsp" rel="external nofollow" >Python——畫一棵漂亮的櫻花樹(不同種櫻花+玫瑰+圣誕樹喔)</a>
</div>
</body>
</html>
login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登錄</title>
<style>
tr{
height:50px;
}
td{
text-align: center;
}
</style>
</head>
<body>
<%
String msg = (String)request.getAttribute("msg");
%>
<%
if(msg != null){
out.print(msg);
}
%>
<form action="myServlet02" method="get">
<h1 align="center">登錄</h1>
<table width="500" border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<td>賬號(hào):</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>密碼:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登錄" /></td>
</tr>
</table>
</form>
</body>
</html>

info.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<style>
#nav{
height: 88px;
padding: 5px 200px;
background-color: aquamarine;
}
#logo{
float: left;
}
#userinfo{
float: right;
height: 50px;
line-height: 80px;
}
#container{
background-color: aqua;
height: 800px;
margin: 2px 200px;
font-size: xx-large;
text-align: center;
}
</style>
</head>
<body>
<div id="nav">
<div id="logo">
<img src="csdn-logo.png" width="180" height="88">
</div>
<div id="userinfo">
<%
String loginUser = (String) session.getAttribute("loginUser");
if (loginUser != null){
out.println(loginUser);
out.println("<a href='logoutServlet'>退出</a>");
}else {
%>
<a href="login.jsp" rel="external nofollow" rel="external nofollow" rel="external nofollow" >登錄</a><a>/注冊(cè)</a>
<%
}
%>
</div>
</div>
<div id="container">
主頁(yè)內(nèi)容
</div>
</body>
</html>

Servlet02.java

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;br/>@WebServlet("/myServlet02")
public class Servlet02 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//修改編碼
request.setCharacterEncoding("utf-8");//請(qǐng)求過(guò)來(lái)的編碼是亨達(dá)返傭www.kaifx.cn/broker/hantecglobal.htmlutf-8
response.setContentType("text/html;charset=utf-8");//響應(yīng)出去的內(nèi)容,為網(wǎng)頁(yè)編碼utf-8
//獲取表單數(shù)據(jù)
String username = request.getParameter("username");
String password = request.getParameter("password");
//驗(yàn)證
if("aaa".equals(username) && "123".equals(password)){
//跳轉(zhuǎn)(請(qǐng)求轉(zhuǎn)發(fā) 請(qǐng)求重定向)
//重定向:兩次請(qǐng)求,不能在request作用域中共享數(shù)據(jù)。
//如果要在兩次請(qǐng)求或多次請(qǐng)求之間,進(jìn)行數(shù)據(jù)共享,需要用session
//使用session步驟
//獲取session
HttpSession session = request.getSession();//如果存在已有的session,則直接返回,否則會(huì)創(chuàng)建一個(gè)新的,返回。
//HttpSession session = request.getSession(true);//同上
//HttpSession session = request.getSession(false);//如果存在已有的session,則直接返回,否則返回null。
//在session的作用域保存數(shù)據(jù),供后續(xù)請(qǐng)求使用
session.setAttribute("loginUser",username);
response.sendRedirect("index.jsp");
/ 多個(gè)頁(yè)面不推薦使用請(qǐng)求轉(zhuǎn)發(fā)
request.setAttribute("loginUser",username);
request.getRequestDispatcher("index.jsp").forward(request,response);
/
}else {
//轉(zhuǎn)發(fā):一次請(qǐng)求,可以在request作用域中,共享數(shù)據(jù)
request.setAttribute("msg","<script>alert('登錄失?。?);</script>");
// response.sendRedirect("login.jsp");
request.getRequestDispatcher("login.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}

LogoutServlet.java

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;br/>@WebServlet("/logoutServlet")
public class LogoutServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//退出系統(tǒng)
//獲取session
HttpSession session = request.getSession();
//刪除session
session.removeAttribute("loginUser");
//跳轉(zhuǎn)到登錄頁(yè)面/首頁(yè)
response.sendRedirect("index.jsp");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

總結(jié)

以上所述是小編給大家介紹的使用Java servlet實(shí)現(xiàn)自動(dòng)登錄退出功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

  • 基于springboot實(shí)現(xiàn)整合shiro實(shí)現(xiàn)登錄認(rèn)證以及授權(quán)過(guò)程解析

    基于springboot實(shí)現(xiàn)整合shiro實(shí)現(xiàn)登錄認(rèn)證以及授權(quán)過(guò)程解析

    這篇文章主要介紹了基于springboot實(shí)現(xiàn)整合shiro實(shí)現(xiàn)登錄認(rèn)證以及授權(quán)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • Spring?Cloud?OAuth2實(shí)現(xiàn)自定義token返回格式

    Spring?Cloud?OAuth2實(shí)現(xiàn)自定義token返回格式

    Spring?Security?OAuth的token返回格式都是默認(rèn)的,但是往往這個(gè)格式是不適配系統(tǒng)。本文將用一個(gè)接口優(yōu)雅的實(shí)現(xiàn)?Spring?Cloud?OAuth2?自定義token返回格式,需要的可以參考一下
    2022-06-06
  • java實(shí)現(xiàn)連連看游戲課程設(shè)計(jì)

    java實(shí)現(xiàn)連連看游戲課程設(shè)計(jì)

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)連連看游戲課程設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • 淺談Java垃圾回收機(jī)制

    淺談Java垃圾回收機(jī)制

    Java 中,程序員不需要關(guān)心所有不再使用的對(duì)象。垃圾回收機(jī)制自動(dòng)銷毀這些對(duì)象。垃圾回收機(jī)制是守護(hù)線程的最佳示例,因?yàn)樗冀K在后臺(tái)運(yùn)行。垃圾回收機(jī)制的主要目標(biāo)是通過(guò)銷毀無(wú)法訪問(wèn)的對(duì)象來(lái)釋放堆內(nèi)存。下面我們就來(lái)詳細(xì)介紹吧
    2021-09-09
  • java逗號(hào)分隔String字符串及數(shù)組、集合相互轉(zhuǎn)換

    java逗號(hào)分隔String字符串及數(shù)組、集合相互轉(zhuǎn)換

    我們?cè)谌粘i_發(fā)時(shí)會(huì)經(jīng)常遇到將一個(gè)字符串按照逗號(hào)進(jìn)行分割,這篇文章主要給大家介紹了關(guān)于java逗號(hào)分隔String字符串及數(shù)組、集合相互轉(zhuǎn)換的相關(guān)資料,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2024-02-02
  • Eclipse中maven的配置詳解

    Eclipse中maven的配置詳解

    這篇文章主要介紹了Eclipse中maven的配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • Spring使用@Filter注解創(chuàng)建自定義過(guò)濾器

    Spring使用@Filter注解創(chuàng)建自定義過(guò)濾器

    Spring 中鮮為人知但非常有用的注解之一是 @Filter,它支持自定義過(guò)濾器,下面我們就來(lái)深入研究一下如何使用 Spring 的 @Filter 注解來(lái)創(chuàng)建自定義過(guò)濾器吧
    2023-11-11
  • 小議Java中@param注解與@see注解的作用

    小議Java中@param注解與@see注解的作用

    這篇文章主要介紹了Java中@param注解與@see注解的作用,注解的功能類似于通常代碼中的注釋,需要的朋友可以參考下
    2015-12-12
  • Java基于狀態(tài)模式實(shí)現(xiàn)的文檔編輯模式切換功能實(shí)例

    Java基于狀態(tài)模式實(shí)現(xiàn)的文檔編輯模式切換功能實(shí)例

    這篇文章主要介紹了Java基于狀態(tài)模式實(shí)現(xiàn)的文檔編輯模式切換功能,結(jié)合實(shí)例形式詳細(xì)分析了狀態(tài)模式的概念、原理及java使用狀態(tài)模式實(shí)現(xiàn)文檔編輯模式切換操作相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-05-05
  • 深入java垃圾回收的詳解

    深入java垃圾回收的詳解

    本篇文章是對(duì)java垃圾回收進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06

最新評(píng)論