2

i have in javascript

var data = new Object();

and some attributes for this object are:

data.name = "mike";
data.id = 1;

i use jquery to pass the object to a jsp page

var request = $.ajax
({
 url: "test.jsp",
 type: "post",
 data:  'obj='+ data,
success:    function(msg) {
     $('#response').html(msg);},
  error:function (xhr, ajaxOptions, thrownError){
 alert(xhr.status);
 alert(thrownError);
}  
});

in the jsp i say

Object objParam = request.getParameter("obj"); out.println(objParam.name);

this doesn't seem to work. please help.

3 Answers 3

2
  1. Because you are posting to the JSP, make sure that you handle inside a doPost(request, response) method.
  2. Generally, all data passed should be in JSON format. In your case, it should read

    data: {'obj' : data}
    
  3. To parse on the server side, use the org.json library:

        JSONObject obj = (JSONObject) JSONValue.parse(request.getParameter("obj"));
        Iterator<?> keys = obj.keys();            
        //iterate over the properties of the JSON object
        while( keys.hasNext() ){
            String key = (String)keys.next();
            if( obj.get(key) instanceof JSONObject ){
                //a JSONObject inside the JSONObject
            } else {
                //process
            }
        }
    

    See the docs: JSONObject

Sign up to request clarification or add additional context in comments.

5 Comments

I changed the way the data is passed to JSon but I don't know the code syntax for a dopost.
yes, but i'm passing a javascript object with attributes through jquery and then to jsp. i want jsp to dissect the object and return the individual attributes. how do i do that?
I just edited my answer to include how to process on server side.
If you find an answer that works, please let the community know by marking it as correct.
the answers given here do not work, they are overcomplicated and i am just a noobie
0

The object of javascript is different from the object of java. The two are not at all comparable. Here you are sending javascript object as is. This should be changed to json string.

data:  'obj='+ JSON.stringify(data),

And also on server you should get the json string and then convert that json to java object by using some of the mechanisms like ObjectMapper

Object objParam = request.getParameter("obj");

ObjectMapper om = new ObjectMapper();
om.readValue(objParam.toString(), ...);

2 Comments

so how do i dissect the javascript object in the jsp? i want the attributes as is shown above out.println(obj.name);
ObjectMapper class has provided the readValue function which allows you to convert. See this tutorial mkyong.com/java/how-to-convert-java-object-to-from-json-jackson
0

you can pass json data(javascript object converted into json data using json.stringify()) in client side.In service side ,using Gson Libarary you can map json data with java object.

    public class User{
private String userName;
//getters
//setters
    }

Json format

{"userName":"test_username"}

Now you can map json data to java object using GSON library.

for example plz refer the below link

https://sites.google.com/site/gson/gson-user-guide

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.