web.xml 总结

  1. 定义头元素,有序:
    1. xml头声明:xml版本,字符编码
    2. DOCTYPE声明:Servlet规范的版本,DTD
    3. web-app
  2. web-app中元素的顺序以及作用、举例

Web.xml中出现的顺序(全小写)

作用与使用举例

Icon

指定IDEGUI工具表示web应用的一两个图像文件的位置

<icon>

<small-icon></small-icon>

<large-icon></large-icon>

<!--可以用small-icon指定16*16的图gif或JPEG图像,large-icon指定32*32的。 -->

</icon>

Display-name

GUI工具标记这个web应用的一个名称

Description

描述这个应用。

Context-param

应用范围内的初始化参数.(init-param的区别是:这个是整个web应用里可用的。Init-param只是在那个指定的jspservlet中可用。 )

Eg.

<context-param>

<param-name>contextParamName</param-name>

<param-value>contextParamValue</param-value>

</context-param>

Filter

过滤器元素。将一个名字与一个实现了javax.servlet.Filter接口的类相关联。

需要在web.xml中使用2.3或以上版本的DTD

过滤器可截取和修改一个servletjsp和请求或从中发出的响应。在执行servletjsp之前,必须先执行第一个相关的过滤器的doFilter方法。FilterChain对象调用doFilter方法时,执行链中的下一个过滤器至最后一个,然后servletjsp才被执行。过滤器具有对到来的ServletRequest对象的全部访问权。

新建一个类implements Filter:

Eg.

public class MyFilter implements Filter {

public void destroy() {

}

/* (non-Javadoc)

* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)

*/

public void doFilter(ServletRequest request, ServletResponse response,

FilterChain chain) throws IOException, ServletException {

HttpServletRequest req=(HttpServletRequest)request;//可以全权操作这个到来的request

System.out.println("这里可以在执行servlet之前进行操作,而且具有对到来的servletRequest对象的所有访问权。"+req.getRemoteHost());

chain.doFilter(request, response);//执行完了doFilet再去执行相应的原servlet或jsp

}

public void init(FilterConfig arg0) throws ServletException {

}

}

filterweb.xml中的声明与servlet相似:

<filter>

<filter-name>myFilter</filter-name>

<filter-class>guoxp.ajax.servlet.MyFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>myFilter</filter-name>

<url-pattern>/*</url-pattern><!--或是指定某个访问路径-->

</filter-mapping>

但它的filter-mapping有两种方式,上面的是与servlet相似的声明方式,它还可以直接声明它截取的servletjsp的名字(不过这个servletjsp的名字要在这个web.xml中稍后进行声明),即用下面的方式:

<filter-mapping>

<filter-name>myFilter</filter-name>

<servlet-name>AJAXServer</servlet-name>

</filter-mapping>

Filter-mapping

将过滤器的名字与一个或多个servletjsp页面相关联。

例子如上。

Listener

指出监听程序类。事件监听程序在建立、修改和删除会话或servlet环境时得到通知。web应用的Servlet-Context建立或消除时,就会触发该监听器。

<listener>

<listener-class>guoxp.ajax.servlet.MyListener</listener-class>

</listener>

MyListener.java:

public class MyListener implements ServletContextListener {

public void contextDestroyed(ServletContextEvent event) {

System.out.println("destroyed....");

}

public void contextInitialized(ServletContextEvent event) {

System.out.println("created....");

}

}

Servlet

命名一个servletjsp页面

Eg.

<servlet>

<servlet-name>AJAXServer</servlet-name>

<servlet-class>guoxp.ajax.servlet.AJAXServer</servlet-class> <!-- 如果是jsp页面,就使用<jsp-file> 代替<servlet-class>。这样的配置可以使多个url共同用一个jsp处理,或是使得在url中去掉.jsp扩展名-->

<load-on-startup>1</load-on-startup><!--指定装载servlet或是jsp时的顺序,因为有的jspservlet需要init较长的时间,按数字从小到大确定先后顺序。-->

</servlet>

Servlet-mapping

为这个servlet名提供一个缺省的url

Eg.

<servlet-mapping>

<servlet-name>AJAXServer</servlet-name>

<url-pattern>/AJAXServer</url-pattern> <!-- 必须以斜杠‘/’开始,还可以包含通配符,如/*.jsp,则这样的.jsp的访问都会请求到这个AJAXServer-->

<init-param>

<param-name>paramName</param-name>

<param-value>paramValue</param-value><!--可以向servlet 提供初始化参数。

servlet中相应的得到这个参数的方法:

init()方法中,

public void init() throws ServletException {

ServletConfig config=getServletConfig();

String value=config.getInitParameter("paramName");

}

jsp中相应的用jspInit()方法:

<%!private String paramValue;

public void jspInit() {

ServletConfig config = getServletConfig();

paramValue = config.getInitParameter("paramName");

}%>

-->

</init-param>

</servlet-mapping>

Session-config

设定会话的超时时间。或用HttpSessionsetMaxInactiveInterval方法明确设置单个会话对象的超时值。

Eg.

<session-config>

<session-timeout>120</session-timeout>

</session-config>

Mime-mapping

web应用的特殊文件保证 分配特定的mime类型。

Eg.

<mime-mapping>

<extension>txt</extension>

<mime-type>application/txt</mime-type>

</mime-mapping>

附:常见的MIME类型

超文本标记语言文本 .htm,.html text/html

普通文本 .txt text/plain

RTF文本 .rtf application/rtf

GIF图形 .gif image/gif

JPEG图形 .ipeg,.jpg image/jpeg

au声音文件 .au audio/basic

MIDI音乐文件 mid,.midi audio/midi,audio/x-midi

RealAudio音乐文件 .ra, .ram audio/x-pn-realaudio

MPEG文件 .mpg,.mpeg video/mpeg

AVI文件 .avi video/x-msvideo

GZIP文件 .gz application/x-gzip

TAR文件 .tar application/x-tar

Welcome-file-list

访问目录名时的起始文件。

Eg.

<welcome-file-list>

<welcome-file>modules/frame/frame.jsp</welcome-file>

<welcome-file>index.jsp</welcome-file>/* 可以指定多个欢迎页,从上向下试用*/

