如何获取由Word文档中的url创建的http会话,以便在单击浏览器中的链接时使用

发布于 2024-07-18 03:35:23 字数 1484 浏览 3 评论 0原文

我有一个基于 servlet 的应用程序,它创建一个会话并在第一次访问时存储一些信息。 此信息将在后续页面中使用。 如果从 msword 文档中单击初始 URL,则该过程会遇到麻烦。 servlet 创建一个会话并发回响应。 响应显示在新打开的浏览器中。 从浏览器内部单击的链接会创建一个新会话。 从浏览器单击的任何后续 url 都会重用第二个会话

有没有办法强制服务器在第二个请求上识别初始会话?

我创建了一个示例 servlet,它检索会话并写入 ID 以及新链接以回调自身。

下面是测试servlet

public class SessionServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        StringBuffer sb = new StringBuffer();
        sb.append("<HTML>\r\n<HEAD>\r\n<title>\r\nServlet Session Test\r\n</title>\r\n");
        sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
        sb.append("</HEAD>\r\n<BODY>\r\n");

        HttpSession userSession = request.getSession(true);

        if (userSession.isNew()) {
            sb.append("User Session is new\r\n");
        } else {
            sb.append("User Session is old\r\n");
        }
        sb.append("<br>User Session ID = " + userSession.getId() + "<BR>");

        sb.append("URL <a href=\"http://localhost:9080/myTestApp/SessionTest\"> Session Test </a>");
        sb.append("</BODY> </HTML>\r\n");

        response.setContentType("text/html; charset=UTF-8");
        ServletOutputStream sos = response.getOutputStream();
        sos.write(sb.toString().getBytes("UTF-8"));
    }
}

I have a servlet based application that creates a session and stores some information the first time it is accessed. This information is used in subsequent pages. The process runs into trouble if the initial url is clicked from inside a msword document. The servlet creates a session and sends the response back. The response is displayed in a newly opened browser. A link clicked from inside the browser creates a new session. Any subsequent urls clicked from the browser reuse the second session

Is there a way to force the server to recognize the initial session on the second request?

I created a sample servlet that retrieves a session and writes the ID along with a new link to call back to itself.

Below is the test servlet

public class SessionServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        StringBuffer sb = new StringBuffer();
        sb.append("<HTML>\r\n<HEAD>\r\n<title>\r\nServlet Session Test\r\n</title>\r\n");
        sb.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
        sb.append("</HEAD>\r\n<BODY>\r\n");

        HttpSession userSession = request.getSession(true);

        if (userSession.isNew()) {
            sb.append("User Session is new\r\n");
        } else {
            sb.append("User Session is old\r\n");
        }
        sb.append("<br>User Session ID = " + userSession.getId() + "<BR>");

        sb.append("URL <a href=\"http://localhost:9080/myTestApp/SessionTest\"> Session Test </a>");
        sb.append("</BODY> </HTML>\r\n");

        response.setContentType("text/html; charset=UTF-8");
        ServletOutputStream sos = response.getOutputStream();
        sos.write(sb.toString().getBytes("UTF-8"));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖 2024-07-25 03:35:23

安装 Web 调试器,例如 Fiddler 或执行 Wireshark 捕获,我敢打赌您的 JSESSIONID cookie 不会在 HTTP 请求上发送。 我在 IE 中打开的 Microsoft Word 链接中看到了这一点。

我所做的是将您的代码更改为 request.getSession(false) ,如果返回 null ,则执行 response.sendRedirect 返回给自己添加一个附加到 URL 的附加查询字符串参数(例如 newSession=true)。 当请求返回并带有此附加参数时,执行 request.getSession(true)。 希望这会找到您现有的会话,但如果没有,它将创建一个新会话。 请小心,不要将代码置于无限的 sendRedirect 循环中。 这是有效的,因为当 IE 发出第二个 HTTP 请求时,它会发送 cookie。

Install a web debugger like Fiddler or do a Wireshark capture and I bet your JSESSIONID cookie is not being sent on the HTTP request. I have seen this with links from Microsoft Word that open in IE.

What I have done is change your code to request.getSession(false) and if you get null back, then do a response.sendRedirect back to yourself with an additional querystring parameter (e.g. newSession=true) appended to the URL. When the request comes back in with this additional parameter, then do a request.getSession(true). Hopefully, this will find yoru existing session, but if not it will create a new session. Be careful not to put you code in an infinite sendRedirect loop. This works because when IE makes the second HTTP request, it sends the cookies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文