How to set a maximum execution time in JSP/Servlet?
I would like to set a time limit for sc开发者_StackOverflowript execution. In PHP I use "set_time_limit". How can I do this in JSP?
I have to disappoint you - there is no such setting. In JSP it is hardly feasible, when using servlets (especially 3.0) you have some options. However all of them require running your logic in a separate thread.
See my answer here, it is about Spring MVC, but it explains the general concept.
Wrap the code you want to execute inside of java Runnable, then use ExecutorService to set a timeout for that task.
http://download.oracle.com/javase/1,5,0/docs/api/java/lang/Runnable.html http://download.oracle.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html
This should be a setting of your servlet container. In tomcat you can configure these on the connector - see here (look for 'timeout'). (Also see here)
精彩评论