如何在浏览器选项卡中区分会话? [英] How to differ sessions in browser-tabs?

查看:34
本文介绍了如何在浏览器选项卡中区分会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 JSP 和 Servlet 的 Java 中实现的 Web 应用程序中;如果我在用户会话中存储信息,则该信息将从同一浏览器的所有选项卡共享.如何在浏览器选项卡中区分会话?在这个例子中:

In a web-application implemented in java using JSP and Servlets; if I store information in the user session, this information is shared from all the tabs from the same browser. How to differ sessions in the browser-tabs? In this example:

<%@page language="java"%>
<%
String user = request.getParameter("user");
user = (user == null ? (String)session.getAttribute("SESSIONS_USER") : user);
session.setAttribute("SESSIONS_USER",user);
%>
<html><head></head><body>
<%=user %>
<form method="post">
User:<input name="user" value="">
<input type="submit" value="send">
</form>
</body></html>

将此代码复制到一个 jsp 页面 (testpage.jsp) 中,将此文件部署到服务器上 Web 应用程序的现有上下文中(我使用 Apache Tomcat),然后打开浏览器(FF、IE7 或 Opera)使用正确的 URL(localhost/context1/testpage.jsp),在输入中输入您的姓名并提交表单.然后在同一浏览器中打开一个新选项卡,然后您可以在新选项卡上看到您的姓名(从会话中获取).小心浏览器缓存,有时似乎没有发生,但它在缓存中,刷新第二个选项卡.

Copy this code in a jsp page (testpage.jsp), deploy this file in an existing context of a web application on the server (I use Apache Tomcat), then open a browser (FF, IE7 or Opera) using the correct URL (localhost/context1/testpage.jsp), type your name in the input and submit the form. Then open a new tab in the same browser, and then you can see your name (get from the session) on the new tab. Be careful with the browser-cache, sometimes seems that it doesn't happen, but it's in the cache, refresh the second tab.

谢谢.

推荐答案

您可以使用 HTML5 SessionStorage (window.sessionStorage).您将生成一个随机 ID 并保存在每个浏览器选项卡的会话存储中.然后每个浏览器选项卡都有自己的 ID.

You can use HTML5 SessionStorage (window.sessionStorage). You will generate a random id and save in session Storage per Browser Tab. Then each browser tab has his own Id.

使用 sessionStorage 存储的数据不会跨浏览器选项卡持续存在,即使两个选项卡都包含来自同一域来源的网页.在换句话说, sessionStorage 中的数据不仅限于调用页面的域和目录,但浏览器选项卡页面所在的位置.与会话 cookie 相比,可以将数据从选项卡保存到选项卡.

Data stored using sessionStorage do not persist across browser tabs, even if two tabs both contain webpages from the same domain origin. In other words, data inside sessionStorage is confined to not just the domain and directory of the invoking page, but the browser tab in which the page is contained in. Contrast that to session cookies, which do persist data from tab to tab.

这篇关于如何在浏览器选项卡中区分会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