Automatically load a page after a few seconds
Written by coregps on Tuesday, November 15th, 2005 in Web Design.
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!
We’ve developed a high load online exam system in which instructors create exams and deliver them through browser. The questions on the paper is randomly chose based on the parameters specified by the instructor. When students are ready to take the exam, the paper for each student will be sent to the browser from the server. I’ve created two paper pages. One is used to display the empty paper when the student login the first time. The other is used to display the paper with the answers the student already entered when he exit the system abnormally while taking an exam, he can simply re-connect, go back to the exam, and resume the exam. On an average we have about 50 to 100 students taking tests concurrently.
Sometimes, one or two machines cannot fetch the paper page successfully. Instead an error page is displayed. If the student then hit the refresh button the page is displayed. I want to direct the student to a friendly error page with this message: ‘The server is busy now, you will be redirected to the paper page after 5 seconds.’, and then reload the paper page automatically, instead of telling the students to refresh the page manually. However, as I mentioned above, there are two paper pages, both can throw the exception. So I must know which page throw the exception. I first thought of using request.getHeader(”Referer”) to get it, but it is always null. Finally, I added an additional parameter to the custom error page in the page directive, which is used to tell the error page which paper page throws the exception. In the error page, I can fetch the parameter from the request, and then set the “Refresh” header to redirect the student to the paper page which throws the exception.
In order to verify my idea, I write the following test code:
// the web page throws the exception, exception.jsp
%><%@ page errorPage=”retry.jsp?from=exception.jsp”
%><%
response.setHeader(”Pragma”, “no-cache”);
response.setHeader(”Expires”, “0″);
response.setHeader(”Cache-Control”, “no-store”);
%><html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″/>
<title></title>
</head>
<body><h2>Exception Testing</h2><%
String test = request.getParameter(”id”);
out.println(Integer.parseInt(test));
%>
</body>
</html>
// error handler page, retry.jsp
%><%@ page isErrorPage=”true”
%><%
String from = request.getParameter(”from”);
response.setHeader(”Refresh”, “5; URL=” + from);
%><html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″/>
<title></title>
</head>
<body>
<div>
The server is busy now, you will be redirected to the paper page after 5 seconds. <br />
<a href=”<%=from%>”>Click here if your page doesn’t automatically refresh after 5 seconds.</a>
</div>
</body>
</html>
As you can see, I also set the cache control headers in order to avoid the browser cache the exception page.










