zoukankan      html  css  js  c++  java
  • Tomcat 5.5.23 文档阅读Tips 7 JNDI Resources

    本文讲述在Tomcat中配置JNDI。JNDI IntialContext其实是J2EE中要求的一个东西,不过Tomcat也实现了。在Tomcat这些app server中配置好资源,在我们的web应用的web.xml中就可以引用这些资源了。 

    1. 在Tomcat中,一个资源可以被配置成server类型的和web应用类型的。所谓server类型的就是在server.xml中用<GlobalNameingResources>定义的资源。在<GlobalNamingResources>中,就存在多个<Resource>的配置,这种资源可以被server中所有的web app所用,web app通过用<ResourceLink>来引用这种资源;在每个web app中,也可以配置只能给自己使用的资源,用<Resource>定义,<Resource>是定义在web app的context中的,在前面已经讲过了,对于一个web app来说,它的context可以定义在两个地方。 

    2. 在Tomcat中,也就是在app server中,我们可以配置如下四种资源: 

    (1) <Environment> - 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 <env-entry> element in the web application deployment descriptor). 
    (2) <Resource> - Configure the name and data type of a resource made available to the application (equivalent to the inclusion of a <resource-ref> element in the web application deployment descriptor). 
    (3) <ResourceLink> - Add a link to a resource defined in the global JNDI context. Use resource links to give a web application access to a resource defined in the<GlobalNamingResources> child element of the <Server> element. 
    (4) <Transaction> - Add a resource factory for instantiating the UserTransaction object instance that is available at java:comp/UserTransaction. 

    第一种就是配置一个key-value类型的资源,所以叫Environment。这种资源在web应用的web.xml中也可以用<env-entry>来定义。下面会讲到在web.xml中的一些东西。第二种就是配置普通资源,一般是一个key对应一个object。比如配置一个JDBC资源,对应的就是一个Connection实例,一个javamail资源,对应的就是一个Session实例等。第三种就是引用一个全局的定义好的资源。第四种不太清楚。 

    Tomcat会根据这些资源的定义,调用对应资源的Factory中的方法(其实JNDI就是一个基于Factory模式的资源管理器样的东西),然后将取道的资源的instance帮定在"java:comp/env" portion上(JNDI有自己的namespace)。 

    OK,对应于在App Server中配置的资源,一个web app在它的web.xml文件中也有一些JNDI的资源配置: 

    (1) <env-entry> - Environment entry, a single-value parameter that can be used to configure how the application will operate. 
    (2) <resource-ref> - 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 5. 
    (3) <resource-env-ref> - 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. 

    第一个<env-entry>和app server中的<Environment>是类似的,也是一个key-value的东西。如果在web.xml中配置了一个和<Environment>中重名的资源,那么,这两者是不相互覆盖的,除非在app server的<Environment>中定义的该资源,设置了attribute override=true。第二个就是本web app对app server中定义的资源的引用了,象我们要使用app server中定义好的jdbc资源,就要写这样的配置;第三个是Servlet 2.4开始的一个东西--资源环境引用,在Servlet 2.4里用来简化设置不需认证信息的资源环境,一般声明与资源相关的被管理对象,如环境参数、resource-ref变量。
    3. OK, 前面我们讲完了基本概念,现在来看看,要使用一个JNDI Resource,需要做几件事情(这里以一个javamail资源举例)。 

    (1) 在Tomcat中定义资源(这个资源是定义在一个web app之内的): 

    CODE: SELECT ALL
    <Context ...>
      ...
      <Resource name="mail/Session" auth="Container"
                type="javax.mail.Session"
                mail.smtp.host="localhost"/>
      ...
    </Context>


    (2) 在web app的web.xml中引用:

    CODE: SELECT ALL
    <resource-ref>
      <description>
        Resource reference to a factory for javax.mail.Session
        instances that may be used for sending electronic mail
        messages, preconfigured to connect to the appropriate
        SMTP server.
      </description>
      <res-ref-name>
        mail/Session
      </res-ref-name>
      <res-type>
        javax.mail.Session
      </res-type>
      <res-auth>
        Container
      </res-auth>
    </resource-ref>


    (3) 书写代码,使用该资源:

    CODE: SELECT ALL
    Context initCtx = new InitialContext();
    Context envCtx = (Context) initCtx.lookup("java:comp/env");
    Session session = (Session) envCtx.lookup("mail/Session");

    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(request.getParameter("from"));
    InternetAddress to[] = new InternetAddress[1];
    to[0] = new InternetAddress(request.getParameter("to"));
    message.setRecipients(Message.RecipientType.TO, to);
    message.setSubject(request.getParameter("subject"));
    message.setContent(request.getParameter("content"), "text/plain");
    Transport.send(message);


    (4) 将javamail的jar包放到Tomcat能找到的地方,这样它才能为我们生成资源。一般我们将这些jar包放到$CATALINA_HOME/common/lib目录下。 

    注:javamail的jar包只需要放到$CATALINA_HOME/common/lib目录下即可,不要再把这些jar包放到我们web app的lib目录下了,否则会出错,因为这是app server管理的资源,不是我们web app管理的资源。 

    4. 本文的最后还写了一个例子,教我们如何书写自己的一个Resource Factory。这样我们就可以自定义一个Resource,然后使用了,有兴趣的话直接看文档吧。 

  • 相关阅读:
    Android studio导入开源项目
    使用Kindeditor上传图片
    IOS实现自动循环滚动广告--ScrollView的优化和封装
    Android开发之Drag&Drop框架实现拖放手势
    IOS中的手势详解
    Android实现图片轮显效果——自定义ViewPager控件
    IOS欢迎界面Launch Screen动态加载广告
    tomcat 启动参数 Xms, Xmx, XX:MaxNewSize, XX:PermSize, -XX:MaxPermSize, Djava.awt.headless
    PHP提升echo, printf, print, file_put_contents等输出方法的效率
    WIN7下强制分第四个主分区的方法
  • 原文地址:https://www.cnblogs.com/super119/p/1933324.html
Copyright © 2011-2022 走看看