Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update session attribute

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.

like image 1000
Vaishali Avatar asked Oct 21 '22 08:10

Vaishali


1 Answers

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);
like image 127
cherouvim Avatar answered Oct 23 '22 10:10

cherouvim