要自定義Debian上Tomcat的錯誤頁面,請按照以下步驟操作:
登錄到您的Debian服務器。
使用文本編輯器打開Tomcat的默認錯誤頁面。這些文件通常位于/usr/share/tomcat9/webapps/manager/META-INF/
目錄下。您可以使用nano或vim等文本編輯器。例如,要使用nano編輯器,請運行以下命令:
sudo nano /usr/share/tomcat9/webapps/manager/META-INF/context.xml
<Context>
標簽內,添加以下代碼以啟用自定義錯誤頁面:<ErrorReportValve enabled="true" showReport="false" showServerInfo="false">
<ErrorPage errorCode="404" location="/custom_404.html"/>
<ErrorPage errorCode="500" location="/custom_500.html"/>
</ErrorReportValve>
這里,errorCode
屬性表示HTTP狀態碼,location
屬性表示自定義錯誤頁面的路徑。您可以根據需要添加更多錯誤代碼和自定義頁面。
保存并關閉文件。
創建自定義錯誤頁面。在/usr/share/tomcat9/webapps/manager/
目錄下創建一個新的HTML文件,例如custom_404.html
和custom_500.html
。使用文本編輯器打開它們,并添加您希望在錯誤發生時顯示的自定義內容。例如:
<!-- custom_404.html -->
<!DOCTYPE html>
<html>
<head>
<title>自定義404錯誤頁面</title>
</head>
<body>
<h1>抱歉,您訪問的頁面不存在。</h1>
<p><a href="/">返回首頁</a></p>
</body>
</html>
sudo systemctl restart tomcat9
現在,當用戶遇到404或500錯誤時,他們將看到您自定義的錯誤頁面。您可以根據需要為其他錯誤代碼創建更多的自定義錯誤頁面。