概述
在Web应用中,有着其一定的文件和目录结构,其中有个十分重要的文件叫部署描述符(如web.xml),这些文件一般都使用XML进行描述,并且对整个应用起着支撑作用。
考试中这部分的内容的考察是重中之重,希望大家予以重视。
web.xml配置中的常规配置
各种常规的配置主要有:
1)该web.xml文件的版本等信息描述,如:
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> </web-app>
2)显示名称、描述信息、会话超时,如:
<web-app>
<description>这是一个样例web.xml</description>
<display-name>web.xml Examples</display-name>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
3)上下文初始化参数, 当一个Web应用启动的时候,服务器会先去读其配置文件中的<context-param>和<listener>元素,这样我们就可以在项目未启动的时候初始化一些参数,比如如果需要在项目启动前打开数据库,这时就可以在<context-param>中配置数据库的连接方式,然后在<listener>中初始化数据库来进行连接,最后再在Listener类中销毁数据库的资源,如:
<web-app>
<context-param>
<description>在项目启动时初始化该参数</description>
<param-name>init-param</param-name>
<param-value>param-value</param-value>
</context-param>
</web-app>
4)Web应用的监听程序,如:
<web-app>
<listener>
<description>负责监听请求</description>
<listener-class>listener.MyRequestListener</listener-class>
</listener>
</web-app>
web.xml配置中的Servlet配置
关于Servlet类的创建在前面的章节已经介绍过,当完成Servlet类的代码后,还需要在web.xml中配置,下面是一个比较完整的Servlet类的配置,如:
<web-app>
<servlet>
<description>这是一个输出Hello world的Servlet</description>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>servlet.HelloWorldServlet</servlet-class>
<init-param>
<description>指定字符编码方式</description>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
<!--以下是Servlet的安全配置-->
<!--注意必须先在安全配置那里设定好角色才行,详细见后续章节中的安全配置-->
<run-as>
<role-name>admin</role-name>
</run-as>
<security-role-ref>
<description>管理员用户</description>
<role-name>admin</role-name>
<role-link>admin</role-link>
</security-role-ref>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorldServlet</url-pattern>
</servlet-mapping>
</web-app>
web.xml配置中的过滤器配置
过滤器的编写在前面的《SCWCD之路——Servlet技术》 已经介绍过,这里不再赘述。过滤器在web.xml中的配置主要包括过滤器的元素和过滤器的映射,如:
<web-app>
<filter>
<description>对客户端请求统一编码</description>
<filter-name>EncodingFilter</filter-name>
<filter-class>filter.EncodingFilter</filter-class>
<init-param>
<description>初始化参数</description>
<param-name>init-param</param-name>
<param-value>param-value</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<!--这里表示所有页面和请求动作都进行过滤-->
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
</web-app>
web.xml配置中的页面配置
1)欢迎页面,主要指打开项目时的默认打开页面的优先级别,如:
<web-app>
<welcome-file-list>
<welcome-file>admin.jsp</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2) 错误页面,这里需要注意的是错误代码和异常类型不能同时被配置 。如果同时配置了则默认使用异常类型,下面的例子是为了全面介绍才把两者都同时罗列了出来,如:
<web-app>
<error-page>
<exception-type>Exception</exception-type>
<error-code>404</error-code>
<location>/error.jsp</location><!—注意必须以/开始-->
</error-page>
</web-app>
3)JSP的属性组,这是在JSP 2.0才出现的,比如对于一些Web应用来说,Scriptlets是不希望出现的东西,因为它们混淆了Java代码和HTML代码,使得程序的维护十分麻烦,这个时候就可以使用属性组来限定它,另外EL也能被禁止出现,如:
<web-app>
<jsp-config>
<jsp-property-group>
<description>给所有JSP页面做一些限定</description>
<display-name>JSP属性组</display-name>
<url-pattern>*.jsp</url-pattern>
<page-encoding>gb2312</page-encoding>
<!--因为是介绍,所以在这里把所有的都列出来-->
<scripting-invalid>true</scripting-invalid>
<el-ignored>true</el-ignored>
<is-xml>true</is-xml>
<deferred-syntax-allowed-as-literal>true</deferred-syntax-allowed-as-literal>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
</jsp-property-group>
</jsp-config>
</web-app>
4)标记库描述文件(tld),当使用EL或自定义标签时常常需要自己编写TLD文件,写完TLD文件之后需要在web.xml中说明,如:
<web-app>
<jsp-config>
<taglib>
<taglib-uri>http://localhost/tlds/mytag.tld</taglib-uri>
<taglib-location>/WEB-INF/tlds/mytag.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
web.xml配置中的引用配置
1)资源引用,如:
<web-app>
<resource-ref>
<description>这是一个资源引用</description>
<res-ref-name>资源引用名称</res-ref-name>
<!--资源引用的类型有四种-->
<!--javax.jms.ConectionFactory-->
<!--javax.mail.Session-->
<!--javax.net.URL-->
<res-type>javax.sql.DataSource</res-type>
<!--资源认证方式有两种-->
<!--Application-->
<res-auth>Container</res-auth>
<!--共享范围有两种-->
<!--Unshareable-->
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
</web-app>
2)资源环境引用
<web-app>
<resource-env-ref>
<description>这是一个资源环境引用</description>
<resource-env-ref-name>资源名称</resource-env-ref-name>
<!--资源环境引用一共有两种类型-->
<!--javax.jms.Topic-->
<resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
</resource-env-ref>
</web-app>
3)EJB引用
<web-app>
<ejb-ref>
<description>这是一个EJB引用</description>
<ejb-ref-name>EJB引用名称</ejb-ref-name>
<!--EJB的引用类型有Session和Entity两种-->
<ejb-ref-type>Session</ejb-ref-type>
<home>home interface</home>
<remote>remote interface</remote>
<ejb-link>链接的EJB</ejb-link>
</ejb-ref>
</web-app>
4)本地EJB引用
<web-app>
<!--本地EJB引用-->
<ejb-local-ref>
<description>本地EJB引用</description>
<ejb-ref-name>EJB引用名</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>local home interface</local-home>
<local>local interface</local>
</ejb-local-ref>
</web-app>
5)消息目标引用
<web-app>
<message-destination-ref>
<description>这是一个消息目标引用</description>
<message-destination-ref-name>引用名</message-destination-ref-name>
<!--消息目标引用的类型有两种-->
<!--javax.jms.Topic-->
<message-destination-type>javax.jms.Queue</message-destination-type>
<!--消息用法有三种-->
<!--Produces-->
<!--ConsumesProduces-->
<message-destination-usage>Consumes</message-destination-usage>
<message-destination-link>指消息目标的链接</message-destination-link>
</message-destination-ref>
</web-app>
web.xml配置中的安全配置
1)登录配置,登录配置的方式有四种,详细请见《SCWCD之路——Web应用的安全性》 ,这里只给出一个简单的例子,如:
<web-app>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>这是一个BASIC认证</realm-name>
</login-config>
</web-app>
2)安全角色配置,安全角色可以由程序员根据Web应用的需要而创建各种安全角色,比如下面是一个创建管理者的角色的例子,如:
<web-app>
<security-role>
<description>管理者角色</description>
<role-name>admin</role-name>
</security-role>
</web-app>
3)安全约束配置,安全约束是为了约束某些资源的被访问而创建的,一个Web应用可以根据需要创建多种不同的安全约束,如:
<web-app>
<security-constraint>
<display-name>安全约束的名称</display-name>
<!--受限资源-->
<web-resource-collection>
<web-resource-name>管理者的页面</web-resource-name>
<description>限制其他用户访问管理页面</description>
<!--被保护的资源,一般是一些JSP页面-->
<url-pattern>admin/*.jsp</url-pattern>
<!--受保护的访问方法,可以保护多种访问方法-->
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<!--授予权限,表明那些身份可以访问上述受保护资源-->
<auth-constraint>
<description>允许访问该资源的角色</description>
<role-name>admin</role-name>
</auth-constraint>
<!--用户数据约束-->
<user-data-constraint>
<description>这是用户数据约束</description>
<!--下面是传输保证类型,一共有三种-->
<!--NONE、INTEGRAL、CONFIDENTIAL-->
<transport-guarantee>NONE</transport-guarantee>
</user-data-constraint>
</security-constraint>
</web-app>
web.xml配置中的其他配置
1)让Web应用支持集群服务器,除了要让所有放入session中的对象都实现序列化接口外,还要在web.xml中进行配置以告诉容器,如:
<web-app>
<distributable />
</web-app>
2)配置MIME的对应元素,让容器知道某种文件对应的MIME类型,如:
<web-app>
<mime-mapping>
<!--让后缀名为mpg的文件对应到MIME类型为video/mpeg-->
<extension>mpg</extension>
<mime-type>video/mpeg</mime-type>
</mime-mapping>
</web-app>
本文详细介绍web.xml文件的各种配置项,包括常规配置、Servlet配置、过滤器配置、页面配置、引用配置及安全配置等内容。
141

被折叠的 条评论
为什么被折叠?



