There is not much you can do if the server is too busy to handle your request. You may be able to configure the server to return a custom page.
In the case of a long running process you can do something the following
package com.test;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletTimeTest extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) {
try {
//Create object to get results of sub-thread
ServletTimeTestResults resultObject = new ServletTimeTestResults();
//Create sub-thread
Thread servletTimeTestThread =
new Thread(
new ServletTimeTestThread(
resultObject, Thread.currentThread()));
//start sub-thread
servletTimeTestThread.start();
//Wait ten seconds for sub-thread to finish processing
try {
Thread.sleep(10000);
} catch (InterruptedException ie) {
//sub-thread has finished in less than 10 seconds
System.out.println("Servlet sleep interrupted");
}
//set the flag to make sure sub-thread stops processing
resultObject.setStopProcessing(true);
//check if sub-thread completed processing sucessfully
if (resultObject.isProcessingComplete()) {
//if sub-thread did finish processing then return results from
//resultObject here.
System.out.println("Processing completed");
} else {
//if sub-thread did not finish processing then redirect to
//error page here.
System.out.println("Processing IS NOT completed");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.test;
/**
* sub-processing thread.
*
* @author Tolmke
*
*/
public class ServletTimeTestThread implements Runnable {
//Object to facilitate inter-thread communication
ServletTimeTestResults resultObject;
//Callback handle to servlet thread
Thread mainThread;
public ServletTimeTestThread(
ServletTimeTestResults resultObject,
Thread mainThread) {
this.resultObject = resultObject;
this.mainThread = mainThread;
}
public void run() {
boolean wait = true;
int count = 0;
//main processing thread. Need to periodically check resultObject.isStopProcessing()
//to determine if main thread wants to terninate sub-processing
while ((wait) && (this.resultObject.isStopProcessing() == false)) {
try {
Thread.sleep(1000);
System.out.println("In proecessing thread...................");
//count can be set to values greater or less than 10 seconds to test
//various scenarios
if (count++ > 3) {
wait = false;
}
} catch (InterruptedException ie) {
ie.printStackTrace();
}
}//End while
//If servlet thread did not set falg indicating timed out then set
//processing successfully completed lag.
if (this.resultObject.isStopProcessing() == false) {
this.resultObject.setProcessingComplete(true);
}
//if servlet thread has not timed out then interrupt the
//the thread from th esleep state.
this.mainThread.interrupt();
}//End run method
}//End class ServletTimeTestThread
package com.test;
/**
* Object to facilitate communication between main servlet thread
* and a processing sub-thread.
*
* @author Tolmke
*
*/
public class ServletTimeTestResults {
//Flag indicating sucessful completion of processing
private boolean processingComplete;
//Flag indicating that sub-process should stop processing
private boolean stopProcessing;
public boolean isProcessingComplete() {
return processingComplete;
}
public void setProcessingComplete(boolean processingComplete) {
this.processingComplete = processingComplete;
}
public boolean isStopProcessing() {
return stopProcessing;
}
public void setStopProcessing(boolean stopProcessing) {
this.stopProcessing = stopProcessing;
}
}