java - send response to local file -
i've got simple html
page 1 button
<html> <head> <script> function sender() { var oreq = new xmlhttprequest(); oreq.open('get', 'http://localhost:8888?param2=value2', true); oreq.onreadystatechange = function() { if (oreq.readystate == 4) { if (oreq.status == 200) { alert(xmlhttp.responsetext); } if (oreq.status == 404) { alert("404040404!"); } } }; oreq.send(); } </script> </head> <body> <button id="click" onclick="sender();">send</button> </body> </html>
and i've got simple http server written on java nothing except reading incoming requests , showing console.
but how send response file server? example file located there
file:///c:/users/admin/desktop/development/test.html
and request looks this
get /?param2=value2 http/1.1 host: localhost:8888 user-agent: mozilla/5.0 (windows nt 6.1; rv:28.0) gecko/20100101 firefox/28.0 accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 accept-language: en,ru-ru;q=0.8,ru;q=0.5,en-us;q=0.3 accept-encoding: gzip, deflate origin: null connection: keep-alive header parsing result: param2=value2
where , how send response?
server reading header this
bufferedreader reader = new bufferedreader(new inputstreamreader( sock.getinputstream())); stringbuilder sb = new stringbuilder(); string line = null; while (true) { line = reader.readline(); if (line == null || line.isempty()) break; sb.append(line + system.getproperty("line.separator")); } return sb.tostring();
well, you're getting response, , if request ok response displayed in javascript alert. manipulation need replace alert actual display in html page.
oreq.onreadystatechange = function() { if (oreq.readystate == 4) { if (oreq.status == 200) { //this should put code (or function call) when went ok } if (oreq.status == 404) { //this when got 404 error } } };
if wish save file on client machine, should send filestream server-side code can saved client (like file download), far know, can't in full ajax , need plain http request it. please note miss big part of error handling in ajax code... there many ways request fail other 404 error, , you're 1 setting 404 error maybe least probable happen.
Comments
Post a Comment