javascript 函数终止会话后调用 servlet

发布于 2024-10-09 08:46:05 字数 586 浏览 1 评论 0原文

我有以下内容:

<a href="/servlet/MyServlet" onclick="javascript:CreatePageView();"> Link 1 </a>

但我注意到 javascript 函数 CreatePageView() 并没有一直执行并且正在创建竞争情况。有时 javascript 会被执行,有时会先执行重定向。

所以我想控制事件的顺序,并考虑在我的 javascript 函数中调用 servlet。

function CreatePageView()
{
    //Execute javascript function here

    //Invoke servlet here
    document.forms[0].action = "/servlet/MyServlet";
    document.forms[0].submit();
}

当我调用 servlet 时,我的会话被销毁,并且我被重定向到登录页面。谁能解释为什么会发生这种情况?或者也许建议一种在不终止会话的情况下调用 servlet 的替代方法?提前致谢。

I had the following:

<a href="/servlet/MyServlet" onclick="javascript:CreatePageView();"> Link 1 </a>

but I noticed that the javascript function CreatePageView() did not get executed all the time and was creating a race situation. Sometimes the javascript would get executed, others times the redirect was happening first.

So I wanted to control the order of events and thought to invoke the servlet within my javascript function.

function CreatePageView()
{
    //Execute javascript function here

    //Invoke servlet here
    document.forms[0].action = "/servlet/MyServlet";
    document.forms[0].submit();
}

When I invoke my servlet, my session gets destroyed and I get redirected to the login page. Can anyone explain why this is happening? Or perhaps suggest an alternate method of invoking the servlet without killing the session? Thanks in advance.

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

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

发布评论

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

评论(1

梦情居士 2024-10-16 08:46:05

这听起来很像 JavaScript 正在触发一个异步请求。否则这个问题就没有任何意义。仅当 JavaScript 函数返回时,链接的操作才会以任何方式执行。但是,当您在 JS 函数中触发异步/ajaxical 请求时,确实可能会发生竞争条件。也就是说,它不同步执行。它“在后台”执行。

您需要确保仅在异步请求完成时才调用该链接。假设您通过 XMLHttpRequest 在“普通”JS 中执行此操作,而不是像 jQuery 这样方便的 Ajaxical JS 库,那么你需要在onreadystatechange中做这个工作。

按如下方式更改链接:

<a href="/servlets/MyServlet" onclick="return createPageView(this)">

(请注意 javascript: 伪协议不必要,并且 JS 函数通常以小写字母开头)

并修复您的 JS 函数如下(不兼容MSIE,请自行修复)

function createPageView(link) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            window.location = link.href; // See?
        }
    }
    xhr.open('GET', 'http://example.com', true);
    xhr.send(null);
    return false; // Block link's default action.
}

至于为什么会话被破坏的问题,当请求标头不包含正确的会话cookie时,或者当您调用 session.invalidate 时,它​​将被“破坏” () 在服务器端,或者在不同的域/上下文上触发请求时。你是唯一能调查谁是罪魁祸首的人。

This sounds much like as if that JavaScript is firing an asynchronous request. Otherwise the problem doesn't make any sense. The link's action will in any way only be executed when the JavaScript function has returned. But when you're firing an asynchronous/ajaxical request in the JS function, then indeed a race condition may occur. It namely doesn't execute in sync. It executes "in the background".

You need to ensure that the link is only invoked when the asynchronous request is finished. Assuming that you're doing it in "plain vanilla" JS by XMLHttpRequest instead of a convenient Ajaxical JS library like jQuery, then you need to do the job in the onreadystatechange.

Change the link as follows:

<a href="/servlets/MyServlet" onclick="return createPageView(this)">

(note that the javascript: pseudoprotocol is unnecessary and that JS functions usually start with lowercase)

And fix your JS function as follows (not MSIE compatible, fix that yourself)

function createPageView(link) {
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            window.location = link.href; // See?
        }
    }
    xhr.open('GET', 'http://example.com', true);
    xhr.send(null);
    return false; // Block link's default action.
}

As to the question why the session get destroyed, it will be "destroyed" when the request headers doesn't contain the proper session cookie, or when you call session.invalidate() in server side, or when the request is been fired on a different domain/context. You're the only one who can investigate which one is the culprit.

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