第10讲部署描述符文件


本讲教学目标:熟悉部署描述符文件中各个元素的作用与使用方法

部署描述符文件的作用

web.xml叫做部署描述符文件,是在Servlet规范中定义的,是web应用的配置文件。

作用:描述了容器运行程序所需要的信息,提供站点的配置设定

web.xml文件是XML文档

  1. 必须以XML声明开头,指出XML版本及字符编码
  2. 顶层(根)元素为,元素名大小写敏感
    web-App和WEB-APP都是不合法的,web-app必须用小写
<?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" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
	id="WebApp_ID" 
	version="3.0">
</web-app>
  1. 各元素对出现在其他元素中的次序敏感

  2. 可省略某些可选元素,但不能将其放在不正确的位置

  3. xsd文档的作用是定义XML文档的合法构建模块,类似dtd

  4. 学习XML Schema(.xsd)教程

    • http://www.w3school.com.cn/schema/index.asp
    • http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

常用元素的声明与配置

icon

包含small-icon和large-icon元素,为大型和小型GIF或JPEG图标图片指定文件名

用于在GUI工具中表示父元素。

<icon>
	<small-icon>/images/small.gif</small-icon>
	<large-icon>/images/large.gif</large-icon>
</icon>

display-name

定义应用的名称

<display-name>myFirstApp</display-name>

description

对应用做出描述

<description>测试应用V1.0</description>

context-param

context-param 应用范围内初始化参数

- <param-name>参数名称</param-name>
- <param-value></param-value>

范例
<context-param>
	<param-name>param_name</param-name>
	<param-value> param_value</param-value>
</context-param>

filter

声明了Web应用程序中的过滤器

– filter-name元素是过滤器的逻辑名称。
– filter-class是过滤器的完全限定类名。
– init-param元素包含的名-值对作为此过滤器的初始化参数。
– 当指定可选的async-supported元素时,表示该过滤器支持异步请求处理。

<filter>
	<filter-name>authorizefilter</filter-name>
	<filter-class>onest.web.filter.AuthorizeFilter</filter-class>
</filter>

filter-mapping

容器使用filter-mapping决定哪个过滤器以什么样的顺序应用到请求。

– filter-name的值必须是部署描述符中声明过的一个过滤器中。
– 匹配的请求可以被指定为url-pattern或servlet-name。

