I am trying to use javascript to do something once it detects a change in the session object. However, javascript does not detect the change in the session object after i have called UpdateServlet. All these is done on the same page (ie. there is no refreshing of page)
Please help! Thanks.
index.jsp
<%
//initial settings
session.setAttribute("dataUpdated", "false");
%>
<script>
$(function() {
var updates = '<%=session.getAttribute("dataUpdated")%>';
alert(updates);
//Shows false even after update
if (updates=="true"){
//alert("Updated");
}
});
</script>
UpdateServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
session.setAttribute("dataUpdated", "true");
//session object set to true
}
You have to use Ajax in order to check this change. You could check periodically with
setIntervallike this:My advice is to do your ajax call with POST method to avoid caching (if it caches, no change will be detected)
Hope this helps. Cheers
PS: I used jQuery cause I saw you were using it on your code