web.xml中的配置
頭資訊:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
</web-app>
配置編碼的過濾器
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern>
</filter-mapping>
配置過濾器
<filter><filter-name>名字</filter-name>
<filter-class>包名.類名</filter-class>
</filter>
<filter-mapping>
<filter-name>同上相同的名字</filter-name>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.action</url-pattern>//適用於以.jsp .action結尾的servlet路徑
</filter-mapping>
還有個<dispatcher></dispatcher>標籤,配置到達servlet的方式。有4種取值:REQUEST、FORWARD、INCLUDE、ERROR。如果沒有配置任何<dispatcher>,預設為REQUEST。
配置監聽器
<listener><listener-class>包名.類名</listener-class>
</listener>
配置<context-param>
<context-param>配置的是一組鍵值對,如下
<context-param>
<param-name>引數名</param-name>
<param-value>引數值</param-value>
</context-param>
當伺服器啟動時,伺服器會讀取web.xml配置,當讀到<listener></listener>和<context-param></context-param>這兩個節點的時候,容器會將這兩個節點set到ServletContext(上下文物件)中,這樣我們在程式中就能通過這個上下文物件去取得我們這個配置值。
String parameterValue = getServletContext().getInitParameter("引數名");
配置session超時時間
<session-config><session-timeout>1440</session-timeout>
</session-config>