ways to pass objects to a servlet?

I think the best way to show my problem with an example.
Lets say I have the following:

MyClass myInstance = new MyClass();

If I would like to pass the instance myInstance to an servlet, the way I have been using is calling a static function in the servlet
something like:

MyServlet.addMyClass(myInstance);

and in the function addMyClass I can either add myInstance to a static hashtable and when I call the servlet, I use a parameter to access the current MyClass in the hashtable (I can also do this by letting addMyclass take in the request as a parameter and add myInstance to the session instead of a hashtable).

But the problem using this method is that I need to clean the hashtable (or session) regularly so it wont fill up the memory of my server.
I can't delete myInstance from the hashtable after each call to the servlet since a user could press a servlet link more than once and then the data should still be available.

Any idea how I can do this differently so I can pass an object to an servlet which I can then use inside the doGet method?

Thank you both for your answers.
First, I'm not using an applet, I'm using jsp.

After thinking about this I see I'm talking nonsense... asked the question to quickly :)

I'm was talking about keeping object between requests, and I can't do that except by storing it somewhere of course!.

Tómas Helgi, This article (the part about HttpMessage) shows many things about server/client communication that I did not know about, good to know how I can pass objects around like this. And after reading that I know what I have to do to make my code work correctly.

Objects, I did not know that a request has setAttribute , but it's good to know that (even though it does not help here since I was actually talking about keeping data between requests).

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…

Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program.
Download Eclipse installation zip file:
Extract files from zip file:
Download and install JDK 8:
Open Eclipse and …