File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Time out inside a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Time out inside a servlet" Watch "Time out inside a servlet" New topic

Time out inside a servlet

Frank Hamilton

Joined: Jan 19, 2011
Posts: 21

I need to implement a time out system inside a doget servlet method. This method try to connect with a nfs server, if It not respond in 5 second then i need to redirect a error.jsp page.
Can somebody tell my what is the best way to do this work.
Thank so much.
Jatin Dhingra

Joined: Jun 28, 2010
Posts: 29
As I guess, you would be using while(true) loop to get connected to the server. Make a variable that holds system time when servlet was instantiated( out of while loop) and make other variable that notes system time each time while loop is executed ( it will be inside while loop). Then inside while loop, check condition that if difference between two "time variables" is more than 5 seconds execute a break statement.

does that sound helpful ?
Frank Hamilton

Joined: Jan 19, 2011
Posts: 21
thank so much Jatin,

I think this solution not work for me.
Inside my servlet I use WEBNFS for creating a nfs client and access to a remote directory. I need a time out if the nfs server is down but I think this api don't give me any mechanism for me pourpose. Therefore I think any mechanism, maybe a thread, can help me.
This is my code:

I think your solution not work because the file.exist() method wait for the connexion and the time variable inside the while never increment.

Anybody can help me with this problem?. I think nsf is over tcp protocolo and maybe can use some tcp connector method try to connect with nfs server and if the server is down this method throw a timeout.

Madhan Sundararajan Devaki
Ranch Hand

Joined: Mar 18, 2011
Posts: 312

Please initiate/run a TimerTask from your Servlet, for the duration required and try to get a feedback from it. Based on what it returns, you can take the next step.

Not many get the right opportunity !
Frank Hamilton

Joined: Jan 19, 2011
Posts: 21
Hi Madhan, thanks for your replay.

I solved my problem with the information in this post, it's work fine for me. I'll probe TimerTask when I have same time, it's looking good.


I agree. Here's the link:
subject: Time out inside a servlet
It's not a secret anymore!