How to get an session information in an java application

Discussions

General J2EE: How to get an session information in an java application

  1. Hi,

    We hava an web application, in that i am setting one datasource key and value into the session i.e
    request.getSession().setAttribute("Datasource", "OracleDS")

    And i need to get this value in the java application, so that i can get connection from datasource.

    I dont really want to pass the Datasource value from JSP/Servlet to java application to get connection from datasource.

    Could any one advice me on this...

    regards,
    Jaleel
  2. The session is a web object.It is contained in the web HTTPRequest class.
    It is not available to normal applications, since they do not have a HTTP request generated.

    Why dont you consider putting it into a property file instead, or an xml file?

    I think the most dirtiest solution to your problem would be to use a singleton, an object that is shared throughout the jvm. You can do it this way, but i strongly recommend against it.

    Cheers
    Ajay
  3. Hi Ajay,

    Let me try your suggestion and come back to you.

    Thanks in deed.


    regards,
    MohdJaleel
  4. Hi Ajay,

    Could you please give me some example of Singleton class to set a string value and get it back in an java application..

    regards,
    MohdJaleel.
  5. /**
    * Primitive singleton class: key thing is constructor is private and
    * acn be used only thru static getInstance() method
    * Please refer to a tutorial for this pattern implementation for better understanding.
    * In your case, you would have to have a String instance inside this class
    */
    public class MySingleton {

    //
    // CONSTRUCTOR
    //
    private MySingleton() {
    }

    /**
    * Method getInstance.
    * @return MySingleton
    */
    // Get the singleton instance.
    public static MySingleton getInstance() {
    synchronized (MySingleton.class) {
    if (objSingleton == null) {
    objSingleton = new MySingleton();
    }
    }
    return objSingleton;
    }

    //
    //Business methods also
    //

    }

    Also, in case u have decided to use singletons, there was a good article on IBM developer works about singletons and better ways of using them.
    Dont have the link, but u cud look it up.

    I still think the property file approach would be better. Singletons shud be avoided as much as possible as they are availabe throughout the jvm to all applications.
    Also, its a little bit of a tricky thing to handle them across jvms(for clustering etc).

    Anyways, its your call after all.
    Cheers
    Ajay
  6. Hi Ajay I am also running same kind of problem... Can u tell me how can we use the property file as it takes static value so how I need to set values from session at the very first time. Kindly reply me back at anjali dot saxena at gmail dot com Thanks
  7. Hi,We hava an web application, in that i am setting one datasource key and value into the session i.e request.getSession().setAttribute("Datasource", "OracleDS")And i need to get this value in the java application, so that i can get connection from datasource.I dont really want to pass the Datasource value from JSP/Servlet to java application to get connection from datasource.Could any one advice me on this...regards,Jaleel
    Hi
    First of all let us know where is your java application is running. If you want to share that attribute using session kind of thing, one solution is there using Stateful session bean and accessing that bean using your java application. Store the value as a Instance variable of SFSB. But it might prove very costly.
    Thanks and regards,
    Amit
  8. Hi Amit,

    I am running java application under Oracle Application Server 10g.

    Give me an idea....

    regards,
    MohdJaleel.