【问题标题】:How do I get an http session created by url in word document to be used when a link in the browser is clicked当单击浏览器中的链接时,如何获取由 word 文档中的 url 创建的 http 会话
【发布时间】:2010-10-19 10:27:02
【问题描述】:

我有一个基于 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"));
    }
}

【问题讨论】:

  • 在其他应用程序中单击链接时的行为有所不同。例如,Acrobat Reader 链接只创建一个会话,浏览器继续用于后续页面。

标签: http session servlets ms-word


【解决方案1】:

安装像Fiddler 这样的网络调试器或进行Wireshark 捕获,我敢打赌,您的 JSESSIONID cookie 不会在 HTTP 请求上发送。我已经通过在 IE 中打开的 Microsoft Word 的链接看到了这一点。

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

【讨论】:

    猜你喜欢
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    • 2016-05-10
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 2014-08-18
    相关资源
    最近更新 更多