• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to pass a javascript object to a Servlet

 
Ranch Hand
Posts: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 595
6
jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And if you really want to be JavaScript savvy:
     
    s ravi chandran
    Ranch Hand
    Posts: 595
    6
    jQuery Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    I'm thinking about a new battle cry. Maybe "Not in the face! Not in the face!" Any thoughts tiny ad?
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic