1

I'm doing a post with jQuery

  $( "#input" ).focusin(function() {
    $(document).keyup(function(e){
    if(e.keyCode==13)
    {   
      $.post("other.jsp",{var:var})
         .done(function(data){

       });
    }
}

Then the other jsp execute a query and returns:

out.println(rsvar.getString("name"));
out.println(rsvar.getString("lastname"));`

My code works... But the Data is retrieved in a single String and I want to append rsvar.getString("name") in an input like a value and rsvar.getString("lastname"); in other input.

$("#name").val(rsvar.getString(name));
<input id="name" >

A friend told me that he uses json_encode + php and iterates the data like data[i].name

So he can split the data...

What library can I use to solve my problem?

trinaldi
  • 2,872
  • 2
  • 32
  • 37
zickno
  • 115
  • 1
  • 3
  • 10
  • [Check out this answer regarding JSON encode for JSP](http://stackoverflow.com/questions/3207092/what-is-the-jsp-equivalent-to-json-encode-in-php) – itsmejodie Jan 17 '14 at 00:01

2 Answers2

1

Use can do a shortcut like

<%@ page trimDirectiveWhitespaces="true" %>
out.print(rsvar.getString("name"));
out.print("$$");
out.print(rsvar.getString("lastname"));

in jquery

        $.ajax({
            type: "POST",
            //dataType: "html",
            url: "other.jsp",
            data: {var:var},
            success: function(data) {
                // on success
               // alert("="+data+"=");
                var arr=data.split("$$");
                alert(arr[0]);
                alert(arr[1]);

            },
            error:function(xhr,status,error) {      
                alert(error);
            }
        });

OR you can use

http://json-taglib.sourceforge.net

//json-lib-2.4-jdk15.jar

json-taglib-0.4.1.jar (check version) to print JSON Object in JSP.

new JSONWriter(response.getWriter())
                      .object()
                        .key("fname")
                        .value(rsvar.getString("name"))
                        .key("lname")
                        .value(rsvar.getString("lastname"))
                      .endObject();
                    response.flushBuffer();

in jquery

        $.ajax({
            type: "POST",
            dataType: "json",
            url: "other1.jsp",
            data: {var:var},
            success: function(data) {
               //console.log(data);
               alert(data['fname']);
               alert(data['lname']);
            },
            error:function(xhr,status,error) {      
                alert(error);
            }
        });
Tusar
  • 759
  • 5
  • 21
-2

If you are looking to post JSON instead of a string, I would recommend using the $.ajax function so you can specify the type of data you want to send.

var data = {var: var};

$.ajax({
    type: "POST",
    dataType: "json",
    url: "other.jsp",
    data: data,
    success: function() {
        // on success
    },
    error: function() {
       // on error
    }
});
SkyOut
  • 390
  • 1
  • 10
  • How can I implement Json in jsp? I mean how did I put my rsvar.getString("name") into a json var? and retrieve it in the other jsp where I did the post? – zickno Jan 17 '14 at 00:15
  • The question, although slightly cryptic, is asking how you *respond* in JSON. As pointed in my comment attached to the question, there are tools available for JSP to encode JSON, such as [Google Gson](http://code.google.com/p/google-gson/) which is quite similar to PHP's json_encode – itsmejodie Jan 17 '14 at 00:21