使用JNDI在Tomcat中共享servlet会话对象和数据 [英] Using JNDI to share servlet session objects and data in Tomcat

查看:177
本文介绍了使用JNDI在Tomcat中共享servlet会话对象和数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几周里,我一直在寻找在两个上下文/战争文件之间共享对象的解决方案。有很多种方法可以做,其中一种是JNDI。

Over the last few weeks i have been looking at solutions to share an object between two contexts/war files. There are a number of ways this can be done and one of them is JNDI.

我对Tomcat中使用的JNDI不太熟悉,所以想澄清一些问题:

I am not very familiar with JNDI used in Tomcat so would like a few questions clarified:

基本上我是有一个对象的实例,它将为多个上下文/应用程序提供以下服务

Basically i have an instance of an object that would provide the following services to more than one context/application


  • 检查用户是否已登录

  • 检查用户会话是否有效

  • 登录用户 - 包括登录登录详细信息

  • 登出用户 - 删除会话

  • Check that the user is logged on
  • Check that the user's session is valid
  • Logon user - Includes logging the logon details
  • Logout user - Remove session

每个应用程序在处理任何请求之前都会调用此对象来验证用户。我不明白的是,如果对象存储在JNDI上,该对象将如何工作。我已经看到了一些如何在Tomcat中使用JNDI的示例,但99%的示例显示了如何配置JDBC数据源。

Each application will call this object to validate the user before it processes any requests. What i dont understand is how the object would work if it is stored on JNDI. I have seen a few examples of how JNDI is used in Tomcat but 99% of the examples show how to configure a JDBC datasource.


  • 如何在JNDI中初始化对象。例如,Tomcat文档中的以下配置显示了JDBC的配置


<Resource name="jdbc/db1"
   auth="Container"
   type="oracle.jdbc.pool.OracleDataSource"
   driverClassName="oracle.jdbc.driver.OracleDriver"
   factory="oracle.jdbc.pool.OracleDataSourceFactory"
   url="jdbc:oracle:thin:@oracle.microdeveloper.com:1521:db1"
   user="scott"
   password="tiger"
   maxActive="20"
   maxIdle="10"
   maxWait="-1">


在我的情况下,我将如何做类似的事情,最重要的是,如何在将对象放入JNDI树之前初始化该对象。一旦它在JNDI树上,它是如何更新的?

How would i do a similar thing in my case and most importantly, how would i initialise the object before it is put on the JNDI tree. Once it is on the JNDI tree, how is it updated?

我想我想要的是在Tomcat中使用JNDI而不是数据库的简单示例连接,但用于服务提供者类型对象。

I guess what i am looking for is a simple example of the use of JNDI in Tomcat but not for database connections but for service provider type objects.

我一直在阅读本教程 http://docs.oracle.com/javase/tutorial/jndi/ops/bind.html 但它更侧重于LDAP命名目录并没有真正帮助。

I have been reading this tutorial http://docs.oracle.com/javase/tutorial/jndi/ops/bind.html but it is focused more on LDAP naming directories which doesnt really help.

好的,我确实在上面列出的教程中找到了一个例子来说明如何绑定一个对象是JNDI树。

Ok i did find an example in the tutorial i listed above that shows how to "bind" an object the the JNDI tree.

