问题
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 -
when login servlet is called
abc
value assessionAttribute
is set.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.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