I am trying to send the JAVA CLASS INSTANCE from my javascript code to the JSP file via ajax . How can i send ? I tried sending the instance like this :
data = {}
data['my_instance'] = JAVA_CLASS_INSTANCE
and sending this data via ajax , problem is , in JSP, it is receiving it as a string rather than a class
By the way, I am getting the java class instance like this :
<script type='text/javascript'>
var class_instance = "<%= my_class_instance %>"; //if this method is wrong, plz tell me correct method to get instance and send via ajax. Already I have a form, along with the form data, i am trying to send this class also. If there is anyother good way for this, just tell me.
</script>
(Updated below)
Fundamentally, what you send from the client to the server via ajax is always a string. It can only be turned into something else by a server-side process interpreting it.
The question doesn’t seem to make any sense. Unless you’re using a Java applet on the client and LiveConnect, you don’t have a Java class instance on the client at all.
If you did have a Java class instance on the client (e.g., from the applet), the only way to send it to the server would be:
Serialize it to a byte stream.
Encode that byte stream into a string (Base64 or similar).
Send that encoded string to the server via ajax.
Decode the string back into a byte stream on the server.
Deserialize it on the server.
…and there would almost certainly be a much better way of getting that information from the client to the server.
You’ve edited your question to say:
That won’t give you a “Java class instance” on the browser. At best, you’ll have a string with some information in it. More likely, depending on what’s inside your
my_class_instanceserver-side variable, you’ll have a JavaScript syntax error. (E.g., if you have a'or a line break or an invalid JavaScript escape sequence, etc., inside it.)If you believe that’s a Java class instance, you need to step back and study the fundamentals of web applications before trying to write this code.