• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using a class without having it as a bean  RSS feed

 
Tobie Henderson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am having another problem in my JSP card-game project. I want to know if it is possible to use a user-defined class in a JSP document without creating a bean for it? For example, I want to use a Game object by saying:

Game game = new Game();

in a JSP document without creating a bean for it. Is that possible?

I need to do this because I want to add Game objects into a Hashmap (which is created at the application-level) so that the Game objects can be accessed by more than one user. I cannot create a bean for a Game as creating it at the session-level would be limiting its access to just one user.

I hope my question was clear. FWIW, I had asked my first question relating to this project over here:
http://www.coderanch.com/t/286041/JSP/java/Object-identifier

Thanks again,
 
Logan Owen
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, all you have to do is put in your jsp:



That will create a new instance of your custom class. If I am totally misunderstanding what you want, just post back, and I'll try to help you out more.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's your beef with using <jsp:useBean>? It'll have the same effect without polluting yout page with needless Java scriplets.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36446
454
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tobie,
I suggest creating the Game in a servlet and adding it to the HashMap there. You can then forward to your JSP and put the display there.

Too much logic in a JSP is a warning sign and leads to the polluted pages Bear described.
 
Tobie Henderson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had tried that, but it was a giving me a "cannot find symbol" error. I believe the problem is I should import it using:

<%@ page import="fully.qualified.ClassName" %>

I am not using it as a bean because I want to share the Game object between two different users, and declaring it at the session level would not allow that. Declaring it at the application level would be not be logical because I want it to be possible to have more than one Game object floating around at the same time.

Originally posted by Logan Owen:
Yes, all you have to do is put in your jsp:



That will create a new instance of your custom class. If I am totally misunderstanding what you want, just post back, and I'll try to help you out more.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Creating a new instance of the class on every page certainly isn't going to achieve any kind of sharing. Whether the class is treated as a bean or not is moot with regards to sharing.

Application context is the appropriate place to share objects across the application. If you need more than one instance, store them in an application scoped hash map by giving each game instance a unique key.

And as Jeanne pointed out, all processing to determine which game instance is to be used should happen in a servlet controller and simply passed to the page, keeping complex processing off the page.
[ December 06, 2004: Message edited by: Bear Bibeault ]
 
Tobie Henderson
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dude, that's what I already said in my first post. Here's what I said:

I need to do this because I want to add Game objects into a Hashmap (which is created at the application-level) so that the Game objects can be accessed by more than one user


I need to create an instance of the object in this page just so I can PUT it into a Hashmap. However, I am getting all sort of weird JSP compilation errors, such as "the servlet uses or overrides a deprecated API", and that it "uses unchecked or unsafe operations", and that I need to "recompile with -Xlint: Deprecation for details".

Anyway, I'm going to try and put this code into a helper class and see if that works. Thanks, anyway.

Originally posted by Bear Bibeault:
Application context is the appropriate place to share objects across the application. If you need more than one instance, store them in an application scoped hash map by giving each game instance a unique key.
[ December 06, 2004: Message edited by: Bear Bibeault ]

[ December 07, 2004: Message edited by: Tobie Henderson ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!