<filter>
	<filter-name>authorizefilter</filter-name>
	<filter-class>onest.web.filter.AuthorizeFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>authorizefilter</filter-name>
	<url-pattern>/admin/*</url-pattern>
</filter-mapping>

listener

指定事件监听程序

listener-class声明应用程序中的一个类必须注册为Web应用程序监听器bean。它的值是监听器类的完全限定类名。

<listener>
	<listener-class>
		onest.dev.ServletcontextListenerDemo
	</listener-class>
</listener>

String框架以后就要用

servlet

servlet元素用于声明一个servlet

jsp-file元素命名JSP页面,包含到以“/”开头的Web应用程序中一个JSP文件的完全路径。

所指的JSP文件可存放于WEB-INF目录

– servlet-name元素包含了servlet的规范名称。
– servlet-class包含了servlet的完全限定类名。
– load-on-startup元素表示该servlet应该在Web应用程序启动时加载。,参数是整数

<servlet>
	<servlet-name>HelloServlet</servlet-name>
		<jsp-file>/hello.jsp</jsp-file>  这行标签类似于ServletClass
	<load-on-startup>1</load-on-startup>
</servlet>

--------------------------------------------------------------
<servlet>
	<servlet-name>HelloServlet</servlet-name>
		<servlet-class>onest.HelloServlet</servlet-class>
	<load-on-startup>1</load-on-startup>
</servlet>

servlet-mapping

定义servlet和URL模式之间的映射。

servlet-name:定义Servlet的名称

url-pattern:使用servlet-mapping元素将定制的URL与刚分配的名称相关联

<servlet>
	<load-on-startup>1</load-on-startup>
	<servlet-name>HelloServlet</servlet-name>
	<servlet-class>onest.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>HelloServlet</servlet-name>
	<url-pattern>/hello</url-pattern>
</servlet-mapping>

session-config

控制会话超时

如果某个会话在一定的时间内未被访问,服务器可以把它扔掉以节约内存。

session-config和session-timeout元素给出一个适用于所有服务器的明确的超时
session-timeout元素的值的单位为 分钟

Session是存储在服务器内存的,不适宜时间太长。

<session-config>
	<session-timeout> 3 </session-timeout>
</session-config>

mime-mapping

关联文件与MIME类型,一般用于多媒体文件关联。

服务器一般都具有一种让Web站点管理员将文件扩展名与媒体相关联的方法。如:自动给予名为mom.jpg的文件一个image/jpeg的MIME类型

如果希望将特殊的文件发送到客户机时分配为某种MIME类型,使用mime-mepping元素

<mime-mapping>
	<extension>foo</extension>
	<mime-type>application/x-fubar</mime-type>
</mime-mapping>

welcome-file-list

指定欢迎页

如果一个URL给出了一个目录名但未给出文件名,服务器应该使用指定的欢迎页面

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

error-page

error-page元素来指定错误页面

包含一个错误代码或异常类型到Web应用程序中资源的路径之间的映射
– error-code错误代码
– exception-type包含了一个Java异常类型的完全限定类名称。
– location包含了web应用程序中相对于web应用程序根目录的资源位置。location的值必须以’/'开头。

<error-page>
	<error-code>500</error-code>
	<location>/500.jsp</location>
</error-page>

<error-page>
	<error-code>404</error-code>
	<location>/404.jsp</location>
</error-page>

<error-page>
	<exception-type>java.io.IOException</exception-type>
	<location>/error.jsp</location>
</error-page>

jsp-config

jsp-config用来提供Web应用程序中的JSP文件的全局配置信息。
– taglib元素可用来为Web应用程序中的JSP页面使用的标签库提供信息。使用<taglib>给TLD文件起一个别名
– jsp-property-group:给一定范围类的JSP文件设置一些特性

<jsp-config>
	<taglib>
		<taglib-uri>mytag</taglib-uri>
		<taglib-location>/WEB-INF/mytag.tld</taglib-location>
	</taglib>
</jsp-config>

提供安全性

利用web.xml中相关元素为服务器内建功能提供安全性:

- 指定验证的方法:<login-config>
- 限制对web资源的访问:<security-constraint>
- 分配角色名:<security-role>

login-config

配置认证方法

  • auth-method为Web应用程序配置认证机制
    - 该元素的内容必须是BASIC、DIGEST、FORM、CLIENT-CERT、或提供商指定的认证模式

  • realm-name表示为Web应用程序选择用于认证模式的领域名。

  • form-login-config指定应该用于基于表单登录的登录和错误页面

<login-config>
<auth-method>BASIC</auth-method>
</login-config>

security-constraint

使用<security-constraint>元素配置安全约束
<display-name>:可选,指定安全约束名称
<web-resource-collection>:指定安全约束应用的资源集合
<auth-constraint>:指定可访问受保护资源的角色
<user-data-constraint>:指定数据通讯的方式
<security-constraint>
	<web-resource-collection>
		<web-resource-name>test2</web-resource-name>
		<url-pattern>/*</url-pattern>
	</web-resource-collection>
	<auth-constraint>
		<role-name>tomcat1</role-name>
	</auth-constraint>
</security-constraint>

security-role

定义了一个安全角色。子元素role-name指定安全角色的名称。

<security-constraint>
	<web-resource-collection>
		<web-resource-name>test2</web-resource-name>
		<url-pattern>/*</url-pattern>
	</web-resource-collection>
	<auth-constraint>
		<role-name>tomcat1</role-name>
	</auth-constraint>
</security-constraint>
<security-role>
	<role-name>tomcat1</role-name>
</security-role>

JavaEE元素

- <distributable/>:支持集群的服务器可安全的在多个服务器上分布Web应用
- <resource-env-ref>:声明一个与某个资源有关的管理对象
- <resource-ref>:声明一个资源引用

distributable

在应用的web.xml加<distributable/> 即可

<?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" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
	id="WebApp_ID" 
	version="3.0">
<distributable/>
</web-app>

resource-env-ref

resource-env-ref有两个子元素:

  • resource-env-ref-name定义资源名,资源的名称相对于java:comp/env
  • resource-env-ref-type查找资源时返回的资源类型,当web应用查找该资源的时候,返回的Java类名的全称
<resource-env-ref>
<resource-env-ref-name>jdbc/mssql</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>

resource-ref

resource-ref元素包括五个子元素,利用JNDI取得应用可利用资源.

  1. description:资源说明
  2. rec-ref-name:资源名称
  3. res-type:资源种类
  4. res-auth:资源由Application或Container来许可
  5. res-sharing-scope:资源是否可以共享.默认值为Shareable
<resource-ref>
	<description>JNDI JDBC DataSource</description>
	<res-ref-name>jdbc/data</res-ref-name>
	<res-type>javax.sql.DataSoruce</res-type>
	<res-auth>Container</res-auth>
</resource-ref>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值