Tomcat: Custom a common error page valve for all web application in tomcat, tomcatvalve

Source: Internet
Author: User

Tomcat: Custom a common error page valve for all web application in tomcat, tomcatvalve

If you deploy multiple Web applications on a Tomcat Server, you want these Web applications to share a set of error pages instead of using the default error pages. You need to customize the error page.

You can configure error pages in each web application through error-page. However, when multiple Web applications are used, it is a little troublesome to configure an error page in the web. xml of each application. However, you cannot configure error pages in tomcat/conf/web. xml. Because Tomcat will merge the public web. xml with the web. xml of each web application during application deployment, the search page is still found in the directory of each web application.

To achieve the shared directory, you can only rewrite the default Implementation of Tomcat by yourself. Fortunately, Tomcat also supports us to do this.

Package com. fjn. frame. catalina. valves; import java. io. IOException; import java. io. inputStream; import java. io. writer; import java. util. logging; import org. apache. catalina. connector. request; import org. apache. catalina. connector. response; import org. apache. catalina. util. requestUtil; import org. apache. catalina. util. serverInfo; import org. apache. catalina. valves. errorReportValve;/***** to use this class to customize the error page, you need to adjust the serve The errorReportValveClass attribute value of the Host in r. xml is this class. * In addition, You Need To package this class into a jar package in the tomcat/lib directory. * @ Since tomcat 6.0 * @ author fs1194361820@163.com June 4, 2015 **/public class ErrorPageValve extends ErrorReportValve {private static final String ERROR_PAGE_NAME = "errorPage.html"; @ Override protected void report (Request request, response response, Throwable throwable) {// Do nothing on non-HTTP responses int statusCode = response. getStatus (); // Do nothing on a 1xx, 2xx and 3xx status // Do n Othing if anything has been written already if (statusCode <400 | response. getContentCount ()> 0 |! Response. isError () {return;} String message = RequestUtil. filter (response. getMessage (); if (message = null) {if (throwable! = Null) {String exceptionMessage = throwable. getMessage (); if (exceptionMessage! = Null & exceptionMessage. length ()> 0) {message = RequestUtil. filter (new partition (exceptionMessage )). nextLine () ;}}if (message = null) {message = "";}} // Do nothing if there is no report for the specified status code String report = null; try {report = sm. getString ("http. "+ statusCode);} catch (Throwable t) {;} if (report = null) return; StringBuffer sb = new StringBuffer (); try {BuildHTMLFromErrorPage (sb);} catch (Exception ex) {}if (sb. length () <1) {defaultBuildHTML (sb, statusCode, message, report, throwable);} try {response. setContentType ("text/html"); response. setCharacterEncoding ("UTF-8");} catch (Throwable t) {if (container. getLogger (). isDebugEnabled () container. getLogger (). debug ("status. setContentType ", t);} Writer writer = response. getReporter (); If (writer! = Null) {// If writer is null, it's an indication that the response has // been hard committed already, which showould never happen writer. write (sb. toString () ;}} catch (IOException e) {;} catch (IllegalStateException e) {;}} private void defaultBuildHTML (StringBuffer sb, int statusCode, String message, String report, throwable throwable) {sb. append ("

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.