Capture 404/500 errors, custom error pages Tomcat

When the server 404, 500 error occurs when want to give a user-friendly interface reality
only need to add some configuration web.xml project

Java code
  1.   
  2.   
  3. <! -  400 Error ->   
  4. <error-page>   
  5. <error-code>400</error-code>   
  6. <location>/error.jsp</location>   
  7. </error-page>   
  8. <! -  404  page error does not exist ->   
  9. <error-page>   
  10. <error-code>404</error-code>   
  11. <location>/error.jsp</location>   
  12. </error-page>   
  13. <! -  500  Internal Server Error ->   
  14. <error-page>   
  15. <error-code>500</error-code>   
  16. <location>/error.jsp</location>   
  17. </error-page>   
  18. <-! Java.lang.Exception exception error, you can define multiple similar error based on the tag ->   
  19. <error-page>   
  20. <exception-type>java.lang.Exception</exception-type>   
  21. <location>/error.jsp</location>   
  22. </error-page>   
  23. <-! Java.lang.NullPointerException exception error, you can define multiple similar error based on the tag ->   
  24. <error-page>   
  25. <exception-type>java.lang.NullPointerException </exception-type>   
  26. <location>/error.jsp</location>   
  27. </error-page>   
  28.   
  29. <error-page>   
  30. <exception-type>javax.servlet.ServletException</exception-type>   
  31. <location>/error.jsp</location>   
  32. </error-page> 

 

In order to get a good user experience, that should not be exposed to 404 such pages to the user,
the starting point of the problem is that I custom error page in Struts2,
in Struts2 is defined as:

Xml Code
  1. <default-action-ref name="pagenotfound"></default-action-ref>  
  2.   
  3. <action name="pagenotfound">  
  4.             <result>/pagenotfound.html</result>  
  5. </action>  
<default-action-ref name="pagenotfound"></default-action-ref>

<action name="pagenotfound">
			<result>/pagenotfound.html</result>
</action>



This means that the access action Yes. If you do not find the action you visit this page,
but I do not if I .do or .action style, and direct use .jsp or .html way to access the page, please, struts will not processing the result is a 404 error is still there.
now deal with the scope of the struts is not, then this should be the scope of processing the application, subject to verification in the project web.xml can set custom error pages, set as follows:

Xml Code
  1. <error-page>  
  2.         <error-code>404</error-code>  
  3.         <location>/pagenotfound.html</location>  
  4. </error-page>  
<error-page>
		<error-code>404</error-code>
		<location>/pagenotfound.html</location>
</error-page>



Now and then visit the page below the project does not exist, the page will jump to pagenotfound custom, so that the default-action-ref configuration of the struts can be removed up because 404 to deal with the tomcat

Reproduced in: https: //my.oschina.net/usenrong/blog/197847

Guess you like

Origin blog.csdn.net/weixin_34336526/article/details/92028961