jsp 项目中 web.xml 的作用

每个 web 应用的 WEB-INF 路径下(而且必须位于该路径)的 web.xml 文件被称为配置描述符。

 

对于 java web 应用而言,WEB-INF 是一个特殊的文件夹,web 容器会包含该文件夹下的内容,客户浏览器无法访问 WEB-INF 路径下的任何内容。

 

在 servlet2.5 规范之前,java web 的绝大部分组件都通过 web.xml 文件来配置管理。

从 servlet3.0 开始,也可以通过注解来配置管理 web 组件,因此 web.xml 文件可以变得更加简洁。

 

web.xml 可以配置的内容:

1、配置 JSP

2、配置和管理 Servlet

3、配置和管理 Listener

4、配置和管理 Filter

5、配置标签库

6、配置 JSP 属性

 

除此之外,web.xml 还负责配置、管理如下常用内容。

1、配置和管理 JAAS 授权认证

2、配置和管理资源引用

3、web 应用首页

 

web.xml 文件的根元素是 <web-app .../> 元素,在 Servlet3.0 规范中,该元素新增了以下属性:

metadata-complete: 该属性接受 true 或 false 两个属性值。当为 true 的时候,该 web 应用将不会加载注解配置的 web 组件(如 Servlet、Filter、Listener 等)

 

配置首页的方法:

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

  

上面的配置指定该 web 应用首页依次是 index.html、index.htm、index.jsp。

如果 index.html 不存在,则由 index.htm 做首页,依次类推。

 

具体内容如下:

<?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"
  metadata-complete="true">

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

  

 

posted @ 2018-03-18 10:26  佚名000  阅读(578)  评论(0编辑  收藏  举报