亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java web實(shí)現(xiàn)賬號(hào)單一登錄,防止同一賬號(hào)重復(fù)登錄(踢人效果)

 更新時(shí)間:2019年10月23日 15:44:33   作者:老孫的博客  
這篇文章主要介紹了Java web實(shí)現(xiàn)賬號(hào)單一登錄,防止同一賬號(hào)重復(fù)登錄,有點(diǎn)類似于qq登錄踢人效果,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

實(shí)現(xiàn)了Java web開(kāi)發(fā)賬號(hào)單一登錄的功能,防止同一賬號(hào)重復(fù)登錄,后面登錄的踢掉前面登錄的,使用過(guò)濾器Filter實(shí)現(xiàn)的??梢韵认螺d項(xiàng)目下來(lái)測(cè)試下效果。

有博客寫的是沒(méi)個(gè)一段時(shí)間(比如500ms)讀取后臺(tái)的session進(jìn)行驗(yàn)證,這種方法除了會(huì)占用資源,還會(huì)出現(xiàn)訪問(wèn)session(請(qǐng)求1)的返回值和自己提交請(qǐng)求(請(qǐng)求2)的返回值發(fā)生沖突。比如請(qǐng)求1先提交,此時(shí)請(qǐng)求1的返回值還未返回到前端,請(qǐng)求2提交,實(shí)際上我們想要的是請(qǐng)求1的返回值先返回,然后再返回請(qǐng)求2的返回值,但是這不是肯定會(huì)發(fā)生的,ajax的機(jī)制所導(dǎo)致的,具體什么原因沒(méi)查到??傊霈F(xiàn)了請(qǐng)求2先返回了返回值,這時(shí)候請(qǐng)求1接收到了請(qǐng)求2的返回值,妥妥的前端出現(xiàn)錯(cuò)誤,但是后臺(tái)卻是成功的。

下面進(jìn)入主題

工程下載鏈接:鏈接: https://pan.baidu.com/s/1Rp09wv7hTJLqx9DiQ_KSeA 提取碼: xyym

其中:jquery-1.11.3.js是網(wǎng)上的工具

在這里插入圖片描述

建立兩個(gè)簡(jiǎn)單的界面

登錄界面:為了簡(jiǎn)單沒(méi)有設(shè)置密碼,直接輸入賬號(hào)點(diǎn)擊登錄就行

在這里插入圖片描述

// index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >
 <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"> 
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" >
 -->
 </head>
 
 <body>
 <input id="username" name="username" type="text">
 <!-- <a href="singlecount.jsp" rel="external nofollow" target="_self"> -->
 <button id="btnlogin" name="btnlogin">登錄</button><!-- </a> -->
 
 <!-- 引入jQuery -->
 <script type="text/javascript" src="js/jquery-1.11.3.js"></script>
 <script type="text/javascript" src="js/jsSubmit.js"></script>
 </body>
</html>

主頁(yè)面:簡(jiǎn)單的一個(gè)提交按鈕

在這里插入圖片描述

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
 <base href="<%=basePath%>" rel="external nofollow" rel="external nofollow" >
 <title>My JSP 'SingleCount.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0"> 
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" rel="external nofollow" >
 -->

 </head>
 
 <body>
 已登錄. <br>
 <button id="btnsubmit" name="submit">提交</button>
 
 <!-- 引入jQuery -->
 <script type="text/javascript" src="js/jquery-1.11.3.js"></script>
 <script type="text/javascript" src="js/jsSubmit.js"></script>
 </body>
</html>

寫ajax,向后臺(tái)提交請(qǐng)求

// jsSubmit.js

