This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Applets and the fly likes how to send object reference to applet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "how to send object reference to applet?" Watch "how to send object reference to applet?" New topic
Author

how to send object reference to applet?

Neeraj Macker
Ranch Hand

Joined: Nov 10, 2004
Posts: 77
how can i send an object reference to an applet..?
the param values are string.

when i do this , it doesnt work.

var applet=document.applets["End Session Applet"];
applet.start();
applet.o="<%=sessionObj%>";
applet.endSession();

here "sessionObj" is the object reference i want to send to my applet.Also "o" is an instance variable of Object type in my applet class.


the abobe mentioned code gives NullPointerException for "o" object.that means it is not set.....please suggest a solution...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41144
    
  45
The code you mention can't throw NullPointerExceptions, because it is JavaScript, not Java. The Exception must be either from the server-side JSP code, or the client-side Applet code.

You can't mix server-side JSP code with client-side JavaScript code, and hope to transfer object references. The best you can do is insert the String value into HTML pages, which is what you are already doing. Then you should be able to transfer it to your applet.

But it's hard to know where things are going wrong without seeing the exception and its stacktrace in full, as well as the code that causes it.


Ping & DNS - my free Android networking tools app
Neeraj Macker
Ranch Hand

Joined: Nov 10, 2004
Posts: 77
Originally posted by Ulf Dittmer:
The code you mention can't throw NullPointerExceptions, because it is JavaScript, not Java. The Exception must be either from the server-side JSP code, or the client-side Applet code.

You can't mix server-side JSP code with client-side JavaScript code, and hope to transfer object references. The best you can do is insert the String value into HTML pages, which is what you are already doing. Then you should be able to transfer it to your applet.

But it's hard to know where things are going wrong without seeing the exception and its stacktrace in full, as well as the code that causes it.



the error is obviously coming from the applet, when i use the object "o" for some operation.

my question is how can i send an object reference to an applet...string, int float can go in param tags....but i need to send an object reference....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41144
    
  45
You can't send an object reference, period. You can send the object value, which, for an immutable object like a string, amounts to just about the same thing.

If for some reason you absolutely, positively, have to transfer objects instead of their values, you'll need to use serialization between the applet and the servlet, at which point JavaScript can't help you.

If something does make it into your applet and gets assigned to "o", but causes a NullPointerException, the question is: Does the assignment work at all? What does the JavaScript code look like that gets generated?

Also, you should not call Applet.start directly. It gets called automatically by the browser/JVM.
Neeraj Macker
Ranch Hand

Joined: Nov 10, 2004
Posts: 77
Originally posted by Ulf Dittmer:
You can't send an object reference, period. You can send the object value, which, for an immutable object like a string, amounts to just about the same thing.

If for some reason you absolutely, positively, have to transfer objects instead of their values, you'll need to use serialization between the applet and the servlet, at which point JavaScript can't help you.

If something does make it into your applet and gets assigned to "o", but causes a NullPointerException, the question is: Does the assignment work at all? What does the JavaScript code look like that gets generated?

Also, you should not call Applet.start directly. It gets called automatically by the browser/JVM.


thanks, i am lucky as i cud extract the key value from the object and send it as a String to the applet. it was because of this value that i needed the object reference.

yes i will not call applet start , that was just a test case.

the gist of the discussion is that from javascript an object reference cannot be send to the applet.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Neeraj Macker:

the gist of the discussion is that from javascript an object reference cannot be send to the applet.


No, the point is that you can't send an object reference from a JSP scriptlet (which runs on the server) to anything running in the browser (JavaScript or the Applet). Remember that all of the JSP scriptlets run first, and their output is used to make up the page, which is then sent to the browser, and then the page is displayed, whereupon the JavaScript and applets run.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to send object reference to applet?
 
Similar Threads
map reference syntax ?
Applet Servlet Serialized Object
Why my servlet is not answering when I try to send an object?
How to send object from Applet to spring controller .
Sending an object to applet