Java 如何更新会话属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23707955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:49:51  来源:igfitidea点击:

How to update session attribute

javajspsessionservlets

提问by Vaishali

I have some session attributes being saved. I have a jsp page on which a call to a servlet is made through. This servlet updates one of the session variable but I am not able to see the reflection of these changes in my jsp.Pls help.

我保存了一些会话属性。我有一个调用 servlet 的 jsp 页面。此 servlet 更新会话变量之一,但我无法在 jsp.Pls 帮助中看到这些更改的反映。

In My servlet

在我的 servlet

    List<DriverList> abc = dao.getABC();
    request.getSession().removeAttribute("abc");
    request.getSession().setAttribute("abc", abc);

In my jsp

在我的jsp中

function update()
{
    var url = "updateServlet";
    var req = $.ajax({
    type: 'GET',
    url: url,
    cache: false,
    type: "GET",
    success: function()
    {
        latlng = [];
        latlng = [<c:forEach var="test" items="${abc}">
                     [<c:out value="${test.latitude}"/>,<c:out value="${test.longitude}"/>,"<c:out value= "${test.name}" />",<c:out value="${test.cellNo}"/>],
                 </c:forEach> ];

    },
    error: function (status) {
         }

    });

}  

The value of ${abc}is same as before. How to get the new value ?

的值${abc}与以前相同。如何获得新值?

The exact flow -

准确的流程——

  1. when login servlet is called abcvalue as sessionAttributeis set.

  2. Now this redirects to base.jsp. I use abc for the first time. Now after every 30 seconds this update()function is called. This update function calls a servlet through ajax where the session attribute abc is updated.

  3. In the success function of ajax request I want to use this new abc value but getting the old one again.

  1. 当登录 servlet 被调用时设置的abcsessionAttribute

  2. 现在这重定向到 base.jsp。我第一次使用 abc。现在每 30 秒update()调用一次该函数。此更新函数通过 ajax 调用 servlet,其中更新会话属性 abc。

  3. 在 ajax 请求的成功函数中,我想使用这个新的 abc 值,但再次获得旧的值。

回答by cherouvim

To access the abcvariable in the JSP try:

要访问abcJSP 中的变量,请尝试:

${sessionScope.abc}

Also note that removing before setting is usually redundant. So:

另请注意,在设置之前删除通常是多余的。所以:

request.getSession().removeAttribute("abc");
request.getSession().setAttribute("abc", abc);

Can simply become:

可以简单地变成:

request.getSession().setAttribute("abc", abc);

回答by Romen

I had similar problem. It turned out that when you use

我有类似的问题。原来,当你使用

HttpSession 

object in your controller, it shouldn't be annotated using

控制器中的对象,不应使用

@SessionAttributes