当前位置: 移动技术网 > IT编程>开发语言>Java > 浅谈JavaWeb中的web.xml配置部署描述符文件

浅谈JavaWeb中的web.xml配置部署描述符文件

2019年07月22日  | 移动技术网IT编程  | 我要评论
1、 定义头和根元素 部署描述符文件就像所有xml文件一样,必须以一个xml头开始。这个头声明可以使用的xml版本并给出文件的字符编码。 docytpe声明必须立即出现

1、 定义头和根元素

部署描述符文件就像所有xml文件一样,必须以一个xml头开始。这个头声明可以使用的xml版本并给出文件的字符编码。

docytpe声明必须立即出现在此头之后。这个声明告诉服务器适用的servlet规范的版本(如2.2或2.3)并指定管理此文件其余部分内容的语法的dtd(document type definition,文档类型定义)。

所有部署描述符文件的顶层(根)元素为web-app。请注意,xml元素不像html,他们是大小写敏感的。因此,web-app和web-app都是不合法的,web-app必须用小写。

2、配置介绍

<discription></discription> 是对站台的描述
<display-name></display-name> 定义站台的名称


distributable><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

1、配置servlet路径

<servlet> 
  <servlet-name>hello1</servlet-name> 
  <servlet-class>china.hubei.helloservelt</servlet-class> 
</servlet> 
<servlet-mapping> 
  <servlet-name>hello1</servlet-name> 
  <url-pattern>/</url-pattern> 
</servlet-mapping> 

2、配置jsp文件路径

<servlet> 
  <servlet-name>hello2</servlet-name> 
  <jsp-file>/pages/student.jsp</jsp-file> 
</servlet> 
<servlet-mapping> 
  <servlet-name>hello2</servlet-name> 
  <url-pattern>/nihao.hello</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></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>


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

比如:

<taglib>
<taglib-uri>mytaglib</taglib-uri>
<taglib-location>/web-inf/tlds/mytaglib.tld</taglib-location>
</taglib>


<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>

<jsp-config> 包括<taglib> 和<jsp-property-group> 两个子元素。

其中<taglib>元素在jsp 1.2时就已经存在;而<jsp-property-group>是jsp 2.0 新增的元素。

<jsp-property-group>元素主要有八个子元素,它们分别为:

1.<description>:设定的说明;

2.<display-name>:设定名称;

3.<url-pattern>:设定值所影响的范围,如:/ch2 或 /*.jsp;

4.<el-ignored>:若为true,表示不支持el 语法;

5.<scripting-invalid>:若为true,表示不支持<% scripting %>语法;

6.<page-encoding>:设定jsp 网页的编码;

7.<include-prelude>:设置jsp 网页的抬头,扩展名为.jspf;

8.<include-coda>:设置jsp 网页的结尾,扩展名为.jspf。

一个简单的<jsp-config>元素完整配置:

<jsp-config>
  <taglib>
    <taglib-uri>taglib</taglib-uri>
    <taglib-location>/web-inf/tlds/mytaglib.tld</taglib-location>
  </taglib>
  <jsp-property-group>
    <description>special property group for jsp configuration jsp example.</description>
    <display-name>jspconfiguration</display-name>
    <url-pattern>/jsp/* </url-pattern>
    <el-ignored>true</el-ignored>
    <page-encoding>gb2312</page-encoding>
    <scripting-invalid>true</scripting-invalid>
    <include-prelude>/include/prelude.jspf</include-prelude>
    <include-coda>/include/coda.jspf</include-coda>
  </jsp-property-group>
</jsp-config> 

以上这篇浅谈javaweb中的web.xml配置部署描述符文件就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网