---
title: "DispatcherServlet在web.xml中的配置"
author: "xiaoyixiao"
date: "2015-04-03T11:16:15Z"
tags: []
categories: ["编程语言"]
slug: "dispatcherservlet-web-xml"
draft: false
lastmod: "2015-04-02T21:13:26Z"
id: 2198523
url: "https://xiaoyixiao.iteye.com/blog/2198523"
---

```
<servlet>  
        <servlet-name>chapter2</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>chapter2</servlet-name>  
        <url-pattern>/</url-pattern>  
    </servlet-mapping>
```

DispatcherServlet也可以配置自己的初始化参数，覆盖默认配置：   
摘自Spring Reference   
参数   
描述   
contextClass   
实现WebApplicationContext接口的类，当前的servlet用它来创建上下文。如果这个参数没有指定， 默认使用XmlWebApplicationContext。   
contextConfigLocation   
传给上下文实例（由contextClass指定）的字符串，用来指定上下文的位置。这个字符串可以被分成多个字符串（使用逗号作为分隔符） 来支持多个上下文（在多上下文的情况下，如果同一个bean被定义两次，后面一个优先）。   
namespace   
WebApplicationContext命名空间。默认值是\[server-name]-servlet。   
因此我们可以通过添加初始化参数  

```
<servlet>  
      <servlet-name>chapter2</servlet-name>  
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
      <load-on-startup>1</load-on-startup>  
      <init-param>  
          <param-name>contextConfigLocation</param-name>  
          <param-value>classpath:spring-servlet-config.xml</param-value>  
      </init-param>  
  </servlet>
```