web.xml文件元素详解

web.xml的配置通常需要定义这些:

Web容器的名称和说明

环境参数初始化

Servlet的名称和映射

Session的设定

Tag library的映射

错误处理

 

 

一:desplay

对web应用的描述。

Example:

<description>note</description>

 

二:desplay-name

web应用的名称。

Example:

<display-name>icon Example</display-name>

 

三:icon

icon元素用来指定GIF格式或JPEG格式的图标文件名。

icon可选子元素:

small-icon元素指向小图标的路径,大小为16x16像素。

large-icon元素指向大图标的路径,大小为32x32像素。

Example:

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

 

四:distributable

用于分布式处理。

 

五:context-param

用来设置web容器的环境参数,参数名在整个web应用中必须是唯一的。

context-param子元素:

param-name和param-value

Example:

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

此处设置的参数,

在Jsp中可以使用${initParam.param_name}来获得,

在Servlet中可以通过String param_name = getServletContext().getInitParamter("param_name");来获得。

 

六:filter

web容器中的过滤器,在请求和响应被servlet处理之前或之后,可以对其操作。

filter元素必须出现在filter-mapping元素之前。

filter子元素:

filter-name用来定义过滤器的名称,该名称在整个应用中必须唯一。

filter-class用来指定过滤器类名。

Example:

<filter>

<filter-name>setCharacterEncoding</filter-name>

<filter-class>coreservlet.javaworld.CH11.SetCharacterEncodingFilter</filter-class>

<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>

</filter>

 

七:filter-mapping

一旦声明了filter,就必须利用filter-mapping来映射servlet或jsp。过滤时是按照filter-mapping元素出现的顺序执行的。

filter-mapping子元素:

filter-name对应过滤器的名称。

servlet-name指定servlet名称

url-pattern指定jsp的url

Example:

<filter-mapping>

<filter-name>shiroFilter</filter-name>

<servlet-name>shiroServlet</servlet-name>

</filter-mapping>

 

<filter-mapping>

<filter-name>shiroFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

 

八:listener

应用事件监听器程序是建立或修改servlet环境或会话对象时通知的类。

listener必须位于所有servlet元素之前。

Example:

<listener>

<listen-class>package.ListenerClass</listen-class>

</listener>

public class ListenerClass implements ServletContextListener {

public void contextInitialized(ServletContextEvent event) {    
        System.out.println("Context created on " +  new Date() + ".");    
    }    
   
    public void contextDestroyed(ServletContextEvent event) {    
        System.out.println("Context destroyed on " +   new Date() + ".");    
    }    

}

Web应用的Servlet-Context建立(如装载Web应用)或消除(如服务器关闭)时,它就在标准输出上显示一条消息。

 

九:servlet

servlet元素用来给servlet或jsp给出名称和定制url,使用servlet-mapping来映射。

servlet子元素:

servlet-name用来定义servlet的名称,该名称在整个应用中必须唯一。

servlet-class用来指定类名。

Example:

<servlet>

<servlet-name>Test</servlet-name>

<servlet-class>package.TestServlet</servlet-class>

</servlet>

 

十:servlet-mapping

映射servlet

Example:

<servlet-mapping>

<servlet-name>Test</servlet-name>

<url-pattern>/UrlTest</url-pattern>

<servlet-mapping>

 

十一:session-config

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

可利用HttpSession的setMaxInactiveInterval方法直接设置个别会话对象的超时值。如果不采用这种方法,则缺省的超时值由具体的服务器决定,

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

Example:

<session-config>   

<session-timeout>180</session-timeout>   

</session-config>  

 

十二:mima-mapping

关联文件与MIME类型,服务器一般都具有一种让Web站点管理员将文件扩展名与媒体相关联的方法。例如,将会自动给予名为mom.jpg的文件一个image/jpeg的MIME 类型。但是,假如你的Web应用具有几个不寻常的文件,你希望保证它们在发送到客户机时分配为某种MIME类型。mime-mapping元素(具有 extension和mime-type子元素)可提供这种保证。

Example:

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

服务器将application/x-fubar的MIME类型分配给所有以.foo结尾的文件。

<mime-mapping>   
<extension>ps</extension>   
<mime-type>application/postscript</mime-type>   
</mime-mapping> 

服务器在发送到客户机时指定.ps文件作为纯文本(text/plain)而不是作为PostScript(application/postscript)。

 

十三:welcome-file-list

当用户在浏览器中输入的URL不包含某个servlet名或jap页面时,welcome-file-list元素可以指定默认文件。

假如用户提供了一个像http: //host/webAppPrefix/directoryName/ 这样的包含一个目录名但没有包含文件名的URL,welcome-file-list元素将可以指定

用户接下来的去向。

Example:

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

如果一个URL给出一个目录名但未给出文件名,服务器应该首先试用index.jsp,然后再试用index.html。

 

十四:error-page

返回特定错误代码或异常时,指定错误页面。

Example:

<error-page>
<error-code>404</error-code>
<location>/resources/404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/resources/500.jsp</location>
</error-page>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值