⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webxml.txt

📁 用java编写的程序
💻 TXT
📖 第 1 页 / 共 5 页
字号:
<small-icon>/images/small-book.gif</small-icon>
<large-icon>/images/tome.jpg</large-icon>
</icon>
l display-name
display-name元素提供GUI工具可能会用来标记此Web应用的一个名称。下面是个例子。
<display-name>Rare Books</display-name>
l description
description元素提供解释性文本,如下所示:
<description>
This Web application represents the store developed for
rare-books.com, an online bookstore specializing in rare
and limited-edition books.
</description>

12 关联文件与MIME类型

服 务器一般都具有一种让Web站点管理员将文件扩展名与媒体相关联的方法。例如,将会自动给予名为mom.jpg的文件一个image/jpeg的MIME 类型。但是,假如你的Web应用具有几个不寻常的文件,你希望保证它们在发送到客户机时分配为某种MIME类型。mime-mapping元素(具有 extension和mime-type子元素)可提供这种保证。例如,下面的代码指示服务器将application/x-fubar的MIME类型分 配给所有以.foo结尾的文件。
<mime-mapping>
<extension>foo</extension>
<mime-type>application/x-fubar</mime-type>
</mime-mapping>
或许,你的Web应用希望重载(override)标准的映射。例如,下面的代码将告诉服务器在发送到客户机时指定.ps文件作为纯文本(text/plain)而不是作为PostScript(application/postscript)。
<mime-mapping>
<extension>ps</extension>
<mime-type>application/postscript</mime-type>
</mime-mapping>


13 定位TLD

JSP taglib元素具有一个必要的uri属性,它给出一个TLD(Tag Library Descriptor)文件相对于Web应用的根的位置。TLD文件的实际名称在发布新的标签库版本时可能会改变,但我们希望避免更改所有现有JSP页 面。此外,可能还希望使用保持taglib元素的简练性的一个简短的uri。这就是部署描述符文件的taglib元素派用场的所在了。Taglib包含两 个子元素:taglib-uri和taglib-location。taglib-uri元素应该与用于JSP taglib元素的uri属性的东西相匹配。Taglib-location元素给出TLD文件的实际位置。例如,假如你将文件chart-tags- 1.3beta.tld放在WebApp/WEB-INF/tlds中。现在,假如web.xml在web-app元素内包含下列内容。
<taglib>
<taglib-uri>/charts.tld</taglib-uri>
<taglib-location>
/WEB-INF/tlds/chart-tags-1.3beta.tld
</taglib-location>
</taglib>
给出这个说明后,JSP页面可通过下面的简化形式使用标签库。
<%@ taglib uri="/charts.tld" prefix="somePrefix" %>

14 指定应用事件监听程序

应用事件监听器程序是建立或修改servlet环境或会话对象时通知的类。它们是servlet规范的版本2.3中的新内容。这里只简单地说明用来向Web应用注册一个监听程序的web.xml的用法。
注册一个监听程序涉及在web.xml的web-app元素内放置一个listener元素。在listener元素内,listener-class元素列出监听程序的完整的限定类名,如下所示:
<listener>
<listener-class>package.ListenerClass</listener-class>
</listener>
虽 然listener元素的结构很简单,但请不要忘记,必须正确地给出web-app元素内的子元素的次序。listener元素位于所有的servlet 元素之前以及所有filter-mapping元素之后。此外,因为应用生存期监听程序是serlvet规范的2.3版本中的新内容,所以必须使用 web.xml DTD的2.3版本,而不是2.2版本。
例如,程序清单5-20给出一个名为ContextReporter的简单的监听程序, 只要Web应用的Servlet-Context建立(如装载Web应用)或消除(如服务器关闭)时,它就在标准输出上显示一条消息。程序清单5-21给 出此监听程序注册所需要的web.xml文件的一部分。

程序清单5-20 ContextReporterjava
package moreservlets;

import javax.servlet.*;
import java.util.*;