$(document).ready(function() {
 // 登錄按鈕
 $("#btnlogin").click(function() {
 //data,dataType,type,url
 $.ajax({
 url: 'LoginServlet?method=login',
 type: 'post',
 data: {username: $("input[name='username']").val()}, // 將用戶名傳給servlet
 //dataType:'json',
 success: function(msg) { // msg為從servlet接收到的返回值
 if (msg == 1) { // 接收到后臺(tái)數(shù)據(jù)為1,正常登錄
 window.location.href = "singlecount.jsp";
 } 
 },
 error:function(){
 window.alert("錯(cuò)誤!");
 }
 });
 });
 // 提交按鈕
 $("#btnsubmit").click(function() {
 //data,dataType,type,url
 $.ajax({
 url: 'SubmitServlet?method=submit',
 type: 'post',
 //dataType:'json',
 success: function(msg) { // msg為從servlet接收到的返回值
 if (msg >= 1) { // 正常
 window.alert("提交總數(shù)" + msg);
 } 
 },
 error:function(jqXHR){
 if(jqXHR.status == 900){ // 900狀態(tài)碼
 window.alert("登錄狀態(tài)失效,請(qǐng)重新登錄!");
 window.location.href = "/OneLogin";
 }
 }
 });
 });
});

servlet

這部分有點(diǎn)長(zhǎng),其實(shí)主要內(nèi)容直接看doPost方法就可以了。

// LoginServlet
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletConfig;
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;
@SuppressWarnings("serial")
//注解表明什么樣的情況下可以訪問(wèn)該內(nèi)容
@WebServlet(urlPatterns={"/LoginServlet"})
public class LoginServlet extends HttpServlet {
 private PrintWriter out; // 輸出流
 private String user;
 private String method;
 private HttpSession session;
 // 建立一個(gè)Map存儲(chǔ)session信息,key-用戶名,value-session
 public static Map<String, HttpSession> user_Session = new HashMap<String, HttpSession>();
 
 @Override
 public void init(ServletConfig config) throws ServletException {
 // TODO Auto-generated method stub
 super.init(config);
 }
 @Override
 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 // TODO Auto-generated method stub
 super.doDelete(req, resp);
 }
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 // TODO Auto-generated method stub
 doPost(req, resp);
 }
 @Override
 // 在這里實(shí)現(xiàn)方法
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 // TODO Auto-generated method stub
 resp.setContentType("text/html");
 //語(yǔ)言編碼
 req.setCharacterEncoding("utf-8");
 resp.setCharacterEncoding("utf-8");
 out = resp.getWriter();
 
 user = req.getParameter("username"); // 獲取index界面username的內(nèi)容
 method = req.getParameter("method"); // 獲取方法名
 session = req.getSession(); // 獲取session
 switch (method) {
 case "login":
 mLogin();
 break;
 default:
 break;
 }
 out.flush();
 out.close();
 
 }

 private void mLogin() { // 按登錄按鈕調(diào)用的方法
 // TODO Auto-generated method stub
 removeUser(user);
 session.setAttribute("name", user);
 user_Session.put(user, session); // 新增或覆蓋session
 System.out.println(user_Session);
 out.println(1); // 返回值1,隨意選個(gè)值,和前端對(duì)應(yīng)就可以
 }
 
 /**
 * 判斷是否有重復(fù)用戶,
 * 若出現(xiàn)重復(fù)用戶,踢掉前面登錄的用戶,即刪除其session
 */
 private void removeUser(String user) {
 if(user_Session.containsKey(user))
 user_Session.get(user).invalidate();
 }
}

// SubmitServlet
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
//注解表明什么樣的情況下可以訪問(wèn)該內(nèi)容 會(huì)在js和web.xml中使用
@WebServlet(urlPatterns={"/SubmitServlet"})
public class SubmitServlet extends HttpServlet {
 
