How to update session attribute

依然范特西╮ 提交于 2021-02-06 12:50:12

问题


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.

In My servlet

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

In my 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 ?

The exact flow -

  1. when login servlet is called abc value as sessionAttribute is 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:


To access the abc variable in the JSP try:

${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);



回答2:


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

HttpSession 

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

@SessionAttributes


来源:https://stackoverflow.com/questions/23707955/how-to-update-session-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!