/** Simple listener that prints a report on the standard output 
* when the ServletContext is created or destroyed.
* <P>
* Taken from More Servlets and JavaServer Pages
* from Prentice Hall and Sun Microsystems Press,
* http://www.moreservlets.com/.
* ? 2002 Marty Hall; may be freely used or adapted.
*/

public class ContextReporter 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() + ".");
}
}


程序清单5-21 web.xml(声明一个监听程序的摘录)
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<!-- ... -->
<filter-mapping> … </filter-mapping>
<listener>
<listener-class>package.ListenerClass</listener-class>
</listener>
<servlet> ... </servlet>
<!-- ... -->
</web-app>


15 J2EE元素

本节描述用作J2EE环境组成部分的Web应用的web.xml元素。这里将提供一个简明的介绍,详细内容可以参阅http://java.sun.com/j2ee/j2ee-1_3-fr-spec.pdf的Java 2 Plantform Enterprise Edition版本1.3规范的第5章。
l distributable
distributable 元素指出,Web应用是以这样的方式编程的:即,支持集群的服务器可安全地在多个服务器上分布Web应用。例如,一个可分布的应用必须只使用 Serializable对象作为其HttpSession对象的属性,而且必须避免用实例变量(字段)来实现持续性。distributable元素直 接出现在discription元素之后,并且不包含子元素或数据,它只是一个如下的标志。
<distributable />
l resource-env-ref
resource -env-ref元素声明一个与某个资源有关的管理对象。此元素由一个可选的description元素、一个resource-env-ref- name元素(一个相对于java:comp/env环境的JNDI名)以及一个resource-env-type元素(指定资源类型的完全限定的 类),如下所示:
<resource-env-ref>
<resource-env-ref-name>
jms/StockQueue
</resource-env-ref-name>
<resource-env-ref-type>
javax.jms.Queue
</resource-env-ref-type>
</resource-env-ref>
l env-entry
env -entry元素声明Web应用的环境项。它由一个可选的description元素、一个env-entry-name元素(一个相对于java: comp/env环境JNDI名)、一个env-entry-value元素(项值)以及一个env-entry-type元素(java.lang程序 包中一个类型的完全限定类名,java.lang.Boolean、java.lang.String等)组成。下面是一个例子:
<env-entry>
<env-entry-name>minAmout</env-entry-name>
<env-entry-value>100.00</env-entry-value>
<env-entry-type>minAmout</env-entry-type>
</env-entry>
l ejb-ref
ejb -ref元素声明对一个EJB的主目录的应用。它由一个可选的description元素、一个ejb-ref-name元素(相对于java: comp/env的EJB应用)、一个ejb-ref-type元素(bean的类型,Entity或Session)、一个home元素(bean的主 目录接口的完全限定名)、一个remote元素(bean的远程接口的完全限定名)以及一个可选的ejb-link元素(当前bean链接的另一个 bean的名称)组成。
l ejb-local-ref
ejb-local-ref元素声明一个EJB的本地主目录的引用。除了用local-home代替home外,此元素具有与ejb-ref元素相同的属性并以相同的方式使用。

给你的session加个监听器 

有人问我怎么实现在网页里显示在线用户的名称——他已经使用了session,但是无法处理用户离开的情况,然后导致在线用户列表的无限增大。跟他 说了自己在application中进行超时检查,更新application的时候就比较当前所有列表中的session是否超过自己指定的时间间隔。 后来想了想,又给他提了使用给session加监听器的方法。但是提的时候自己也没有做过,所以只是说这种方式很复杂,建议他还是自己进行超时检查。刚才 又看了看资料,发现实际上给session加监听器的方式很简单,不禁觉得自己有点误人子弟了,现在将方法写在这,借以告诫自己以后要严谨。
首先写一个SessionBinder类,它实现了HttpSessionBindingListener接口的valueBound方法和valueUnbound方法,示例代码如下:
public class SessionBinder implements HttpSessionBindingListener {
  public void valueBound(HttpSessionBindingEvent event){
    //you can do anything you want!this method will be called when this binder is bind with any session.
  }

