javascript - how to decode json data in java (how to fetch json data from localhost and print its values) -


i trying perform simple json encoding , decoding example java.

in example sending signup page details javascript. in javascript encoding values in json format , sending them servlet.

i don't know i'm going wrong i'm unable decode (parse) , print data @ servlet end.

i'm new json , java , want first print values json array in servelet can later put them in database.

/*this javascript code*/    function signup()  {     var request = createcorsrequest( "get", "http://localhost:8080/jsondem/pass" );    /*pass servlet*/      var name = document.getelementbyid('name').value;      var mobileno = document.getelementbyid('mobileno').value;      var emailid = document.getelementbyid('emailid').value;      var password = document.getelementbyid('password').value;      alert(name);      alert(mobileno);      alert(emailid);      alert(password);    /*i m printing values cross checking*/        var data ={"name":name,"password":password,"mobileno":mobileno,"emailid":emailid};        	alert(json.stringify(data));  	var senddata = function(data){     	alert(json.stringify(data));        $.ajax({       url:'http://localhost:8080/jsondem/pass',       type: 'get',       contenttype: 'application/json',      data: json.stringify(data),  	success: function(response)  	{              alert(response);  	},          error: function(response)          {            alert('error'+response);          }  });  };  senddata(data);  }

following servlet want take json data uploaded on localhost servlet , want print it

/*this servlet's doget method*/ protected void doget(httpservletrequest request, httpservletresponse response)             throws servletexception, ioexception {          new javahttpurlconnectionreader();    }  /*i add content in servlet have done in separate class  javahttpurlconnectionreader*/  import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; import java.net.urlencoder; import org.json.simple.jsonobject; import org.json.simple.parser.jsonparser;   class javahttpurlconnectionreader {       public javahttpurlconnectionreader()   {     try     {       string myurl = "http://localhost:8080/jsondem/pass";       // if url can contain weird characters want        // encode here, this:       myurl = urlencoder.encode(myurl, "utf-8");       dohttpurlconnectionaction(myurl);      }     catch (exception e)     {       system.out.println(e);     }   }   /*i m doing calling method dohttpurlconnectionaction*/      private void dohttpurlconnectionaction(string myurl) throws exception {          url url = null;     bufferedreader reader = null;     stringbuilder stringbuilder;  jsonparser jsonparser = new jsonparser();      try     {       // create httpurlconnection       url = new url(myurl);       httpurlconnection connection = (httpurlconnection) url.openconnection();        // want http here       connection.setrequestmethod("get");        // uncomment if want write output url       //connection.setdooutput(true);        // give 15 seconds respond       connection.setreadtimeout(15*1000);       connection.connect();        // read output server         reader = new bufferedreader(new inputstreamreader(connection.getinputstream()));        jsonobject jsonobject = (jsonobject) jsonparser.parse(reader);        string name = (string) jsonobject.get("name");       system.out.println("name: " + name);        long mobileno = (long) jsonobject.get("mobileno");              system.out.println("mobile number: " + mobileno);        string emailid = (string) jsonobject.get("emailid");       system.out.println("email id: " + emailid);         string password = (string) jsonobject.get("password");       system.out.println("password: " + password);      }     catch (exception e)     {       e.printstacktrace();       throw e;     }          {        if (reader != null)       {         try         {           reader.close();         }         catch (ioexception ioe)         {           ioe.printstacktrace();         }       }     }     } } 

i getting following output. can help? enter image description here

your javascript submitting code server. on server side (in servlet), need read data in doget method. wouldn't "connect" server again. here example of doget method implementation:

protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {      string name = request.getparameter("name");     string password = request.getparameter("password");     string mobilenumber = request.getparameter("mobileno");     //and on... } 

additionally, you're sending data server, you'd rather use post http method and, in servlet, implement dopost instead of doget.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -