Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Shared data through ServletContext  RSS feed

 
Michelle Baird
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish to share a simple class between many servlets, and tried using getAttribute() and setAttribute() methods of ServletContext. The set method appears to work alright, but when I get the attribute in another servlet and cast it down to the class, I get a ClassCast Exception. However, when I log the getClass().getName() of the attribute, it claims that it is indeed of the type to which I tried to cast it down. Any ideas? Here is the code I used:
Object newObj =(config.getServletContext().getAttribute(SESSION_TABLE_KEY));
DummyHead newDummyHead = new DummyHead();
if (newObj != null)
{
newDummyHead = (DummyHead)newObj; // exception thrown here
}
 
Thomas Goorden
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative method *might* be (haven't tried it myself) through the use of sessions:
A java server will normally hold only one session per client per browser, which means you could use a session and use:

I believe these methods have been deprecated the newest API for a different naming system, check what's available first.
I'm assuming you want to hold an instance of a class accross servlets, otherwise I don't see the point.
[This message has been edited by Thomas Goorden (edited August 29, 2001).]
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thomas:
You are right, java.lang.String)" TARGET=_blank rel="nofollow">getValue(...) and putValue(...) of the
HttpSession are deprecated.
However, the replacement is getAttribute(...) and setAttribute(...) as suggested in the API.
Michelle:
Are you sure there was no typo when you are casting the
Object. I agree the JSP/Servlet maybe not compile in the first
place, but I would need more info on the foll line:
=(config.getServletContext().getAttribute(SESSION_TABLE_KEY))
Specifically what type of object is your config
ie; what class?
Let us know.
regds.
- satya
 
Michelle Baird
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable "config" refers to the ServletConfig class. The servlet compiles as it is now, just throws a casting exception when I pull the object out.
Unfortunately, I can't use the HttpSessions to hold the info, because we have to allow for customers who disable cookies.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used the servlet context to store objects, without much trouble. The object i store are value objects (simple name value pairs in a HashMap)

Perhaps there is a problem with the object you are storing? For example, if there is a certain field that doesn't serialize well, like a db Connection? I'm really not sure if this would be a 'class cast exception', I'm grasping at straws.

The only thing I can suggest is:
- are you *sure* the object you pull out of the context is truly castable to that class?
- typos

Another small point: Unless there is other code in between, you are needlessly creating a new DummyHead object.


Sorry that might not have been helpful. But just a "must be possible, I've done it" kind of post.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The variable "config" refers to the ServletConfig class. The servlet compiles as it is now, just throws a casting exception when I pull the object out.

I am a little worried abt this.....
I am a little flaky on the ServletConfig object...but,
As far as I understand (and my next cube neighbour) there is
ONLY ONE ServletConfig object per servlet.
Hence, if two users access your servlet, then it may cause
a problem in that one of the Attribute will be overwritten.


Unfortunately, I can't use the HttpSessions to hold the info, because we have to allow for customers who disable cookies.

Session and Cookie are two different things. I think you can
still have a HttpSession even when a user disables Cookies.
You probably should use the URLrewritting.
Again as I mentioned, I am a little falky on these, I will
brush up my dusted books and update if I find something
different from what I have said here....
or hopefully someone else will correct me.
regds.
- satya
ps:
Looks like I was racing with Mike in posting a reply. Hopefully, he could review what I said. Thanks Mike.

[This message has been edited by Madhav Lakkapragada (edited August 30, 2001).]
 
Michelle Baird
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought maybe some members of the dummy class were causing the problems, so I removed them all. However, I still get the error. Although the getClass().getName() call says that the object I pull out is a DummyHead, I used instanceof to doublecheck. Instanceof says that the object is not an instance of a DummyHead. Also, this only happens between different servlets. I can set and get DummyHeads all I like within one servlet. The problem is when another servlet tries to get a DummyHead that the original servlet created. How can the object be of class "DummyHead" but yet not be an instance of DummyHead?
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Instanceof says that the object is not an instance of a DummyHead.

thats more confusing. I understand that the object is
not an instance of the DummyHead class, so no wonder
you get a class cast exception...
Also, this only happens between different servlets. I can set and get DummyHeads all I like within one servlet. The problem is when another servlet tries to get a DummyHead that the original servlet created
so what config are you using?
config object of the original servlet or
the another (second) servlet?
I think it plays a very important role...
Is its okay with you, post pieces of the real code so
someone could point out the mistake...
(or send the code to me, I can try it out tomorrow.
i will be gone for the long weekend though. also not
soliciting...send it if its okay with you)
- satya
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a ClassLoader problem, my guess is that they ARE the same object, just loaded under differnt ClassLoaders, meaning the Classes are not equal. (had this problem with WebLogic and storing objects on the session)
You can try opening an object stream with a piped stream, passing the objcet in one end, out the other and should now be able to cast it to the correct type. (It will of course have to Serializable)
As an alternative, have you considered creating an external class as an object cache and storing it there?
Dave.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure that all servlets are members of the same "web application" - If the servlet engine thinks they are different applications then I think the classloaders will be different.
Which servlet engine is this?
Bill

------------------
author of:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!