  public void valueUnbound(HttpSessionBindingEvent event) {
    //you can do something while this session is invalidate
  }
}
现在写好了SessionBinder,我们现在选择在一个servlet中向session中加入这个监听器——在jsp中的代码书写与此相同
//省略前面的代码,此操作可能发生在servlet的doGet方法中,也可能是doPost方法中
  HttpSession session = req.getSession(true);//首先获得需要加入监听器的session对象,req是HttpRequest对象
  SessionBinder sb = new SessionBinder();//建立一个监听器对象
  session.putValue("BinderObject",sb);//将监听器加入此session中,从此时开始执行sb的valueBound方法
//省略后面的代码
随后,如果整个session超时或者被用户中止之后,sb的valueUnbound自动执行
呵呵,以上代码以供参考

******************************************************************************************************************
还可以参考:http://java.sun.com/dtd/web-app_2_3.dtd
符号说明:
1. <!ELEMENT 是元素的声明,说明你要定义的是一个元素;
     <!ATTLIST  是属性的声明 
2. #PCDATA包含字符或文本数据
3. "," 子元素使用逗号分隔排序; "|"表示或者; 
<MYFILE (name,address?,email+,phone*)>
4.name 只能使用一次;
5.address? 使用一次或者不使用
6.email+ 使用至少一次或多次
7.phone* 使用一次,多次,或者根本不使用

