web.xml是Java Web应用的部署描述符,必须置于WEB-INF目录下,其根元素为,需指定匹配Servlet规范的version及对应命名空间与schemaLocation。

web.xml 是 Java Web 应用的传统部署描述符(Deployment Descriptor),用于配置 Servlet、Filter、Listener、欢迎页、错误页等核心行为。虽然从 Servlet 3.0+ 开始支持注解(如 @WebServlet)替代部分配置,但 web.xml 仍广泛用于集中管理、兼容老项目或需要明确声明的场景。
web.xml 放在哪?基本结构长什么样?
它必须放在项目的 WEB-INF/ 目录下(即 src/main/webapp/WEB-INF/web.xml),且文件名严格为 web.xml。最简合法结构如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<p><!-- 配置内容写在这里 --></p><p></web-app>立即学习“Java免费学习笔记(深入)”;
注意:version 属性要和你用的 Servlet 规范匹配(常见 3.1、4.0、5.0),对应不同命名空间和 schemaLocation,否则启动会报错。
怎么注册一个 Servlet?
即使用了 @WebServlet,有时仍需在 web.xml 中显式注册,比如要指定加载顺序、设置初始化参数,或兼容旧容器。
- 用
<servlet></servlet>声明 Servlet 类名和别名 - 用
<servlet-mapping></servlet-mapping>绑定 URL 模式
示例:注册名为 MyServlet 的类,映射到 /hello
生成可在 deepbi 沙箱 iframe 内运行或作为独立文件运行的交互式 HTML 页面,使用 `window.deepbi` JS SDK 实现数据查询、空间管理、文件浏览及用户操作。当用户提出以下需求时启用该能力: - 创建 KPI 仪表盘、数据表格、文件浏览器、成员列表、管理页面; - 明确要求“转成 dhtml”、“转译成 HTML”、“把 XX 页面转成……”、“控制台页面”、“管理后台”、“DeepBI 页面迁移”; - 提及位于 `apps/console/src/featur
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.example.MyServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>msg</param-name>
<param-value>Hello from web.xml</param-value>
</init-param>
</servlet>
<p><servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>说明:<load-on-startup></load-on-startup> 值越小越早加载(负数表示不预加载);<init-param></init-param> 可在 Servlet 的 init() 方法中通过 getInitParameter() 获取。
怎么配置 Filter 和 Listener?
Filter 用于拦截请求/响应(如编码过滤、权限校验),Listener 用于监听应用生命周期事件(如上下文启动、Session 创建)。
-
<filter></filter>+<filter-mapping></filter-mapping>:类似 Servlet,先声明再映射 -
<listener></listener>:直接写全限定类名,无需映射
示例:添加字符编码 Filter 和上下文监听器
<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>
</filter>
<p><filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></p><p><listener>
<listener-class>com.example.MyContextListener</listener-class>
</listener>其他常用配置项
这些不是必须的,但很实用:
-
<welcome-file-list></welcome-file-list>:定义默认访问页,如 index.html、index.jsp -
<error-page></error-page>:按状态码或异常类型跳转错误页(如 404 → /404.html,Exception → /error.jsp) -
<context-param></context-param>:全局上下文参数,可通过ServletContext.getInitParameter()获取,常用于框架配置(如 Spring 的 contextConfigLocation) -
<session-config></session-config>:配置 Session 超时(单位:分钟)、HttpOnly、Secure 等安全属性
示例片段:
<welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <p><error-page> <error-code>404</error-code> <location>/404.html</location> </error-page></p><p><context-param> <param-name>logLevel</param-name> <param-value>DEBUG</param-value> </context-param></p><p><session-config> <session-timeout>30</session-timeout> <cookie-config> <http-only>true</http-only> <secure>true</secure> </cookie-config> </session-config>
基本上就这些。web.xml 不复杂但容易忽略命名空间和版本匹配,写完建议用 IDE 校验或部署时看控制台是否报 schema 错误。现代项目可逐步迁移到注解 + Java Config,但理解 web.xml 仍是读懂传统 Java Web 工程的基础。

















