gpt4 book ai didi

javascript - 如何在特定 session 中跟踪特定浏览器的网站

转载 作者:行者123 更新时间:2023-11-29 21:08:35 25 4
gpt4 key购买 nike

1.如何在同一个浏览器的下一个标签中发现您的网站被点击?2、如何防止网站在第二个标签打开?

最佳答案

如果浏览器首先调用您的站点,您会在服务器端创建一个 session ,这会导致将 session cookie 发送到浏览器。在您的 HTML 中,您可以嵌入一个隐藏的表单值。这个隐藏值必须包含在每个后续调用中。最好始终使用 POST,这样隐藏的值就不会包含在 URL 中。

如果用户打开第二个选项卡并想打开您网站的 URL,则不包括隐藏参数,但包含第一个选项卡中的 session cookie。

所以在服务器端你知道已经有一个 session 但是隐藏的值丢失了。因此,您可以发送完全不同的响应。

更新这是一个小例子。

在网页内容文件夹中有一个子文件夹protected。所有包含的 JSP 文件只能在一个选项卡中打开。这里只有MyJsp.jsp

在根文件夹中有两个 JSP:error.jsp 当有人试图在第二个选项卡中打开 protected JSP 时显示。和重定向到 protected/MyJsp.jspindex.jsp

并且有一个 servlet 过滤器映射到 protected 文件夹。在执行此文件夹中的 JSP 之前将调用此过滤器。

protected/MyJsp.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<p>Hello,
<c:choose>
<c:when test="${not empty param.name}">
<c:out value="${param.name}" />.
</c:when>
<c:otherwise>
stranger.
</c:otherwise>
</c:choose>
</p>
<form method="post">
<label>Please enter your name</label>
<input id="name" name="name" type="text"/>
<input id="marker" name="marker" type="hidden"
value="<c:out value="${sessionScope.marker}"/>"/>
<button type="submit">OK</button>
</form>
</body>
</html>

此 JSP 请求名称。表单提交通过 POST 调用相同的 JSP。隐藏字段填充了 session 中的值。

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!--include the library-->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:redirect url="protected/MyJsp.jsp"/>

servlet 过滤器:

@WebFilter("/protected/*")
public class OneTabFilter implements Filter {

private static final String MARKER_NAME = "marker";
private static final String MARKER_VALUE = "4711*0815";

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
final HttpServletRequest req = (HttpServletRequest) request;
final HttpServletResponse rsp = (HttpServletResponse) response;
HttpSession session = req.getSession(false);
if(session == null) {
session = req.getSession(true);
// Put the marker value into session so it is usable in JSP files.
session.setAttribute(MARKER_NAME, MARKER_VALUE);
// pass the request along the filter chain
chain.doFilter(request, response);
} else {
if(MARKER_VALUE.equals(req.getParameter(MARKER_NAME))) {
// pass the request along the filter chain
chain.doFilter(request, response);
} else {
// Redirect to the error page.
// The error page itself is not affected by this filter.
rsp.sendRedirect(req.getServletContext().getContextPath() + "/error.jsp");
}
}
}

// ...
}

自己试试吧!

关于javascript - 如何在特定 session 中跟踪特定浏览器的网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42763812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com