<!--The web-app element is the root of the deployment descriptor fora web application.--><!ELEMENT web-app (icon?, display-name?, description?, distributable?,context-param*, filter*, filter-mapping*, listener*, servlet*,servlet-mapping*, session-config?, mime-mapping*, welcome-file-list?,error-page*, taglib*, resource-env-ref*, resource-ref*, security-constraint*,login-config?, security-role*, env-entry*, ejb-ref*,  ejb-local-ref*)><!--The auth-constraint element indicates the user roles that shouldbe permitted access to this resource collection. The role-nameused here must either correspond to the role-name of one of thesecurity-role elements defined for this web application, or bethe specially reserved role-name "*" that is a compact syntax forindicating all roles in the web application. If both "*" androlenames appear, the container interprets this as all roles.If no roles are defined, no user is allowed access to the portion ofthe web application described by the containing security-constraint.The container matches role names case sensitively when determiningaccess.Used in: security-constraint--><!ELEMENT auth-constraint (description?, role-name*)><!--The auth-method element is used to configure the authenticationmechanism for the web application. As a prerequisite to gaining access to any web resources which are protected by an authorizationconstraint, a user must have authenticated using the configuredmechanism. Legal values for this element are "BASIC", "DIGEST","FORM", or "CLIENT-CERT".Used in: login-config--><!ELEMENT auth-method (#PCDATA)><!--The context-param element contains the declaration of a webapplication's servlet context initialization parameters.Used in: web-app--><!ELEMENT context-param (param-name, param-value, description?)><!--The description element is used to provide text describing the parentelement.  The description element should include any information thatthe web application war file producer wants to provide to the consumer ofthe web application war file (i.e., to the Deployer). Typically, the toolsused by the web application war file consumer will display the descriptionwhen processing the parent element that contains the description.Used in: auth-constraint, context-param, ejb-local-ref, ejb-ref,env-entry, filter, init-param, resource-env-ref, resource-ref, run-as,security-role, security-role-ref, servlet, user-data-constraint,web-app, web-resource-collection--><!ELEMENT description (#PCDATA)><!--The display-name element contains a short name that is intended to bedisplayed by tools.  The display name need not be unique.Used in: filter, security-constraint, servlet, web-appExample:<display-name>Employee Self Service</display-name>--><!ELEMENT display-name (#PCDATA)><!--The distributable element, by its presence in a web applicationdeployment descriptor, indicates that this web application isprogrammed appropriately to be deployed into a distributed servletcontainerUsed in: web-app--><!ELEMENT distributable EMPTY><!--The ejb-link element is used in the ejb-ref or ejb-local-refelements to specify that an EJB reference is linked to anenterprise bean.The name in the ejb-link element is composed of apath name specifying the ejb-jar containing the referenced enterprisebean with the ejb-name of the target bean appended and separated fromthe path name by "#".  The path name is relative to the war filecontaining the web application that is referencing the enterprise bean.This allows multiple enterprise beans with the same ejb-name to beuniquely identified.Used in: ejb-local-ref, ejb-refExamples:	<ejb-link>EmployeeRecord</ejb-link>	<ejb-link>../products/product.jar#ProductEJB</ejb-link>--><!ELEMENT ejb-link (#PCDATA)><!--The ejb-local-ref element is used for the declaration of a reference toan enterprise bean's local home. The declaration consists of:	- an optional description	- the EJB reference name used in the code of the web application	  that's referencing the enterprise bean	- the expected type of the referenced enterprise bean	- the expected local home and local interfaces of the referenced	  enterprise bean	- optional ejb-link information, used to specify the referenced	  enterprise beanUsed in: web-app--><!ELEMENT ejb-local-ref (description?, ejb-ref-name, ejb-ref-type,		local-home, local, ejb-link?)><!--The ejb-ref element is used for the declaration of a reference toan enterprise bean's home. The declaration consists of:	- an optional description	- the EJB reference name used in the code of	  the web application that's referencing the enterprise bean	- the expected type of the referenced enterprise bean	- the expected home and remote interfaces of the referenced	  enterprise bean	- optional ejb-link information, used to specify the referenced	  enterprise beanUsed in: web-app--><!ELEMENT ejb-ref (description?, ejb-ref-name, ejb-ref-type,		home, remote, ejb-link?)><!--The ejb-ref-name element contains the name of an EJB reference. TheEJB reference is an entry in the web application's environment and isrelative to the java:comp/env context.  The name must be uniquewithin the web application.It is recommended that name is prefixed with "ejb/".Used in: ejb-local-ref, ejb-refExample:<ejb-ref-name>ejb/Payroll</ejb-ref-name>--><!ELEMENT ejb-ref-name (#PCDATA)><!--The ejb-ref-type element contains the expected type of thereferenced enterprise bean.The ejb-ref-type element must be one of the following:	<ejb-ref-type>Entity</ejb-ref-type>	<ejb-ref-type>Session</ejb-ref-type>Used in: ejb-local-ref, ejb-ref--><!ELEMENT ejb-ref-type (#PCDATA)><!--The env-entry element contains the declaration of a web application'senvironment entry. The declaration consists of an optionaldescription, the name of the environment entry, and an optionalvalue.  If a value is not specified, one must be suppliedduring deployment.--><!ELEMENT env-entry (description?, env-entry-name, env-entry-value?,env-entry-type)><!--The env-entry-name element contains the name of a web applications'senvironment entry.  The name is a JNDI name relative to thejava:comp/env context.  The name must be unique within a web application.Example:<env-entry-name>minAmount</env-entry-name>Used in: env-entry--><!ELEMENT env-entry-name (#PCDATA)><!--The env-entry-type element contains the fully-qualified Java type ofthe environment entry value that is expected by the web application'scode.The following are the legal values of env-entry-type:	java.lang.Boolean	java.lang.Byte	java.lang.Character	java.lang.String	java.lang.Short	java.lang.Integer	java.lang.Long	java.lang.Float	java.lang.DoubleUsed in: env-entry--><!ELEMENT env-entry-type (#PCDATA)><!--The env-entry-value element contains the value of a web application'senvironment entry. The value must be a String that is valid for theconstructor of the specified type that takes a single Stringparameter, or for java.lang.Character, a single character.Example:<env-entry-value>100.00</env-entry-value>Used in: env-entry--><!ELEMENT env-entry-value (#PCDATA)><!--The error-code contains an HTTP error code, ex: 404Used in: error-page--><!ELEMENT error-code (#PCDATA)><!--The error-page element contains a mapping between an error codeor exception type to the path of a resource in the web applicationUsed in: web-app--><!ELEMENT error-page ((error-code | exception-type), location)><!--The exception type contains a fully qualified class name of aJava exception type.Used in: error-page--><!ELEMENT exception-type (#PCDATA)><!--The extension element contains a string describing anextension. example: "txt"Used in: mime-mapping--><!ELEMENT extension (#PCDATA)><!--Declares a filter in the web application. The filter is mapped toeither a servlet or a URL pattern in the filter-mapping element, usingthe filter-name value to reference. Filters can access theinitialization parameters declared in the deployment descriptor atruntime via the FilterConfig interface.Used in: web-app--><!ELEMENT filter (icon?, filter-name, display-name?, description?,filter-class, init-param*)><!--The fully qualified classname of the filter.Used in: filter--><!ELEMENT filter-class (#PCDATA)><!--Declaration of the filter mappings in this web application. Thecontainer uses the filter-mapping declarations to decide which filtersto apply to a request, and in what order. The container matches therequest URI to a Servlet in the normal way. To determine which filtersto apply it matches filter-mapping declarations either on servlet-name,or on url-pattern for each filter-mapping element, depending on whichstyle is used. The order in which filters are invoked is the order inwhich filter-mapping declarations that match a request URI for aservlet appear in the list of filter-mapping elements.The filter-namevalue must be the value of the <filter-name> sub-elements of one of the<filter> declarations in the deployment descriptor.Used in: web-app--><!ELEMENT filter-mapping (filter-name, (url-pattern | servlet-name))><!--The logical name of the filter. This name is used to map the filter.Each filter name is unique within the web application.Used in: filter, filter-mapping--><!ELEMENT filter-name (#PCDATA)><!--The form-error-page element defines the location in the web appwhere the error page that is displayed when login is not successfulcan be found. The path begins with a leading / and is interpretedrelative to the root of the WAR.Used in: form-login-config--><!ELEMENT form-error-page (#PCDATA)><!--The form-login-config element specifies the login and error pagesthat should be used in form based login. If form based authenticationis not used, these elements are ignored.Used in: login-config--><!ELEMENT form-login-config (form-login-page, form-error-page)><!--The form-login-page element defines the location in the web appwhere the page that can be used for login can be found. The pathbegins with a leading / and is interpreted relative to the root of the WAR.Used in: form-login-config--><!ELEMENT form-login-page (#PCDATA)><!--The home element contains the fully-qualified name of the enterprisebean's home interface.Used in: ejb-refExample:<home>com.aardvark.payroll.PayrollHome</home>--><!ELEMENT home (#PCDATA)><!--The http-method contains an HTTP method (GET | POST |...).Used in: web-resource-collection--><!ELEMENT http-method (#PCDATA)><!--The icon element contains small-icon and large-icon elements thatspecify the file names for small and a large GIF or JPEG icon imagesused to represent the parent element in a GUI tool.Used in: filter, servlet, web-app--><!ELEMENT icon (small-icon?, large-icon?)><!--The init-param element contains a name/value pair as aninitialization param of the servletUsed in: filter, servlet--><!ELEMENT init-param (param-name, param-value, description?)><!--The jsp-file element contains the full path to a JSP file withinthe web application beginning with a `/'.Used in: servlet--><!ELEMENT jsp-file (#PCDATA)><!--The large-icon element contains the name of a filecontaining a large (32 x 32) icon image. The filename is a relative path within the web application'swar file.The image may be either in the JPEG or GIF format.The icon can be used by tools.Used in: iconExample:<large-icon>employee-service-icon32x32.jpg</large-icon>--><!ELEMENT large-icon (#PCDATA)><!--The listener element indicates the deployment properties for a webapplication listener bean.Used in: web-app--><!ELEMENT listener (listener-class)><!--The listener-class element declares a cla

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -