登录

  • 登录
  • 忘记密码?点击找回

注册

  • 获取手机验证码 60
  • 注册

找回密码

  • 获取手机验证码60
  • 找回
毕业论文网 > 外文翻译 > 计算机类 > 计算机科学与技术 > 正文

怎么用JNDI资源外文翻译资料

 2022-08-24 11:08  

JNDI Resources How-To

Table of Contents

Introduction

Tomcat provides a JNDI InitialContext implementation instance for each web application running under it, in a manner that is compatible with those provided by a Java Enterprise Edition application server. The Java EE standard provides a standard set of elements in the /WEB-INF/web.xml file to reference/define resources.

See the following Specifications for more information about programming APIs for JNDI, and for the features supported by Java Enterprise Edition (Java EE) servers, which Tomcat emulates for the services that it provides:

web.xml configuration

The following elements may be used in the web application deployment descriptor (/WEB-INF/web.xml) of your web application to define resources:

  • lt;env-entrygt; - Environment entry, a single-value parameter that can be used to configure how the application will operate.
  • lt;resource-refgt; - Resource reference, which is typically to an object factory for resources such as a JDBC DataSource, a JavaMail Session, or custom object factories configured into Tomcat.
  • lt;resource-env-refgt; - Resource environment reference, a new variation of resource-ref added in Servlet 2.4 that is simpler to configure for resources that do not require authentication information.

Providing that Tomcat is able to identify an appropriate resource factory to use to create the resource and that no further configuration information is required, Tomcat will use the information in /WEB-INF/web.xml to create the resource.

Tomcat provides a number of Tomcat specific options for JNDI resources that cannot be specified in web.xml. These include closeMethod that enables faster cleaning-up of JNDI resources when a web application stops and singleton that controls whether or not a new instance of the resource is created for every JNDI lookup. To use these configuration options the resource must be specified in a web applications lt;Contextgt; element or in the lt;GlobalNamingResourcesgt; element of $CATALINA_BASE/conf/server.xml.

context.xml configuration

If Tomcat is unable to identify the appropriate resource factory and/or additional configuration information is required, additional Tomcat specific configuration must be specified before Tomcat can create the resource. Tomcat specific resource configuration is entered in the lt;Contextgt; elements that can be specified in either $CATALINA_BASE/conf/server.xml or, preferably, the per-web-application context XML file (META-INF/context.xml).

Tomcat specific resource configuration is performed using the following elements in the lt;Contextgt; element:

  • lt;Environmentgt; - Configure names and values for scalar environment entries that will be exposed to the web application through the JNDI InitialContext (equivalent to the inclusion of an lt;env-entrygt; element in the web application deployment descriptor).
  • lt;Resourcegt; - Configure the name and data type of a resource made available to the application (equivalent to the inclusion of a lt;resource-refgt; element in the web application deployment descriptor).
  • lt;ResourceLinkgt; - Add a link to a resource defined in the global JNDI context. Use resource links to give a web

    剩余内容已隐藏,支付完成后下载完整资料


    怎么用JNDI资源

    内容列表

      • 介绍
      • web.xml配置
      • context.xml配置
      • 全局配置
      • 使用资源
      • Tomcat标准资源工厂
    1. 通用JavaBean资源
    2. UserDatabase资源
    3. JavaMail会话
    4. JDBC数据资源
      • 添加自定义资源工厂

    介绍

    Tomcat为其下运行的每个Web应用程序提供JNDI InitialContext实现实例,其方式与Java Enterprise Edition应用程序服务器提供的方式兼容。JavaEE标准在文件/WEB-INF/web.xml中提供了一组标准的元素,以引用/定义资源。

    查看以下规范,以获取更多有关JNDI编程接口的信息,以及Tomcat为其提供的服务进行了仿真的Java Enterprise Edition(Java EE)服务器所支持的特性:

      • Java命名和目录接口(包含在JDK1.4及更高版本中)
      • JavaEE平台规范(尤其,查看有关命名的第5章)

    web.xml配置

    你的Web应用程序的Web应用程序部署描述符(/WEB-INF/web.xml)中可以使用以下元素来定义资源:

    • lt;env-entrygt; - 环境条目,一个单值参数,可用于配置应用程序的运行方式。
    • lt;resource-refgt; - 资源引用,通常是对象工厂的资源,例如JDBC DataSource,JavaMail Session或配置到Tomcat中的自定义对象工厂。
    • lt;resource-env-refgt; - 资源环境引用,这是Servlet2.4中新增的resource-ref的变体,它对于不需要身份验证信息的资源更易于配置。

    如果Tomcat能够标识用于创建资源的适当资源工厂,并且不需要其他配置信息,则Tomcat将使用/WEB-INF/web.xml中的信息来创建资源。

    Tomcat为无法在web.xml中指定的JNDI资源提供了许多Tomcat特定的选项。这些包括closeMethod在内的方法在Web应用程序停止时可以更快地清理JNDI资源,而且singleton控制是否为每个JNDI查找都创建资源的新实例。要使用这些配置选项,必须在Web应用程序的lt;Contextgt;元素或者$CATALINA_BASE/conf/server.xml中的lt;GlobalNamingResourcesgt;元素中指定相关资源。

    context.xml配置

    如果Tomcat无法识别适当的资源工厂和/或需要额外的配置信息,则在Tomcat可以创建资源之前,必须指定额外的Tomcat特定的配置。

    Tomcat特定的资源配置要输入到lt;Contextgt;元素中,该元素可以在$CATALINA_BASE/conf/server.xml中指定,或者最好是在每个web应用程序的上下文XML文件(META-INF/context.xml)中指定。

    Tomcat特定的资源配置是使用lt;Contextgt;元素中的以下几个元素来执行的:

    • lt;Environmentgt;-标量环境条目相关的配置名称和值将会通过JNDI InitialContext向web应用程序公开.(相当于在Web应用程序部署描述符中包含一个lt;env-entrygt;元素)。
    • lt;Resourcegt;-配置可用于应用程序的资源的名称和数据类型(相当于在Web应用程序部署描述符中包含一个lt;resource-refgt;元素)。
    • lt;ResourceLinkgt;-将链接添加到在全局JNDI上下文中定义的资源。使用资源链接使Web应用程序可以访问在lt;Servergt;元素的lt;GlobalNamingResourcesgt;子元素中定义的资源。
    • lt;Transactiongt;-添加一个资源工厂,用于实例化UserTransaction对象实例,该实例可从java:comp/UserTransaction获得。

    任何数量的这些元素都可以嵌套在一个lt;Contextgt;元素内,并且仅与该特定的Web应用程序相关联。

    如果已在lt;Contextgt;元素中定义了资源,则无需在中定义该资源/WEB-INF/web.xml。但是,建议保留/WEB-INF/web.xml中的条目以记录Web应用程序的资源需求。

    如果为Web应用程序部署描述符(/WEB-INF/web.xml)中包含的lt;env-entrygt;元素和作为Web应用程序中lt;Contextgt;元素的一部分的lt;Environmentgt;元素中定义了相同的资源名称,则只有在相应的lt;Environmentgt;元素允许的情况下,在部署描述符中的值才会优先被使用(通过将override属性设置为“true”)。

    全局配置

    Tomcat为整个服务器维护一个单独的全局资源命名空间。这些是在$CATALINA_BASE/conf/server.xml中的lt;GlobalNamingResourcesgt;元素中配置的。你可以使用lt;ResourceLinkgt;将这些资源公开给Web应用程序,以将其包含在每个Web应用程序上下文中。

    如果使用lt;ResourceLinkgt;定义了资源,则无需在/WEB-INF/web.xml中定义该资源。但是,建议保留/WEB-INF/web.xml中的该条目以记录Web应用程序的资源需求。

    使用资源

    将InitialContext配置为最初部署的Web应用程序,并可供Web应用程序组件使用(用于只读访问)。所有已配置的条目和资源都放置在JNDI名称空间的java:comp/env部分中,因此一种对资源的典型访问(在这种情况下,是对JDBC DataSource访问)将如下所示:

    // Obtain our environment naming context

    Context initCtx = new InitialContext();

    Context envCtx = (Context) initCtx.lookup('java:comp/env');

    // Look up our data source

    DataSource ds = (DataSource)

    envCtx.lookup('jdbc/EmployeeDB');

    // Allocate and use a connection from the pool

    Connection conn = ds.getConnection();

    ... use this connection to access the database ...

    conn.close();

    Tomcat标准资源工厂

    Tomcat包括一系列标准资源工厂,这些工厂可以为你的Web应用程序提供服务,但是在不修改Web应用程序或部署描述符的情况下(通过lt;Contextgt;元素)为你提供了配置灵活性。以下每个小节详细介绍了标准资源工厂的配置和使用。

    有关如何在Tomcat中创建,安装,配置和使用你自己的自定义资源工厂类的信息,查看添加自定义资源工厂。

    注意–在标准资源工厂中,只有“JDBC数据源”和“用户事务”工厂被强制在其他平台上可用,然后只有在平台实现Java Enterprise Edition(JavaEE)规范时才需要它们。所有其他标准资源工厂,以及你自己编写的自定义资源工厂,都是特定于Tomcat的,不能假定在其他容器上可用。

    通用JavaBean资源

    0.简介

    此资源工厂可用于创建符合标准JavaBeans命名约定的任何Java类的对象(即,它具有零参数构造函数,并具有符合setFoo()命名模式的属性设置器。

    如果工厂的Singleton属性被设置为false,在每次为这个条目的lookup()被调用时,资源工厂只会创建一个适当的bean类新实例。

    下面介绍了使用此工具所需的步骤。

    1.创建你的JavaBean类

    创建JavaBean类,该类将在每次查找资源工厂时实例化。对于此示例,假设你创建一个com.mycompany.MyBean类,它看起来像这样:

    package com.mycompany;

    public class MyBean {

    private String foo = 'Default Foo';

    public String getFoo() {

    return (this.foo);

    }

    public void setFoo(String foo) {

    this.foo = foo;

    }

    private int bar = 0;

    public int getBar() {

    return (this.bar);

    }

    public void setBar(int bar) {

    this.bar = bar;

    }

    }

    2.声明你的资源需求

    接下来,修改你的Web应用程序部署描述符(/WEB-INF/web.xml),以声明JNDI名称,在该名称下你将请求该bean的新实例。最简单的方法是使用lt;resource-env-refgt;元素,如下所示:

    lt;resource-env-refgt;

    lt;descriptiongt;

    Object factory for MyBean instances.

    lt;/descriptiongt;

    lt;resource-env-ref-namegt;

    bean/MyBeanFactory

    lt;/resource-env-ref-namegt;

    lt;resource-env-ref-typegt;

    com.mycompany.MyBean

    lt;/resource-env-ref-typegt;

    lt;/resource-env-refgt;

    警告-确保你遵守DTD对于Web应用程序部署描述符所要求的元素排序!有关详细信息,查看Servlet规范。

    3.编写应用程序对此资源的使用代码

    此资源环境参考的典型用法如下所示:

    Context initCtx = new InitialContext();

    Context envCtx = (Context) initCtx.lookup('java:comp/env');

    MyBean bean = (MyBean) envCtx.lookup('bean/MyBeanFactory');

    writer.println('foo = ' bean.getFoo() ', bar = '

    bean.getBar());

    4.配置Tomcat的资源工厂

    要配置Tomcat的资源工厂,在此Web应用程序的lt;Contextgt;元素中添加这样的元素。

    lt;Context ...gt;

    ...

    lt;Resource name='bean/MyBeanFactory' auth='Container'

    type='com.mycompany.MyBean'

    factory='org.apache.naming.factory.BeanFactory'

    bar='23'/gt;

    ...

    lt;/Contextgt;

    注意,资源名称(此处是bean/MyBeanFactory)必须与Web应用程序部署描述符中指定的值匹配。我们也在初始化bar属性的值,这将导致setBar(23)在返回新bean之前调用。因为我们未初始化foo属性(尽管我们可以这样做),bean将包含其构造函数设置的任何默认值。

    一些bean具有无法自动从字符串值转换的类型的属性。使用Tomcat BeanFactory设置此类属性将失败,并抛出NamingException。如果这些bean提供了从字符串值设置属性的方法,则可以将Tomcat BeanFactory配置为使用这些方法。使用forceString属性完成配置。

    假设我们的bean看起来像这样:

    package com.mycompany;

    import java.net

    剩余内容已隐藏,支付完成后下载完整资料


    资料编号:[235618],资料为PDF文档或Word文档,PDF文档可免费转换为Word

您需要先支付 30元 才能查看全部内容!立即支付

企业微信

Copyright © 2010-2022 毕业论文网 站点地图