This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes Object identifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Object identifier" Watch "Object identifier" New topic
Author

Object identifier

Tobie Henderson
Greenhorn

Joined: Nov 15, 2004
Posts: 20
Hi,

I am having a slight problem in my JSP project. Basically, my project is a two-player game over the Internet. There are two bean classes - Player and Game. Now, I want these two players to use the same Game object. The problem is: the name of the Game object cannot be hard-coded into the JSP code. So, for example, there will be a auto-generated name for the Game object such as game123, and I want the JSP document to create an instance of Game by:

Game game123 = new Game();

However, I cannot hard-code it this way, because the next game to be shared by two players will be called game124.

Intuitively, I thought the answer would be to store the name of the Game object in a String and then use it to create the object. But does anyone know if Java allows objects to get their names from String variables?

If that is not possible, has anyone faced such a problem before and know a way to get around this? I am open to any sort of idea, because I really need a way to get around this.
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Why dont you put the game object in a HashMap with the name of the object as a key?

BTW, why are you doing this? Do you want a differrent game object per user that you want to persist across requests? You could easily store the game object in the session
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

"Tobie H",

We're pleased to have you here with us on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tobie Henderson
Greenhorn

Joined: Nov 15, 2004
Posts: 20
Hey Jayesh,

That's what I was looking for! The HashMap thing works perfectly for me. Thanks!

I have to do this because the same Game object is used by two different users. If I store it as a session, I cannot have both the players accessing it.

Originally posted by Jayesh Lalwani:
Why dont you put the game object in a HashMap with the name of the object as a key?

BTW, why are you doing this? Do you want a differrent game object per user that you want to persist across requests? You could easily store the game object in the session
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
Originally posted by Tobie Henderson:
Hey Jayesh,


I have to do this because the same Game object is used by two different users. If I store it as a session, I cannot have both the players accessing it.





gotcha!! but, wont all your game objects build up in your system over time?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object identifier
 
Similar Threads
How can I add elements in Hashmap?
OO design question?
my jsp code is ugly, how can i clean it?
Where to put method that creates core of a class?
Errors when compiling these classes...