首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Tomcat 核心配置技巧:虛擬目錄、端口、錯誤頁、Session 與 Filter

     編輯:Java 相關知識

整理 Tomcat 5/6 常用配置優化,涵蓋 server.xml 與 web.xml 關鍵設定。

1. 新增虛擬目錄

在 server.xml 的 <Host> 內添加:

<Context path="/oicq" docBase="myweb" debug="0" reloadable="true"/>
  • docBase:相對 webapps 的實體目錄
  • path:虛擬 URL 路徑,如 https://localhost/oicq

2. 修改服務端口

server.xml 約第 56 行:port="8080" → port="80"(需管理員權限)

3. web.xml 關鍵配置

歡迎頁面

<welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

錯誤頁面

<error-page>
  <error-code>404</error-code>
  <location>/notFileFound.jsp</location>
</error-page>
<error-page>
  <exception-type>java.lang.NullPointerException</exception-type>
  <location>/null.jsp</location>
</error-page>

標準錯誤頁模板

<%@ page isErrorPage="true" %>
出錯了:<p>錯誤訊息: <%= exception.getMessage() %></p>
Stack Trace:
<font color="red">
<%
  java.io.CharArrayWriter cw = new java.io.CharArrayWriter();
  java.io.PrintWriter pw = new java.io.PrintWriter(cw, true);
  exception.printStackTrace(pw);
  out.println(cw.toString());
%>
</font>

Session 逾時 (分鐘)

<session-config>
  <session-timeout>30</session-timeout>
</session-config>

Filter 配置

<filter>
  <filter-name>FilterSource</filter-name>
  <filter-class>project4.FilterSource</filter-class>
</filter>
<filter-mapping>
  <filter-name>FilterSource</filter-name>
  <url-pattern>/WwwServlet</url-pattern>
</filter-mapping>

常見 Filter 類型與鏈路順序

  1. 身份驗證
  2. 日誌審計
  3. 圖片轉換
  4. 資料壓縮
  5. 加密解密
  6. Token 處理
  7. 資源存取事件觸發
  8. XSLT 轉換
  9. MIME 類型鏈

Filter 執行順序由 web.xml 宣告順序決定,建議:安全 → 資源 → 內容類型 → 其他。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved