Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how to allow jsp and applet to access SAME object instance ..  RSS feed

 
sarim raza
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can a jsp file and an applet in that jsp file both access the get/set method of an instance of one particular class ?
problems
1. where do i store the applet class, and the accessor class, can i use codebase and store applet class also in WEB-INF/classes where the accessor class is currently ?
if yes how ?
2. suppose class name is Bid,
first jsp creates instance Bid b = new Bid()
and sets some property
but then applet class will create another instance
Bid b = new Bid()
but this will not get the same value since a new isntance is created .. is the only solution using static method ??
thanks
 
Shawn Bayern
Author
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sarim raza:
how can a jsp file and an applet in that jsp file both access the get/set method of an instance of one particular class ?
problems
1. where do i store the applet class, and the accessor class, can i use codebase and store applet class also in WEB-INF/classes where the accessor class is currently ?
if yes how ?
2. suppose class name is Bid,
first jsp creates instance Bid b = new Bid()
and sets some property
but then applet class will create another instance
Bid b = new Bid()
but this will not get the same value since a new isntance is created .. is the only solution using static method ??
thanks

An applet that runs within a browser and a JSP that runs within a server do not share a JVM or any local memory, so they can't share objects automatically. You need to provide some mechanism to communicate data explicitly between the two. Historically, RMI has been popular, though these days, many people see web services as more interesting.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sarim
how about using a "singleton"??? i don't know if making a singleton will work for you because if u want to have multiple instances at other points (than this particular situation) then it will not solve ur problem.
other solution i can think of is following,
-create another servlet that stores the object u want to store
-pass that object to the servlet from JSP.
-that servlet stores the object say in servlet context
-make ur applet to ask for the object to the servlet
but here the problem is not that easy to solve because if multiple JSP's instances are creating the object then which object instance should the servlet return when asked by the applet so that it is the correct instance (the same instance written by the jsp in which the applet is)?
to make this work properly u might want to have some id that associates an object instance with the jsp and let the applet know about this. so jsp passes the object to the servlet with some id which is also known by the applet. the applet asks for the object instance with the same id.
i don't know how do i sound? may be stupid! but this is just an idea....may b there is some better solution...
regards
maulin
 
chanoch wiggers
Author
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that instead of sharing an instance on two machines on two VMs, you might be better to just setting values in the applet dynamically.. Assuming that the values are not changing drastically between requests, wouldnt it be possible to write the applet reference in the HTML dynamically and setting initialization parameters for the applet dynamically too.
Unless you are trying to build some kind of dynamic counter on a web page that shows stock prices or whatever, then I cant believe that there is no alternative ot RMI from an applet to a servelt! I think the best you might do in this case is setting an automatic refresh on the page and writing out the values of the session stored bean as applet parameters so taht the applet and the jsp synchronize on the bean values. The applet can then send values back using sockets: link provided
http://www.j-nine.com/pubs/applet2servlet/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!