John
John

Reputation: 838

response.setheader("Refresh", "0") not working

I am trying to refresh my webpage when user session has expired or the connection is not active. I tried a lot of codes but it didn't seem to work. The last code that 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 didnt work as well. the servlet is being called by a javascript which expects a json object.

Upvotes: 4

Views: 3749

Answers (2)

DevZer0
DevZer0

Reputation: 13535

If you want a page to be refreshed use the following meta tag in the page header

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

Upvotes: 0

MaVRoSCy
MaVRoSCy

Reputation: 17839

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

So if no request is made from the client, then nothing can be send from the server (simply because the server doesn't have a request to response )

I know you have seen this functionality and yes it can be done. But not in this manner. Ajax is your best bet here... (sending requests at regular intervals and getting response back from server etc)

Upvotes: 1

Related Questions