File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Accessing a session scoped variable anywhere outside the session scoped context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Accessing a session scoped variable anywhere outside the session scoped context" Watch "Accessing a session scoped variable anywhere outside the session scoped context" New topic
Author

Accessing a session scoped variable anywhere outside the session scoped context

Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
I am stuck with this very interesting requirement and need your inputs!

In our web application, the goal is to pass a user's company to a static method getSession() of a ConnectDatabase class that returns a hibernate session. One common area, the HttpSession is where we store the user's companyName as it is user specific but am unable to find a way to access it outside of it when needed.

Our entire application was originally built for handling just a single database and all works fine until we decided that we will begin handling multiple databases from the same application by passing the property, user's company name, based on which we would return the user's database specific sessionFactory session.

This implementation too was quite simple in the static method

changed to

where we would create a hashMap<String, SessionFactory> where the key string is the company name.

Now comes the problem of changing the entire application where there are references to this method usually accessed as
ConnectDatabase.getSession()
to be changed to
ConnectDatabase.getSession(companyName) and what I was trying to achieve is a way to store this companyName in a dummy class initialized in a block of a final variable using a run time declaration as follows.

If I were able to achieve this I would be able to access the companyName of the user from any instance of this class CompanyName, however, the method that assigns the companyName it's value changes too every time this method a new instance of this class is created.

And, I cannot create a singleton class public static final companyName INSTANCE = new companyName(); as it would then refer to the latest added companyName instance

However, coming up with that method that initializes this final variable this way failed, and I don't see any way to achieve something similar to the following:


Or is there any other way to achieve this, please don't suggest me to change all of the 1000 methods to send the companyName to get the right sessionFactory as sometimes there is no access to get the companyName at all as there is no request, session variables available in utility classes needing access to the session factory and that have nothing to do with the presentation layer where you usually have access to request, session?

Cheers
Krishna
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
I guess there is no solution really but passing this value, user specific from the presentation layer every each time someone makes a call to the business layer and so I have no choice but change all these thousand methods.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Please make your post more attractive by putting the code between the code tags ;)


JDBCSupport - An easy to use, light-weight JDBC framework -
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110
After appending the CODE block as per the request above, am I the only one seeing the page appearing extended out of the default browser page width?

Fixed, after I fixed the CODE block comments' formatting!
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
I didn't completely understand your situation.

Is it like you need session's data in a class where you don't have access of session object?

Why don't you pass session object to this class?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

ankur rathi wrote:Why don't you pass session object to this class?

Because that would be a severe violation of best practices.

It's a poor idea to pass container objects around. Not only does it create strong binding that should not exist, objects like requests, responses and sessions can go out of scope and creating references to them can be a recipe for trouble.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Bear Bibeault wrote:
ankur rathi wrote:Why don't you pass session object to this class?

Because that would be a severe violation of best practices.

It's a poor idea to pass container objects around. Not only does it create strong binding that should not exist, objects like requests, responses and sessions can go out of scope and creating references to them can be a recipe for trouble.


Doesn't controller pass these objects to action classes, in frameworks?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Yes, and that's as far as they should go. Passing them to the lower layers of the application not only strongly binds those classes to the servlet environment, it creates a violation of the principle of Separation of Concerns.

It's a much better practice to only pass the data, not the container instances.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
If you think you need to pass a session around, it is time to rethink your structure.

Instead create a custom class to hold your user data and keep a reference to that in your session.

Make that class Serializable and you can persist it to disk or a db. Let the container do its job according to the API.

Bill
Rama Krishna
Ranch Hand

Joined: Oct 16, 2007
Posts: 110

After putting some thought into this, the only way to get this done is to either pass a reference of session itself, which is not a good idea as you have to be able to access it again by composition by creating and passing instances of it every time, this would cause performance issues and this does not follow the standard conventions of separating concerns,
OR
an instance of a new class (one that has some of the common properties for the current user) that is instantiated run time in every action/servlet class (presentation layer) in its init() method used and set/accessed via composition both at the presentation layer and passed on to business layer via construction setter, to force all the business classes to have access to this object when their methods are used, unlike the static methods and utility methods that don't have or cannot deal with request, session directly have to be dealt separately! At the presentation layer, I would add this class object via composition in the super class as follows and to pass it on to the Business Tier, I would add it to another super class extended by all Business classes that HAS to be instantiated via a constructor whose argument is this class object.

// User class containing user specific details
Class User
{
public String companyName;

// getter and setter
}

Presentation layer access:

ParentClass that is extended by all servlet classes would maintain User class as a property via composition and is set in the init() method.

The Question is, how do we get this ActionServlet that calls this ParentAction class, set this one parent property run time in the STRUTS framework or rather how do I get this property set via composition when ever an Action is invoked!!

Business layer access:
SomeDAOInterface someDAO = new SomeDAOImpl(MY_PRESENTATION_CLASS_OBJ_REF);
someDAO.callSomeBusinessMethod();

I do have to create individual constructors to pass on this class object overriding the default ones in all of the business classes though! I guess this atleast reduces the changes I have to make.

Cheers
Krishna
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing a session scoped variable anywhere outside the session scoped context