This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!
See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

how to pass a javascript object to a Servlet

 
Ranch Hand
Posts: 590
6
jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i have created a javascript object which is an array of objects. Something like this:


i want to get this object in my Servlet somehow. I just don't remember if this is possible or i am doing something completely illogical.
the getParameter() method in Servlet will return a string, so that way is not applicable here.. any other possible alternative?

Thanks
 
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can pass the properties as individual parameters, or serialize the object as JSON and deserialize it on the server.

Which makes the most sense depends on factors we know nothing of.
 
s ravi chandran
Ranch Hand
Posts: 590
6
jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying. When you say to pass the parameter, you mean with getParameter() or something else ? Or are you talking about sending the individual map Object which is being created? I am thinking of avoiding Json as of now.
Also, the way I am creating the object in JavaScript , is that optimal way? The requirement is that, I have a loop in which every iteration will generate few attribute values which grouped together should be processed by the java end. I could think of this way only. Don't know if this is the best way.
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way of communicating from client to server is via a request sending request parameters.
Yes, that means Strings. You can't pass the javascript object directly.

Using JSON to stringify the object at the client side, pass it as a parameter, and then decode it at the server end is a very common approach.

If this is going to result in multiple requests to the server, you might want to think about shifting the looping logic server side.
 
Bear Bibeault
Marshal
Posts: 67463
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

s ravi chandran wrote:Or are you talking about sending the individual map Object which is being created?


You cannot send objects. Only strings. Hence the JSON. Or individual properties as strings.

I am thinking of avoiding Json as of now.


JSON, not Json. And if you want to "pass" JavaScript objects back and forth, you'll need to get over your aversion.

Also, the way I am creating the object in JavaScript , is that optimal way?


No. It's pretty sloppy. Here's a few pointers:
  • ObjectMap is a bad name. What is it really? Also, like in Java, lowercase first character.
  • ObjectMap ["ID"] is not the standard way to express this. Never use the [] notation for fixed property names, and property names should be in camel case just like variables. So objectMap.id and objectMap.name, and so on.
  • When you do use [], no space between the name and the braces.
  • I don't understand what the array is for.
  •  
    Bear Bibeault
    Marshal
    Posts: 67463
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    And if you really want to be JavaScript savvy:
     
    s ravi chandran
    Ranch Hand
    Posts: 590
    6
    jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Bear Bibeault wrote:

    s ravi chandran wrote:Or are you talking about sending the individual map Object which is being created?


    You cannot send objects. Only strings. Hence the JSON. Or individual properties as strings.

    I am thinking of avoiding Json as of now.


    JSON, not Json. And if you want to "pass" JavaScript objects back and forth, you'll need to get over your aversion.

    Also, the way I am creating the object in JavaScript , is that optimal way?


    No. It's pretty sloppy. Here's a few pointers:
  • ObjectMap is a bad name. What is it really? Also, like in Java, lowercase first character.
  • ObjectMap ["ID"] is not the standard way to express this. Never use the [] notation for fixed property names, and property names should be in camel case just like variables. So objectMap.id and objectMap.name, and so on.
  • When you do use [], no space between the name and the braces.
  • I don't understand what the array is for.


  • Thanks for replying. Well, about the actual example given here.. it was something else when i copied, i just changed the names and values, it was a matter of cut, copy and paste.. so, the naming came up like this. in actual code, we follow the naming standards. I will work on the JSON implementation. Will have to see what all changes i need, to make this work.

    Thanks
     
    Bear Bibeault
    Marshal
    Posts: 67463
    173
    Mac Mac OS X IntelliJ IDE jQuery Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    s ravi chandran wrote:. it was something else when i copied


    Just be aware that any code you copy is only as good as the source. And this, er, wasn't a particularly good example.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic