javaweb⼯程l配置详解
在⼯程中,l⽤来初始化⼯程配置信息,⽐如说welcome页⾯,filter,listener,servlet,servlet-mapping,启动加载级别等等。每⼀个xml⽂件都有定义他书写规范的schema⽂件,l所对应的xml Schema⽂件中定义了多少种标签元素,l中就可以出现它所定义的标签元素,也就具备哪些特定的功能。l的模式⽂件是由Sun 公司定义的,每个l⽂件的根元素为<web-app>中,必须标明这个l使⽤的是哪个模式⽂件。
[html]
1. <?xml version="1.0" encoding="UTF-8"?>
2. <web-app version="2.5"
3. xmlns="java.sun/xml/ns/javaee"
4. xmlns:xsi="/2001/XMLSchema-instance"
5. xsi:schemaLocation="java.sun/xml/ns/javaee
6. java.sun/xml/ns/javaee/web-app_2_5.xsd">
7. </web-app>
下⾯就来介绍⼀下l中常⽤的标签及其功能
1.<description>,<display-name>,<icon>
<description>项⽬描述</discription>  对项⽬做出描述.
<display-name>项⽬名称</display-name> 定义项⽬的名称.
<icon> icon元素包含small-icon和large-icon两个⼦元素.⽤来指定web站台中⼩图标和⼤图标的路径.
<small-icon>/路径/smallicon.gif</small-icon>small-icon元素应指向web站台中某个⼩图标的路径,⼤⼩为16 X 16 pixel,但是图象⽂件必须为GIF或JPEG格式,扩展名必须为:.gif或 .jpg.
<large-icon>/路径/largeicon-jpg</large-icon> large-icon元素应指向web站台中某个⼤图表路径,⼤⼩为32 X 32 pixel,但是图象⽂件必须为GIF或JPEG的格式,扩展名必须为; gif 或jpg.
例如:
[html]
1. <display-name>Develop Example</display-name>
2. <description>JSP 2.0 Tech Book's Examples</description>
3.  <icon>
4.    <small-icon>/images/small.gif</small-icon>
5.    <large-icon>/images/large.gir</large-icon>
6.  </icon>
2.<context-param>
<context-param>元素含有⼀对参数名和参数值,⽤作应⽤的servlet上下⽂初始化参数。参数名在整个Web应⽤中必须是惟⼀的。
context-param 元素⽤来设定web应⽤的环境参数(context),它包含两个⼦元素: param-name和param-value.
<param-name>参数名称</param-name> 设定Context名称
<param-value>值</param-value> 设定Context名称的值 </context-param>
例如:
[html]
1. <context-param>
2.    <param-name>param_name</param-name>
3.    <param-value>param_value</param-value>
4. </context-param>
此所设定的参数,在JSP⽹页中可以使⽤下列⽅法来取得:${initParam.param_name}
若在Servlet可以使⽤下列⽅法来获得:
String param_name=getServletContext().getInitParamter("param_name");
3.<filter>
filter元素⽤于指定Web容器中的过滤器。
在请求和响应对象被servlet处理之前或之后,可以使⽤过滤器对这两个对象进⾏操作。
利⽤下⼀节介绍的filter-mapping元素,过滤器被映射到⼀个servlet或⼀个URL模式。
这个过滤器的filter元素和filter-mapping 元素必须具有相同的名称。
filter元素⽤来声明filter的相关设定.filter元素除了下⾯介绍的的⼦元素之外,还包括之前介绍过的<icon>,<display-name>,<description>,<init-param>,其⽤途⼀样.
下⾯介绍filter-name,filter-class和init-param元素
init-param元素与context-param 元素具有相同的元素描述符。
filter-name元素⽤来定义过滤器的名称,该名称在整个应⽤中都必须是惟⼀的。
filter-class元素指定过滤器类的完全限定的名称。
<filter-name>Filter的名称</filter-name> 定义Filter的名称.
<filter-class>Filter的类名称</filter-class> 定义Filter的类名称.例如:com.foo.hello
例如:
[html]
1. <filter>
2.  <filter-name>setCharacterEncoding</filter-name>
3.  <filter-class>coreservlet.javaworld.CH11.SetCharacterEncodingFilter</filter-class>
4.  <init-param>
5.      <param-name>encoding</param-name>
6.      <param-value>GB2312</param-value>
7.  </init-param>
8. </filter>
4.<filter-mapping>
filter-mapping元素⽤来声明Web应⽤中的过滤器映射。过滤器可被映射到⼀个servlet或⼀个URL模式。将过滤器映射到⼀个 servlet中会造成过滤器作⽤于servlet上。将过滤器映射到⼀个URL模式中则可以将过滤器应⽤于任何资源,只要该资源的URL与URL模式匹配。过滤是按照部署描述符的filter-mapping元素出现的顺序执⾏的。
filter-mapping 元素的两个主要⼦元素filter-name和url-pattern.⽤来定义Filter所对应的URL。还有servlet-name和dispatcher⼦元素,不是很常⽤。
<filter-name>Filter的名称</filter-name>  定义Filter的名称.
<url-pattern>URL</url-pattern> Filter所对应的RUL.例如:<url-pattern>/Filter/Hello</url-pattern>
<servlet-name>Servlet的名称<servlet-name> 定义servlet的名称.
<dispatcher>REQUEST|INCLUDE|FORWARD|ERROR</disaptcher> 设定Filter对应的请求⽅式,有
RQUEST,INCLUDE,FORWAR,ERROR四种,默认为REQUEST.
例如:
[html]
1. <filter-mapping>
2.    <filter-name>GZIPEncoding</filter-name>
3.    <url-pattern>/*</url-pattern>
4. </filter-mapping>
完整的filter配置例⼦如下:
[html]
1. <filter>
2.    <filter-name>struts2</filter-name>
3.    <filter-class>org.apache.filter.StrutsPrepareAndExecuteFilter</filter-class>
4. </filter>
5. <filter-mapping>
6.    <filter-name>struts2</filter-name>
7.    <url-pattern>/*</url-pattern>
8. </filter-mapping>
5.<servlet>
在l中完成的⼀个最常见的任务是对servlet或JSP页⾯给出名称和定制的URL。⽤servlet元素分配名称,使⽤servlet-mapping元素将定制的URL与刚分配的名称相关联。
例如:
[html]
1. <servlet>
2.  <servlet-name>Test</servlet-name>
3.  <servlet-class&servlets.TestServlet</servlet-class>
4. </servlet>
这表⽰位于WEB-INF/classes/com/moreservlets/TestServlet的servlet已经得到了注册名Test。
6.<servlet-mapping>
servlet-mapping元素包含两个⼦元素servlet-name和url-pattern.⽤来定义servlet所对应URL.
<servlet-name>Servlet的名称</servlet-name> 定义Servlet的名称.
<url-pattern>Servlet URL</url-pattern> 定义Servlet所对应的RUL.例如:<url-pattern>/Servlet/Hello</url-pattern>
例如:
[html]
1. <servlet-mapping>
2.    <servlet-name>LoginChecker</servlet-name>
3.    <url-pattern>/LoginChecker</url-pattern>
4. </servlet-mapping>
完整的servlet配置如下所⽰:
[html]
1. <servlet>
2.  <servlet-name>ServletName</servlet-name>
3.  <servlet-class&Servlet</servlet-class>  <!--Servlet的类-->
4.  <init-param>                                    <!--初始化⼀个变量,可看成全局变量,可省略-->
5.    <param-name>参数名称</param-name>              <!--变量名称-->
6.    <param-value>参数值</param-value>              <!--变量值-->
7.  </init-param>
8. </servlet>
9. <servlet-mapping>
10.  <servlet-name>ServletName</servlet-name>
11.  <url-pattern>/aaa/xxx</url-pattern>                  <!--映射的url路径 -->
12. </servlet-mapping>
在地址栏中输⼊localhost:8080/web-App/aaa/xxx就可以访问了。
7.<listener>
listener元素⽤来注册⼀个类,可以在Web应⽤中包含该类。使⽤listener元素,可以收到事件什么时候发⽣以及⽤什么作为响应的通知。
listener元素⽤来定义Listener接⼝,它的主要⼦元素为<listener-class>
<listen-class>Listener的类名称</listener-class> 定义Listener的类名称.例如: com.foo.hello
例如:
[html]
1. <listener>
2.    <listener-class><span >com.foo.hello</span></listener-
class>
3. </listener>
8.<session-cofing>
session-config包含⼀个⼦元素session-timeout.定义web应⽤中的session参数.
<session-timeout>分钟</session-timeout> 定义这个web应⽤所有session的有效期限.单位为分钟.
例如:
[html]
1. <session-config>
2.    <session-timeout>20</session-timeout>
3. </session-config>
9.<mime-mapping>
mime-mapping包含两个⼦元素extension和mime-type.定义某⼀个扩展名和某⼀MIME Type做对映.
<extension>扩展名名称</extension> 扩展名称
<mime-type>MIME格式</mime-type> MIME格式.
例如:
[html]
1. <mime-mapping>
2.    <extension>doc</extension>
3.    <mime-type>application/vnd.ms-word</mime-type>
4. </mime-mapping>
5. <mime-mapping>
6.    <extension>xls</extension>
7.    <mime-type>application/vnd.ms-excel</mime-type>
8. </mime-mapping>
9. <mime-mapping>
10.    <extension>ppt</extesnion>
11.    <mime-type>application/vnd.ms-powerpoint</mime-type>
12. </mime-mapping>
10.<welcome-file-list>
welcome-file-list包含⼀个⼦元素welcome-file.⽤来定义⾸页列单.
<welcome-file>⽤来指定⾸页⽂件名称</welcome-flie>
welcome-file⽤来指定⾸页⽂件名称.我们可以⽤<welcome-file>指定⼏个⾸页,⽽服务器会依照设定的顺序来⾸页.
例如:
[html]
1. <welcome-file-list>
2.    <welcome-file>index.jsp</welcome-file>
3.    <welcome-file>index.htm</welcome-file>
4. </welcome-file-list>
11.<error-page>
error-page元素包含三个⼦元素error-code,exception-type和location.
将错误代码(Error Code)或异常(Exception)的种类对应到web应⽤资源路径.
<error-code>错误代码</error-code> HTTP Error code,例如: 404
<exception-type>Exception</exception-type> ⼀个完整名称的Java异常类型
<location>/路径</location>  在web应⽤内的相关资源路径
例如:
[html]
1. <error-page>
2.    <error-code>404</error-code>
3.    <location>/error40
4.jsp</location>
param name4. </error-page>
5. <error-page>
6.    <exception-type>java.lang.Exception</exception-type>
7.    <location>/except.jsp</location>
8. </error-page>
12.<jsp-config>
jsp-config元素主要⽤来设定JSP的相关配置,<jsp:config>包括<taglib>和<jsp-property-group>两个⼦元素.其中<taglib>元素在JSP 1.2时就已经存在了;⽽<jsp-property-group>是JSP 2.0新增的元素.
<taglib>
taglib元素包含两个⼦元素taglib-uri和taglib-location.⽤来设定JSP⽹页⽤到的Tag Library路径.
<taglib-uri>URI</taglib-uri> taglib-uri定义TLD⽂件的URI,JSP⽹页的taglib指令可以经由这个URI存取到TLD⽂件.
<taglib-location>/WEB-INF/lib/xxx.tld</taglib-laction> TLD⽂件对应Web应⽤的存放位置.
<jsp-property-group>
jsp-property-group元素包含8个元素,分别为:
<description>Description</descrition>    此设定的说明
<display-name>Name</display-name>        此设定的名称
<url-pattern>URL</url-pattern>          设定值所影响的范围,如:/CH2 或者/*.jsp
<el-ignored>true|false</el-ignored>      若为true,表⽰不⽀持EL语法.
<scripting-invalid>true|false</scripting-invalid>    若为true表⽰不⽀持<%scription%>语法.
<page-encoding>encoding</page-encoding>  设定JSP⽹页的编码
<include-prelude>.jspf</include-prelude> 设置JSP⽹页的抬头,扩展名为.jspf
<include-coda>.jspf</include-coda>      设置JSP⽹页的结尾,扩展名为.jspf
例如:
[html]
1. <jsp-config>
2.    <taglib>
3.        <taglib-uri>Taglib</taglib-uri>
4.        <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
5.    </taglib>
6.    <jsp-property-group>
7.        <description>
8.          Special property group for JSP Configuration JSP example.
9.        </description>
10.        <display-name>JSPConfiguration</display-name>
11.        <uri-pattern>/*</uri-pattern>
12.        <el-ignored>true</el-ignored>
13.        <page-encoding>GB2312</page-encoding>
14.        <scripting-inivalid>true</scripting-inivalid>
15.    ............
16.    </jsp-property-group>
17. </jsp-config>
12.<resource-ref>
resource-ref元素包括五个⼦元素description,res-ref-name,res-type,res-auth,res-sharing-scope.利⽤JNDI取得应⽤可利⽤资源.
<description>说明</description> 资源说明
<rec-ref-name>资源名称</rec-ref-name> 资源名称
<res-type>资源种类</res-type> 资源种类
<res-auth>Application|Container</res-auth> 资源由Application或Container来许可
<res-sharing-scope>Shareable|Unshareable</res-sharing-scope> 资源是否可以共享.默认值为 Shareable
例如:
[html]
1. <resource-ref>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。