aspose file tools*
The moose likes JSP and the fly likes how to pass a javascript object to a Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to pass a javascript object to a Servlet" Watch "how to pass a javascript object to a Servlet" New topic
Author

how to pass a javascript object to a Servlet

s ravi chandran
Ranch Hand

Joined: Dec 20, 2010
Posts: 154
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

Joined: Jan 10, 2002
Posts: 61417
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
s ravi chandran
Ranch Hand

Joined: Dec 20, 2010
Posts: 154
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

Joined: Jul 06, 2005
Posts: 1018
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

Joined: Jan 10, 2002
Posts: 61417
    
  67

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

    Joined: Jan 10, 2002
    Posts: 61417
        
      67

    And if you really want to be JavaScript savvy:
    s ravi chandran
    Ranch Hand

    Joined: Dec 20, 2010
    Posts: 154
    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

    Joined: Jan 10, 2002
    Posts: 61417
        
      67

    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 agree. Here's the link: http://aspose.com/file-tools
     
    subject: how to pass a javascript object to a Servlet