aspose file tools*
The moose likes Servlets and the fly likes Implementing/getting session in class without any effects in old code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Implementing/getting session in class without any effects in old code" Watch "Implementing/getting session in class without any effects in old code" New topic
Author

Implementing/getting session in class without any effects in old code

Mohanasundar Nagarajan
Greenhorn

Joined: Mar 23, 2010
Posts: 19
Dear All,

Herewith I will explain the scenario first,

We are using JSP/Servelt based web application running on Tomcat and I'm in the process of migration, struggled here.

In a servelet, 1 object being initialized that contains an pre initialized variable x = "abc".
The same class is been instantiated in more than 200 servlets.


Now the problem is the value of x has to be updated from the session.
I don't want to address all the files instead is it possible to get session in that class with out altering its methods and constructors.

Is it any easiest way to address this issue?

Kindly help me, I'm really struggling.

Regards,
Mohanasundar.n



Kumaravadivel Subramani
Ranch Hand

Joined: Jul 05, 2008
Posts: 166

You can pass it to any POJO class, have read the below two conversations gone here already.
webpage1
webpage2


No pain, No gain.
OCJP 1.6
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12821
    
    5
Now the problem is the value of x has to be updated from the session.


Are you really saying that a variable used in 200 servlets changes according to a user variable contained in a session?

Given that any number of users could be using these servlets, how do you plan to keep them from mixing up the uses of that variable?

Please clarify

Bill
Mohanasundar Nagarajan
Greenhorn

Joined: Mar 23, 2010
Posts: 19
Yes really instantiating,

Let me Explaining clearly, if any mistake forgive me.

We have an Application of 10 years old, which supports both Oracle and PostgreSQL databases.

Wherever requires db specific query/logic, we will use.



Till now we are having 2 Context for Oracle and PostgreSQL separate

For Oracle context, We have class like



For PostgreSQL context, We have same class like




classes will be used in servlets like,

[b][/b]

DBType is used in more than 200 servlet + jsp's.

Now I'm trying to join both the context,
Consider I'm keeping DBType.type in session



If I get session in DBType, I may change like this,


Hope you got my requirement.

Thanks in advance,
Mohanasundar N
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18882
    
    8

I'm going to assume that putting the database type in the user's session was the right decision. Although that certainly isn't obvious from what you have posted so far. For all I know it might be better to put the database type in the application scope instead of session scope.

So then that DBType class would need to have access to the user's session. If it doesn't have that now, you would have to change the DBType class so that the servlet can pass it the session. And that would mean you would have to change every servlet which uses it.

But I would really prefer to answer your first question. The one where you were told why you had to change the system in the first place. Without knowing your actual requirements, we're just left discussing incompletely-described proposed solutions to a problem which we know nothing about. So anything you get as an answer won't be very useful.
Mohanasundar Nagarajan
Greenhorn

Joined: Mar 23, 2010
Posts: 19
I cannot go for application scope as user can choose db either oracle or PostgreSQL
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12821
    
    5
If all those classes need is a single string, they certainly don't need the whole session. Whats wrong with a constructor

DBType( String theType) // where the String can come from anywhere, session, cookie, hidden variable, etc.

Bill
Mohanasundar Nagarajan
Greenhorn

Joined: Mar 23, 2010
Posts: 19
If I overload constructor, I need to address all the pages where default constructor is been used.

Is there any possibility to do this without overloading by the way of listeners or any other mecanisam to do this.

If not possible I have to address all the pages if my team lead is ready or they have to continue with two context.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12821
    
    5
Are you really saying that a variable used in 200 servlets changes according each user's preferences?

Given that any number of users could be using these servlets, how do you plan to keep them from mixing up the uses of that variable?

I don't see any quick and dirty way around it. The whole set of servlets contain a bad design decision - no easy way out.

Bill
Mohanasundar Nagarajan
Greenhorn

Joined: Mar 23, 2010
Posts: 19
Thanks bill. We are providing supports e-tendering and for few client are using oracle others postgress.
But source code are similar.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implementing/getting session in class without any effects in old code