简体   繁体   English

从javascript(通过ajax)发送java类实例到jsp或servlet

[英]send java class instance from javascript (via ajax) to jsp or servlet

I am trying to send the JAVA CLASS INSTANCE from my javascript code to the JSP file via ajax . 我试图通过ajax将JAVA CLASS INSTANCE从我的javascript代码发送到JSP文件。 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 并且通过ajax发送这些数据,问题是,在JSP中,它接收它作为字符串而不是类

By the way, I am getting the java class instance like this : 顺便说一句,我得到这样的java类实例:

<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. 从根本上说,您通过ajax从客户端发送到服务器的内容始终是一个字符串。 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. 除非您在客户端和LiveConnect上使用Java小程序,否则客户端上根本没有Java类实例。

If you did have a Java class instance on the client (eg, from the applet), the only way to send it to the server would be: 如果你确实有客户端(例如,来自小应用程序),将其发送到服务器将是唯一的方式在Java类的实例:

  1. Serialize it to a byte stream. 将其序列化为字节流。

  2. Encode that byte stream into a string (Base64 or similar). 将该字节流编码为字符串(Base64或类似)。

  3. Send that encoded string to the server via ajax. 通过ajax将编码的字符串发送到服务器。

  4. Decode the string back into a byte stream on the server. 将字符串解码回服务器上的字节流。

  5. 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: 你编辑过你的问题说:

By the way, I am getting the java class instance like this : 顺便说一句,我得到这样的java类实例:

 <script type='text/javascript'> <script type ='text / javascript'>\n     var class_instance = "<%= my_class_instance %>"; var class_instance =“<%= my_class_instance%>”;\n</script> </ SCRIPT> 

That won't give you a "Java class instance" on the browser. 这不会在浏览器上为您提供“Java类实例”。 At best, you'll have a string with some information in it. 充其量,你将拥有一个包含一些信息的字符串。 More likely, depending on what's inside your my_class_instance server-side variable, you'll have a JavaScript syntax error. 更有可能的是,根据my_class_instance服务器端变量中的内容,您将遇到JavaScript语法错误。 (Eg, if you have a ' or a line break or an invalid JavaScript escape sequence, etc., inside it.) (例如,如果你在其中有'或换行符或无效的JavaScript转义序列等等。)

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. 如果您认为这是一个Java类实例,那么在尝试编写此代码之前,您需要退一步研究Web应用程序的基础知识。

You are writing scriptlet to your jsp. 您正在为您的jsp编写scriptlet This is a bad thing 99.9% of times. 99.9%的时间这是一件坏事。 You should avoid it all the times. 你应该一直避免它。 Most of the case, you can write it better with the jstl . 大多数情况下,您可以使用jstl更好地编写它。

And more importantly, your code is not going to put the java object instance to client browser. 更重要的是,您的代码不会将Java对象实例放到客户端浏览器中。 It's totally wrong. 这是完全错误的。

Please take a look. 请看一下。 How to avoid using scriptlets in my JSP page? 如何避免在JSP页面中使用scriptlet?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM