This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to pass a javascript object to a Servlet

 
s ravi chandran
Ranch Hand
Posts: 328
Java jQuery
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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: 328
Java jQuery
  • 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.
 
Stefan Evans
Bartender
Posts: 1691
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
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • 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
    Author and ninkuma
    Marshal
    Pie
    Posts: 64708
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 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: 328
    Java jQuery
    • 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
    Author and ninkuma
    Marshal
    Pie
    Posts: 64708
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic