I am developing a web project with spring boot. I am using JSP on the frontend. I save some calculations I made in my service to session and then I show them in my JSP file with getAttribute()
method. My problem is this: When I open my JSP page for the first time, these values freeze to null. When I refresh the page, the values appear as they should be, but it always takes time for the values to arrive, and I have to manually refresh the page in order for these values to appear.
I don't think there is a way to zero this wait time and a way to show session values instantly.
Here is my example service java code:
@Override
public void testService(HttpServletRequest request, HttpServletResponse response) throws Exception {
String testValue = "testValue";
request.getSession().setAttribute("testValue", testValue);
}
Here is an example of JSP file:
<div id="testValue">
<%=request.getSession().getAttribute("testValue")%>
</div>
The screenshots of problem statement:
- the value before refresing the page
- the value after refreshing the page
As seen in the images, I can see the value I want when I manually refresh the page. But we cannot expect the user to manually refresh the page. I don't know if there is a way to directly wait for these session values without waiting. I tried to write an ajax call that will update the relevant party when the session values are set:
<script>
function updateSessionVariables() {
var url = "/diger/indexpage/searchnew.oim";
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.onreadystatechange = function () {
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
document.getElementById("testValue").innerHTML = xmlHttpRequest.responseText;
}
}
};
}
updateSessionVariables();
</script>
But there was no change, I had to manually refresh the page again to see the values. How can I solve this problem? I want to show these values to the user directly without waiting or at least without having to manually refresh the page.