简体   繁体   中英

How to extend ErrorReportValve in Tomcat on Azure PaaS?

I am attempting to implement custom error pages on Tomcat, running on PaaS in Azure.

Adding the following to META-INF/context.xml, as per Which is the best way to mask / hide tomcat version from error pages?

<Valve className="org.apache.catalina.valves.ErrorReportValve"
    showReport="false" 
    showServerInfo="false"/> 

will show a bare-bones error page, which does hide exceptions etc. from the user but is neither pretty, nor does it send the actual error message to the log.

在此处输入图像描述

So ideally, I would like to try extending the custom ErrorReportValve.

The following gives instructions how to set a custom ErrorReportValve in Tomcat How to set a custom ErrorReportValve in Tomcat? . Or see @martinnemec3's answer in tomcat8 - custom error page for 400

Is there any way of implementing this solution in Tomcat running on PaaS on Azure, where there is no access to server.xml , but there is META-INF/context.xml ?

For instance, putting

<Host errorReportValveClass="org.valves.CustomErrorReportValve"  />

in the context.xml ?

Tomcat Web App enabled through Portal Application Settings normally cannot be customized. The preferable way if you need to customize is to deploy via the Azure marketplace tomcat image. However, you can still customize via a workaround with your current product. Here is how. First, we need to start tomcat with custom server.xml.

 <configuration> <system.webServer> <handlers> <remove name="httpPlatformHandlerMain" /> <add name="httpPlatformHandlerMain" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/> </handlers> <httpPlatform processPath="%programfiles(x86)%\apache-tomcat 8.0.23\bin\startup.bat" arguments="-config D:\home\site\wwwroot\conf\server.xml" requestTimeout="00:04:00" startupTimeLimit="60" startupRetryCount="3" stdoutLogEnabled="true"> <environmentVariables> <environmentVariable name="CATALINA_OPTS" value="-Xms1024m -Xmx1024m -Dport.http=%HTTP_PLATFORM_PORT% -Dsite.logdir=d:/home/LogFiles/ -Dsite.tempdir=d:\home\site\workdir" /> </environmentVariables> </httpPlatform> <applicationInitialization> <add initializationPage="/examples/servlets/servlet/HelloWorldExample" /> </applicationInitialization> </system.webServer> </configuration>

In web.config add arguments to point to your server.xml file. Sample below: server.xml file should be copied from either tomcat installation or you can use below. Note that we need to change the path for tomcat-users.xml, if you intend to use manager, admin-gui etc.

 <?xml version='1.0' encoding='utf-8'?> <Server port="-1" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="D:\home\site\wwwroot\conf\tomcat-users.xml" /> </GlobalNamingResources> <Service name="Catalina"> <Connector port="${port.http}" protocol="HTTP/1.1" address="127.0.0.1" connectionTimeout="20000" /> <Engine name="Catalina" defaultHost=""> <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/></Realm> <Host name="localhost" appBase="d:\home\site\wwwroot\webapps" xmlBase="d:\home\site\wwwroot\" unpackWARs="true" autoDeploy="true" workDir="${site.tempdir}"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="${site.logdir}" prefix="site_access_log." suffix=".txt"ww pattern="%h %l %u %t &quot;%r&quot; %s %b" /> </Host> </Engine> </Service> </Server>

Copy server.xml, tomcat-users.xml and tomcat-users.xsd files to d:\home\site\wwwroot\conf directory. Tomcat-users.xml file:

 <?xml version='1.0' encoding='utf-8'?> <tomcat-users xmlns="http://tomcat.apache.org/xml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd" version="1.0"> <role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/> </tomcat-users>

You can then customize anything you want for you tomcat install. You can also deploy manager, admin-gui code to webapps folder and enable those to deploy manager, admin gui etc.

This should help you to implement your custom server error pages. Please let me know if there are further questions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM