title: Tomcat: Handle Servlet Exception
body: <p>I am setting up PHP on my Tomcatserver using the PECL-servlet (PHP/PECL-version 5.2.5). The server now sucessfully handles real PHP-files, but i am having a problem with requests for nonexistent pages.</p>
  
  <p>A request for such a page, f.ex: <a href="http://www.mydomain.com/nonexistentfile.php" rel="nofollow noreferrer">http://www.mydomain.com/nonexistentfile.php</a>, causes the servlet to raise a java.io.IOException, which is never catched. Tomcat is therefore terminated.</p>
  
  <p>How can i fix this? Can i catch exceptions from the servlet? Is there a way to only map existing files to the servlet?</p>
  
  <hr>
  
  <p>Thanks for the reply, its seems to be working now. I did your suggested re-map in web.xml, mapping the php-files to my first self-made servlet, which basically wrapps around the phpservlet and handles raised exceptions. Is this a good way solving the problem?</p>
  
  <p>It seems a little strange to me that tomcat isn't able to handle such exceptions on its own. I can see the point in terminating the server to protect it from executing in corrupt states, but it should be possible to enable custom exception-handling...</p>
  
  <p>Here is my localhost.2009-12-09.log after invoking phpservlet directly with non-existent page:<br>
  (tomcat displays the 4 first lines of the stacktrace int its default errorpage before terminating)</p>
  
  <pre>
  09.des.2009 13:35:50 org.apache.catalina.core.ApplicationContext log
  INFO: ContextListener: contextInitialized()
  09.des.2009 13:35:50 org.apache.catalina.core.ApplicationContext log
  INFO: SessionListener: contextInitialized()
  09.des.2009 13:35:50 org.apache.catalina.core.ApplicationContext log
  INFO: ContextListener: attributeAdded('org.apache.catalina.Registry', 'org.apache.tomcat.util.modeler.Registry@1010058')
  09.des.2009 13:35:50 org.apache.catalina.core.ApplicationContext log
  INFO: ContextListener: attributeAdded('org.apache.catalina.MBeanServer', 'com.sun.jmx.mbeanserver.JmxMBeanServer@bdab91')
  09.des.2009 13:35:54 org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet php threw exception
  java.io.IOException: 
   at net.php.servlet.send(Native Method)
   at net.php.servlet.service(servlet.java:190)
   at net.php.servlet.service(servlet.java:214)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
   at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
   at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
   at java.lang.Thread.run(Unknown Source)
  </pre>