 private PrintWriter out; // 輸出流
 private String method;
 private int number = 0; // 計(jì)數(shù)
 @Override
 public void init(ServletConfig config) throws ServletException {
 // TODO Auto-generated method stub
 super.init(config);
 }
 @Override
 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 // TODO Auto-generated method stub
 super.doDelete(req, resp);
 }
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 // TODO Auto-generated method stub
 doPost(req, resp);
 }
 @Override
 // 在這里實(shí)現(xiàn)方法
 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
 // TODO Auto-generated method stub
 resp.setContentType("text/html");
 //語(yǔ)言編碼
 req.setCharacterEncoding("utf-8");
 resp.setCharacterEncoding("utf-8");
 out = resp.getWriter();
 
 method = req.getParameter("method"); // 獲取方法名
 switch (method) {
 case "submit":
 mSubmit();
 break;
 default:
 break;
 }
 out.flush();
 out.close();
 }
 
 private void mSubmit() { // 按提交按鈕調(diào)用的方法
 // TODO Auto-generated method stub
 number++;
 out.println(number);
 }
}

過(guò)濾器

過(guò)濾器的原理這里就不說(shuō)了,簡(jiǎn)單來(lái)說(shuō)就是請(qǐng)求要先經(jīng)過(guò)過(guò)濾器才能到達(dá)servlet,也就是說(shuō)如果請(qǐng)求不滿足要求就無(wú)法通過(guò)過(guò)濾器,這里的要求是要有session。

package 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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebFilter("/SessionFilter")
public class SessionFilter implements Filter {
 @Override
 public void destroy() {
 // TODO Auto-generated method stub
 
 }
 @Override
 public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
 throws IOException, ServletException {
 // TODO Auto-generated method stub
 HttpServletRequest request = (HttpServletRequest) arg0;
 HttpServletResponse response = (HttpServletResponse) arg1;
 String strURL = request.getRequestURL().toString(); // 獲取請(qǐng)求路徑
 // System.out.println(strURL);
 // 只過(guò)濾來(lái)自SubmitServlet請(qǐng)求和singlecount.jsp的加載,可以設(shè)置成自己想過(guò)濾的
 // 需要在web.xml中添加<filter>
 if(strURL.indexOf("SubmitServlet") != -1 || strURL.indexOf("singlecount.jsp") != -1){ 
 if(request.getSession().getAttribute("name") == null){
 request.getSession().invalidate();
 response.sendError(900, "登錄失效,請(qǐng)重新登錄!"); // 自定義狀態(tài)碼,session失效
 // 900 到ajax的error中處理
 return;
 }
 else {
 arg2.doFilter(arg0, arg1);
 }
 }
 else {
 arg2.doFilter(arg0, arg1);
 }
 }
 @Override
 public void init(FilterConfig arg0) throws ServletException {
 // TODO Auto-generated method stub
 
 }
}

配置web.xml

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

 <filter>
 <filter-name>filter.SessionFilter</filter-name>
 <filter-class>filter.SessionFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>filter.SessionFilter</filter-name> 
 <url-pattern>/singlecount.jsp</url-pattern> <!-- 給界面添加過(guò)濾器 -->
 </filter-mapping>
 <filter-mapping>
 <filter-name>filter.SessionFilter</filter-name>
 <url-pattern>/SubmitServlet</url-pattern> <!-- 給servlet添加過(guò)濾器 -->
 </filter-mapping>
</web-app>

實(shí)現(xiàn)效果

可以使用兩個(gè)不同的瀏覽器當(dāng)兩個(gè)客戶端,或者電腦多就用多臺(tái)電腦。

相同賬號(hào)登錄時(shí),前面賬號(hào)再點(diǎn)提交請(qǐng)求就會(huì)給出提示,跳轉(zhuǎn)到登錄界面

在這里插入圖片描述

未登錄直接進(jìn)入:http://localhost:8080/OneLogin/singlecount.jsp

在這里插入圖片描述

如果也想實(shí)現(xiàn)跳轉(zhuǎn)效果,在jsSubmit.js的$(document).ready(function() {…}); 前面加入是否有session的判斷,沒(méi)有就給出提示,跳轉(zhuǎn)到登錄界面。

總結(jié)

以上所述是小編給大家介紹的Java web實(shí)現(xiàn)賬號(hào)單一登錄,防止同一賬號(hào)重復(fù)登錄,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論