</welcome-file-list>

Error-page

Error-code或特定异常时,订制显示的页面。注意location必须以斜线/开头,也就是相对于整个应用的。两个方式:error-codeexception-type

<error-page>

<error-code>404</error-code>

<location>/error404.jsp</location><!--关于指定错误处理页面,也可以在jsp中指定

<%@pageerrorPage="error.jsp"%>-->

</error-page>

<error-page>

<exception-type>

javax.servlet.ServletException

</exception-type>

<location>/exception.jsp</location>

</error-page>

里面有三个属性可供处理异常时使用,分别是:status_code、message、与exception_type

指定显示处理异常的页面exception.jsp,isErrorPage="true"指定举例:

<%@ page language="java" pageEncoding="UTF-8"

contentType="text/html; charset=UTF-8" isErrorPage="true"%>

<%--指定了isErrorPage就可以在EL中使用隐式对象${pageContext.exception}

--%>

<html>

<head>

<title>错误与例外处理页面</title>

</head>

<body>

错误码:

<%=request.getAttribute("javax.servlet.error.status_code")%>

<br />

错误信息:

<%=request.getAttribute("javax.servlet.error.message")%>

<br />

异常:

<%=request.getAttribute("javax.servlet.error.exception_type")%>

<br />

</body>

</html>

Jsp-config

<taglib>对标记库文件指定别名.当标记库名改变时可以不改变jsp中的引用名。2.4,<tablib>已经作为<jsp-config>的子元素。

Eg.

<jsp-config>

<taglib>

<taglib-uri>/dhtmlxgrid.tld</taglib-uri>

<taglib-location>/WEB-INF/dhtmlxgrid.tld</taglib-location>

</taglib>

</jsp-config>

Resource-env-ref

声明与资源相关的一个管理对象

Resource-ref

声明资源工厂使用的外部资源

Secuity-constraint

制订应该保护的url

Login-config

指定如何给试图访问受保护页面 用户授权。

Secuity-role

给出安全角色列表

Env-entry

web应用的环境项

Ejb-ref

声明EJB主目录的引用

Ejb-local-ref

声明一个EJB的本地主目录的应用

3. 重新映射/servlet/URL模式

在一个特定的Web应用中禁止以http://host/webAppPrefix/servlet/ 开始的URL的处理非常简单。所需做的事情就是建立一个错误消息servlet,并使用url-pattern元素将所有匹配请求转向该servlet。只要简单地使用:

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

reference: <http://blog.csdn.net/tanghc1983/archive/2007/05/08/1601247.aspx>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值