Tomcat:处理Servlet异常
我正在使用 PECL-servlet(PHP/PECL-版本 5.2.5)在 Tomcat 服务器上设置 PHP。服务器现在成功处理真正的 PHP 文件,但我在请求不存在的页面时遇到问题。
对此类页面的请求,例如: http://www.mydomain.com/nonexistentfile.php ,导致 servlet 引发 java.io.IOException,但该异常永远不会被捕获。 Tomcat 因此被终止。
我该如何解决这个问题?我可以捕获 servlet 中的异常吗?有没有办法只将现有文件映射到 servlet?
感谢您的回复,它现在似乎可以工作。我在 web.xml 中做了您建议的重新映射,将 php 文件映射到我的第一个自制 servlet,它基本上包装了 phpservlet 并处理引发的异常。这是解决问题的好方法吗?
tomcat 无法自行处理此类异常,这对我来说似乎有点奇怪。我可以看到终止服务器以防止其在损坏状态下执行的意义,但应该可以启用自定义异常处理...
这是我直接使用非调用 phpservlet 后的 localhost.2009-12-09.log -现有页面:
(tomcat 在终止前显示其默认错误页面的堆栈跟踪的前 4 行)
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)
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.
A request for such a page, f.ex: http://www.mydomain.com/nonexistentfile.php, causes the servlet to raise a java.io.IOException, which is never catched. Tomcat is therefore terminated.
How can i fix this? Can i catch exceptions from the servlet? Is there a way to only map existing files to the servlet?
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?
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...
Here is my localhost.2009-12-09.log after invoking phpservlet directly with non-existent page:
(tomcat displays the 4 first lines of the stacktrace int its default errorpage before terminating)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您通常在
web.xml
中将其声明为
。但既然你字面上说,“这永远不会被捕获。Tomcat 因此被终止”,我对设置产生了怀疑。你真的没有看到 Tomcat 的默认错误页面,里面有堆栈跟踪吗?您在应用程序服务器日志中没有看到任何内容吗?您是如何知道
IOException
的?至少,您可以尝试在
*.php
的url-pattern
上映射Filter
,其中基本上包含以下几行:但是看看如何你描述了这个问题,我宁愿认为 PHP servlet 或 webcontainer 无法显示错误页面,因为响应已经提交,但在这种情况下,你应该看到一个相当不言自明的
IllegalStateException: 响应已经提交应用程序服务器日志中出现
错误。You normally declare it as
<error-page>
inweb.xml
.But since you literally said, "which is never catched. Tomcat is therefore terminated", I have my doubts about the setup. Do you really not see Tomcat's default error page with a stacktrace inside? Don't you see anything in the appserver logs? How did you know about the
IOException
?At least, you could try to map a
Filter
on theurl-pattern
of*.php
which contains basically the following lines:But seeing how you described the problem, I rather think that the PHP servlet or the webcontainer failed to display the error page, because the response is already committed, but in this case you should have seen a fairly self-explaining
IllegalStateException: Response already committed
error in the appserver logs.