web.xml元素介绍

web.xml元素介绍

我将自己知道的web.xml的元素整理了一下:

web.xml首先是肯定要包含它的schema.

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
 version="2.4">

</web-app>

其它的元素都放在<web-app></web-app>之中。

<discription></discription> 是对站台的描述
<display-name></display-name> 定义站台的名称
<distributable/> 是指定该站台是否可分布式处理


<context-param></context-param> 用来设定web站台的环境参数,它包含两个子元素:
<param-name></param-name> 用来指定参数的名称
<param-value></param-value> 用来设定参数值

比如:
<context-param>
 <param-name>my_param</param-name>
 <param-value>hello</param-value>
</context-param>

在此设定的参数,可以在servlet中用 getServletContext().getInitParameter("my_param") 来取得


<filter></filter> 是用来声明filter的相关设定,它包含以下子元素:
<filter-name></filter-name> 这当然就是指定filter的名字
<filter-class></filter-class> 这是用来定义filter的类的名称
<init-param></init-param> 用来定义参数,它有两个子元素:
<param-name></param-name> 用来指定参数的名称
<param-value></param-value> 用来设定参数值

比如:
<filter>
 <filter-name>SetCharacterEncoding</filter-name>
 <filter-class>com.myTest.SetCharacterEncodingFilter</filter-class>
 <init-param>
  <param-name>encoding</param-name>
  <param-value>GB2312</param-value>
 </init-param>
</filter>

与<filter></filter>同时使用的是<filter-mapping></filter-mapping> 用来定义filter所对应的URL,它有两个子元素:
<filter-name></filter-name> 指定filter的名字
<url-pattern></url-pattern> 指定filter所对应的URL

比如:
<filter-mapping>
 <filter-name>SetCharacterEncoding</filter-name>
 <url-pattern>/*</url-pattern>
</filter-mapping>


<listener></listener> 用来设定Listener接口,它的主要子元素为
<listener-class></listener-class> 定义Listener的类名称

比如:
<listener>
 <listener-class>com.myTest.ContextListener</listener-class>
</listener>


<servlet></servlet> 用来声明一个servlet的数据,主要有以下子元素:
<servlet-name></servlet-name> 指定servlet的名称
<servlet-class></servlet-class> 指定servlet的类名称
<jsp-file></jsp-file> 指定web站台中的某个JSP网页的完整路径
<init-param></init-param> 用来定义参数,和前面的<init-param>差不多

同样,与<servlet></servlet>一起使用的是<servlet-mapping></servlet-mapping> 用来定义servlet所对应的URL,包含两个子元素:
<servlet-name></servlet-name> 指定servlet的名称
<url-pattern></url-pattern> 指定servlet所对应的URL

比如:
<servlet>
 <servlet-name>ShoppingServlet</servlet-name>
 <servlet-class>com.myTest.ShoppingServlet</servlet-class>
</servlet>

<servlet-mapping>
 <servlet-name>ShoppingServlet</servlet-name>
 <url-pattern>/shop/ShoppingServlet</url-pattern>
</servlet-mapping>


<session-config></session-config> 用来定义web站台中的session参数,包含一个子元素:
<session-timeout></session-timeout> 用来定义这个web站台所有session的有效期限,单位为 分钟


<mime-mapping></mime-mapping> 定义某一个扩展名和某一个MIME Type做对映,包含两个 子元素:
<extension></extension> 扩展名的名称
<mime-type></mime-type> MIME格式

比如:
<mime-mapping>
 <extension>doc</extension>
 <mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>
<mime-mapping>
 <extension>xls</extension>
 <mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>


<welcome-file-list></welcom-file-list> 用来定义首页的列单,包含一个子元素:
<welcome-file></welcome-file> 指定首页的文件名称

比如:
<welcome-file-list>
 <welcome-file>index.jsp</welcome-file>
 <welcome-file>index.html</welcome-file>
</welcom-file-list>


<error-page></error-page> 用来处理错误代码或异常的页面,有三个子元素:
<error-code></error-code> 指定错误代码
<exception-type></exception-type> 指定一个JAVA异常类型
<location></location> 指定在web站台内的相关资源路径

比如:
<error-page>
 <error-code>404</error-code>
 <location>/error404.jsp</location>
</error-page>
<error-page>
 <exception-type>java.lang.Exception</exception-type>
 <location>/exception.jsp</location>
</error-page>

<jsp-config>
<taglib></taglib>
</jsp-config> 用来设定JSP网页所用到的Tag Library路径,有两个子元素:
<taglib-uri></taglib-uri> 定义TLD文件的URI,在JSP网页中用taglib指令便可取得该URI的 TLD文件
<taglib-location></taglib-location> 指定TLD文件相对于web站台的存放位置

比如:
<jsp-config>
 <taglib>
  <taglib-uri>myTaglib</taglib-uri>
  <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
 </taglib>
</jsp-config>

<resource-ref></resource-ref> 定义利用JNDI取得站台可利用的资源,有五个子元素:
<description></description> 资源说明
<rec-ref-name></rec-ref-name> 资源名称
<res-type></res-type> 资源种类
<res-auth></res-auth> 资源经由Application或Container来许可
<res-sharing-scope></res-sharing-scope> 资源是否可以共享,有Shareable和Unshareable两个 值,默认为Shareable

比如,配置数据库连接池就可在此配置:
<resource-ref>
 <description>JNDI JDBC DataSource of shop</description>
 <res-ref-name>jdbc/sample_db</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
</resource-ref>


通过此次整理,对web.xml中元素有了更清楚的了解,以后继续努力... 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值