Response.setheader ("Update", "0") does not work

I am trying to refresh my web page when a user session has expired or the connection is inactive. I tried a lot of codes, but it didn't seem to work. The last code I used is

if(session.getAttribute("connection") != null && !session.getAttribute("connection").equals("")){ conn = (DBConnection) session.getAttribute("connection"); if(conn == null){ response.setContentType("text/html"); response.setHeader("Refresh", "3"); return; } }else{ response.setContentType("text/html"); response.setHeader("Refresh", "3"); return; } 

I also tried response.sendRedirect () and request.getrequestdispatcher.forward (), but it didn't work either. the servlet is invoked by javascript, which expects a json object.

+4
source share
2 answers

You cannot directly execute the command on the client directly from the servlet. Note that the http protocol relies on client requests and server responses.

So, if a request is not received from the client, then nothing can be sent from the server (simply because the server does not have a response request)

I know that you saw this functionality, and yes, it can be done. But not like that. Ajax is your best bet here ... (sending requests at regular intervals and receiving a response from the server, etc.)

+1
source

If you want to refresh the page, use the following meta tag in the page header

 <meta http-equiv="refresh" content="5"> 
0
source

Source: https://habr.com/ru/post/1480052/


All Articles