简体   繁体   中英

redirect page invoked from java class

Im new to java web app, JavaEE if I'm not mistaken. it happen that I was task to finished a certain project, while learning Java web app. So I'm not even certain if where am at is right. Here is the dilemma Im facing.

It had a timer on a certain java class. based on the timer count, per couple of seconds, the page is suppose to redirect to another page. To explain further of what I want to achieve, refer to the following:

user opens Page1 after 30 mins redirects to Page2 after 15 minutes redirects to Page3

the timer is set on the java class and not on the jsp file. is there a way that the java class could issue a redirect command to the web browser?

As far as I know this is very tricky if not near impossible to achieve without using the client side. The problem is that the communication between the server and the client works in a request/response manner. This means that if the user requests a page, controller prepares it and then it is served to a client via response. Doing it right away without a timeout isn't a problem with response.sendRedirect() , but in order to push an action from the server to the client after some timeout, you would have to implement some sort of a listener on the client side.

This problem can be of course solved with a script on the client's side which could handle automatic redirects.

Use JavaScript timeout function and not Java's

 setTimeout(function(){  
 // Set  your redirection logic here 
 },(60000 * 10 ));

Try this :

response.sendRedirect(request.getContextPath()+"/YourJsp.jsp");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM