命名与定制URL。我们可以为Servlet和JSP文件命名并定制URL,其中定制URL是依赖命名的,命名必须在定制URL前。下面拿serlet来举例:
(1)、为Servlet命名:
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>org.whatisjava.TestServlet</servlet-class>
</servlet>
(1)、为Servlet命名:
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>org.whatisjava.TestServlet</servlet-class>
</servlet>
(2)、为Servlet定制URL、
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
定制初始化参数:可以定制servlet、JSP、Context的初始化参数,然后可以再servlet、JSP、Context中获取这些参数值。
下面用servlet来举例:
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>org.whatisjava.TestServlet</servlet-class>
<init-param>
<param-name>userName</param-name>
<param-value>Daniel</param-value>
</init-param>
<init-param>
<param-name>E-mail</param-name>
<param-value>125485762@qq.com</param-value>
</init-param>
</servlet>
经过上面的配置,在servlet中能够调用getServletConfig().getInitParameter("param1")获得参数名对应的值。
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>org.whatisjava.TestServlet</servlet-class>
<init-param>
<param-name>userName</param-name>
<param-value>Daniel</param-value>
</init-param>
<init-param>
<param-name>E-mail</param-name>
<param-value>125485762@qq.com</param-value>
</init-param>
</servlet>
经过上面的配置,在servlet中能够调用getServletConfig().getInitParameter("param1")获得参数名对应的值。
<
description
>
Spring MVC Test
</
description
>
< display-name >springMVC </ display-name >
<!-- CharacterEncodingFilter类具有encoding和forceEncoding两个属性,其中encoding是表示设置request的编码,forceEncoding表示是否同时设置response的编码。 <filter>下的<filter-name>内的值和<filter-mapping>下的<filter-name>内的值要完全一致 -->
< filter >
< filter-name >characterEncodingFilter </ 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 >characterEncodingFilter </ filter-name >
<!-- /下的所有请求都为UTF-8编码 -->
< url-pattern >/* </ url-pattern >
</ filter-mapping >
<!-- Spring MVC相关设置 <servlet>下的<servlet-name>内的值和<servlet-mapping>下的<servlet-name>内的值要完全一致 -->
< servlet >
< servlet-name >SpringMVC Test </ servlet-name >
<!-- DispatcherServlet主要负责流程的控制 -->
< servlet-class >org.springframework.web.servlet.DispatcherServlet </ servlet-class >
<!-- Spring MVC配置文件路径 -->
< init-param >
< param-name >contextConfigLocation </ param-name >
< param-value >/WEB-INF/springMVC-servlet.xml </ param-value >
</ init-param >
<!-- 表示启动容器时初始化该Servlet -->
< load-on-startup >1 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >SpringMVCTest </ servlet-name >
<!-- 表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。 -->
< url-pattern >/ </ url-pattern >
</ servlet-mapping >
< display-name >springMVC </ display-name >
<!-- CharacterEncodingFilter类具有encoding和forceEncoding两个属性,其中encoding是表示设置request的编码,forceEncoding表示是否同时设置response的编码。 <filter>下的<filter-name>内的值和<filter-mapping>下的<filter-name>内的值要完全一致 -->
< filter >
< filter-name >characterEncodingFilter </ 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 >characterEncodingFilter </ filter-name >
<!-- /下的所有请求都为UTF-8编码 -->
< url-pattern >/* </ url-pattern >
</ filter-mapping >
<!-- Spring MVC相关设置 <servlet>下的<servlet-name>内的值和<servlet-mapping>下的<servlet-name>内的值要完全一致 -->
< servlet >
< servlet-name >SpringMVC Test </ servlet-name >
<!-- DispatcherServlet主要负责流程的控制 -->
< servlet-class >org.springframework.web.servlet.DispatcherServlet </ servlet-class >
<!-- Spring MVC配置文件路径 -->
< init-param >
< param-name >contextConfigLocation </ param-name >
< param-value >/WEB-INF/springMVC-servlet.xml </ param-value >
</ init-param >
<!-- 表示启动容器时初始化该Servlet -->
< load-on-startup >1 </ load-on-startup >
</ servlet >
< servlet-mapping >
< servlet-name >SpringMVCTest </ servlet-name >
<!-- 表示哪些请求交给Spring Web MVC处理, “/” 是用来定义默认servlet映射的。也可以如“*.html”表示拦截所有以html为扩展名的请求。 -->
< url-pattern >/ </ url-pattern >
</ servlet-mapping >
指定错误处理页面,可以通过“异常类型”或“错误码”来指定错误处理页面。
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
-----------------------------
<error-page>
<exception-type>java.lang.Exception<exception-type>
<location>/exception.jsp<location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
-----------------------------
<error-page>
<exception-type>java.lang.Exception<exception-type>
<location>/exception.jsp<location>
</error-page>
设置过滤器:比如设置一个编码过滤器,过滤所有资源
<filter>
<filter-name>XXXCharaSetFilter</filter-name>
<filter-class>net.test.CharSetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XXXCharaSetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>XXXCharaSetFilter</filter-name>
<filter-class>net.test.CharSetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>XXXCharaSetFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
设置监听器:
<listener>
<listener-class>net.test.XXXLisenet</listener-class>
</listener>
<listener>
<listener-class>net.test.XXXLisenet</listener-class>
</listener>
设置会话(Session)过期时间,其中时间以分钟为单位,假如设置60分钟超时:
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<session-config>
<session-timeout>60</session-timeout>
</session-config>
<!-- 监听spring上下文容器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 加载spring的xml配置文件到 spring的上下文容器中 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:root-context.xml</param-value>
</context-param>
<!-- 配置Spring MVC DispatcherServlet -->
<servlet>
<servlet-name>MVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 初始化参数 -->
<init-param>
<!-- 加载SpringMVC的xml到 spring的上下文容器中 -->
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/mvc*.*
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 配置DispatcherServlet所需要拦截的 url -->
<servlet-mapping>
<servlet-name>MVC</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>