class SerObj {
    public static void main(String[] args) {

    // Set up environment for creating initial context
        Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, 
        "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Create object to be bound
        Button b = new Button("Push me");

        // Perform bind
        ctx.bind("cn=Button", b);

        // Check that it is bound
        Button b2 = (Button)ctx.lookup("cn=Button");
        System.out.println(b2);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Operation failed: " + e);
    }
    }
}




  • 如何修改它以使用Tomcat环境而不是如上所示的LDAP环境?

  • 绑定是否存储对象的副本或对象的引用?我不确定的是,例如,如果3个webapps访问该对象,它们是否都将访问该对象的同一个实例或不同的实例?我希望只有一个我放在JNDI树中的对象实例。

  • 我必须遗漏一些非常简单的事情。这是另一个我发现的例子,它展示了如何在Tomcat中配置JNDI对象 - http: //flylib.com/books/en/4.255.1.275/1/

    I must be missing something very simple. Here is another example i found which shows how to configure a JNDI object in Tomcat - http://flylib.com/books/en/4.255.1.275/1/


    • com.jspservletcookbook.StockPriceBean 已初始化?

    • 如果一个webapp使用com.jspservletcookbook.StockPriceBean对象并更改其状态,则此更改是否反映在所有其他Web应用程序中访问此对象?

    • 最重要的是,将创建多少个 com.jspservletcookbook.StockPriceBean 的实例 - 我需要它成为访问该对象的所有webapp的相同实例。

    • How is the com.jspservletcookbook.StockPriceBean initialised?
    • If one webapp uses the com.jspservletcookbook.StockPriceBean object and changes its state, is this change reflected across all other webapps that access this object?
    • And most importantly, how many instances of the com.jspservletcookbook.StockPriceBean will be created - I need it to be the same instance for all webapps that would access the object.

    @Ben Brunk - 目前会话数据是存储在由其中一个应用程序(上下文)管理的Map中。我需要的是其他应用程序调用此应用程序来检查会话是否有效。我的问题是,对于这些应用程序来获取管理会话数据的应用程序的服务,我需要使用JNDI来获取对相关对象的引用。

    @Ben Brunk - At the moment the session data is stored in a Map which is managed by one of the applications(context). All i need is for the other applications to call this application to check if the session is valid. My problem is that for these applications to get the services of the application that manages the session data, i need to use JNDI for them to get a reference to the relevant object.

    数据不会存储在JNDI中。我只想使用JNDI作为应用程序查找和使用Session Manager对象的手段。但问题是我读到的关于JNDI的所有内容都让我相信每次在目录中搜索或查找对象时它都会用于创建新对象。

    The data will not be stored in JNDI. I just want to use JNDI as a means for the applications to find and use the Session Manager object. The problem though is that everything i read about JNDI leads me to believe that it is used to create new objects every time an object is searched or 'looked up' in the directory.

    这是图表,我认为显示我想要实现的目标:

    Here is diagram i think shows exactly what i want to achieve:

    @EJP - 只是为了澄清一下,上面列出的函数只是共享对象可能做的例子。这些仅作为示例提供。它将执行的所有功能都与用户和登录的会话相关。例如账户信息(用户名,上次登录等),密码信息(有效期等),用户权限(即允许用户访问的应用程序的哪些区域)。

    @EJP - Just to clarify, the functions i listed above are just examples of what the shared object might do. These were provided just as an example. All the functions it would do are related to the user and the logged on session. For example things like account information(username, last login etc), password information (expiry date etc), user privileges (i.e. which areas of the application the user is allowed access to).

    鉴于问题确实与通过JNDI(或任何其他方式)的多上下文通信有关,而不是与对象的要求有关,它实际上没有意义列出该对象应该做的所有事情,所以我只列出了它可以做什么的例子。

    Given that the question is really related to multi-context communication via JNDI (Or any other means) and not about the requirements of the object it doesn't really make sense to list everything that the object is supposed to do so i just listed examples of what it could do.

    道歉,如果我把问题的方式弄糊涂了。

    Apologies if the way i put the question confused anyone.

    推荐答案

    如果您不关心使用LDAP,只需通过在其中创建表来管理会话信息来使用数据库本身。您应该能够找到大量有关如何执行此操作的示例,因为它是企业应用程序中的常见需求。事实上,我之前没有看过使用LDAP目录进行此操作的示例。看起来LDAP不适合跨域管理会话,因为它实际上并不是为了更新而设计的。

    If you don't care to use LDAP, just use the database itself by creating tables in there to manage session information. You should be able to find plenty of examples of how to do that because it is a common need in enterprise applications. In fact, I had not seen an example of doing it using the LDAP directory before. It seems like LDAP would be a bad fit for managing sessions across domains because it really isn't designed to be updated a lot.

    为了清楚起见:JNDI不是存储机制。它是一种在企业环境中命名资源,然后让容器管理与这些资源的连接的机制。您需要将会话数据存储在数据库中,或者您可以使用文件系统或NoSQL映射类型解决方案。

    Just to be clear: JNDI is not a storage mechanism. It is a mechanism for naming resources in an enterprise environment and then having the container manage connections to those resources. You need to store your session data in a database or you could use the filesystem or a NoSQL map type solution.

    这篇关于使用JNDI在Tomcat中共享servlet会话对象和数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