i have .jsp page handles basic html (text field, submit button table populate) , javascript. on click calls java servlet class handles request , response , jsp respectfully. have java class handle url connection make rest call using should return json string result. scenario: user wishes make simple search entering id value populate table using rest connect url , json response.
i have separately tested java class make rest call made url connection basic authentication data in json string , works fine. (i have coded 4 different methods of doing , work fine , return expected json).
i have separately tested jsp page making call servlet populate "dummy" values in table , servlet response fine , table populates expected.
the problem:
when tried populate table values obtained rest call, hangs , no result. tried investigate why , found out crazy reason, servlet doesn't line of code sets header basic authentication access url. tried commenting basic auth 1 line code out (to debug, have no authentication) passing through "dummy" hard coded data, , populates table.
i don't know why servlet doesn't when set authentication. guess overwriting servlet's response path , therefore lost initial response location jsp? (i.e. send json to?) can me here? know whats happening? assumption of problem correct? if so, how overcome problem?
javascript call servlet:
$("#myform2").submit(function(e) { $.ajax({ type: "get", url: "servlet1", // script form input handled. data: $("#myform2").serialize(), // serializes form's elements. success: function(responsedata){ var dataobj = $.parsejson(responsedata) $('#mytable').append(>>add values table here<<); }); e.preventdefault(); // avoid execute actual submit of form. $("#myform2")[0].reset(); // clear previous values entered });
servlet class, request made jsp page , servlet's response:
rest getcallonurl = new rest(); // instance of rest class public void doget(httpservletrequest request, httpservletresponse response) throws servletexception,ioexception { // create dummy string "test" hold id value passed in javascript string testid = request.getparameter("id"); // hard coded - testing purposes if (testid.equals("1")){ //call java class' method connect url , retrieve json string getcallonurl.connecttourlandmakeagetrequest(); valuetopopulatetable = getcallonurl.testjsonstring; } response.setcontenttype(content_type); printwriter out = response.getwriter(); // using backslash escape quotes make valid json response out.print("{\"testrow\": \"valuetopopulatetable\"}"); out.close(); }
my java class method make url call , basic authentication:
string testjsonstring = "defaultvalue";//testing purposes //creates valid encoded authentication string private string basicauth(string name, string pass){ string authstring = name + ":" + pass; byte[] authencbytes = base64.encodebase64(authstring.getbytes()); string authstringenc = new string(authencbytes); return authstringenc; } private httpurlconnection preparegetconnection(final url verifierurl) throws ioexception { final httpurlconnection connection = (httpurlconnection) verifierurl.openconnection(); connection.setrequestmethod("get"); //this line authorizes access url via basic auth connection.setrequestproperty("authorization", "basic " + basicauth(name, password)); return connection; } public void connecttourlandmakeagetrequest(){ try { string webpage = "url here";// pass valid rest url endpoints url url = new url(webpage); httpurlconnection conn = preparegetconnection(url); // url response json string inputstream geturlresponse = conn.getinputstream(); bufferedreader rd = new bufferedreader(new inputstreamreader(geturlresponse)); //get first line of string - testing purposes only! testjsonstring = rd.readline(); rd.close(); conn.disconnect(); } catch(ioexception e){ system.out.println("io exception: "+e); } }