{"id":453,"date":"2011-03-03T12:42:02","date_gmt":"2011-03-03T04:42:02","guid":{"rendered":"http:\/\/www.clonefactor.com\/wordpress\/?p=453"},"modified":"2013-08-17T23:17:34","modified_gmt":"2013-08-17T15:17:34","slug":"%e5%be%9e-java-%e6%8e%a5%e6%94%b6-javascript-submit-%e7%9a%84-json","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/webdevelop\/453\/","title":{"rendered":"\u5f9e JAVA \u63a5\u6536 Javascript submit \u7684 JSON"},"content":{"rendered":"<p>\u5176\u5be6\u5728\u00a0<a href=\"http:\/\/www.json.org\/\">http:\/\/www.json.org\/<\/a> \u4e2d\u6709\u8a73\u7d30\u7684\u8aaa\u660e\u5404\u7a2e\u8a9e\u8a00\u9593\u4ea4\u63db\u7684\u6a19\u6e96.<\/p>\n<p>\u6b64\u6587\u4e3b\u8981\u662f\u7d00\u9304\u600e\u6a23\u5f9e\u4f3a\u670d\u63a5\u53d7 Client \u7aef upload \u7684 JSON \u5728\u4e0d\u4f7f\u7528 Request \u7684\u60c5\u6cc1\u4e0b.<\/p>\n<p>\u9019\u88e1\u4f7f\u7528 Js \u7aef\u7684 lib\u00a0[superbutton link=&#8221;https:\/\/github.com\/douglascrockford\/JSON-js&#8221; title=&#8221;&#8221; image=&#8221;&#8221; class=&#8221;sprbtn_orange&#8221; target=&#8221;_blank&#8221; rel=&#8221;&#8221;]JSON.js[\/superbutton]<\/p>\n<p>\u4ee5\u53ca Java \u7aef\u7684 lib\u00a0[superbutton link=&#8221;http:\/\/www.json.org\/java\/index.html&#8221; title=&#8221;JAVAObject&#8221; image=&#8221;&#8221; class=&#8221;sprbtn_yellow&#8221; target=&#8221;&#8221; rel=&#8221;&#8221;]JAVAObject.java[\/superbutton]<\/p>\n<p>[js]<br \/>\nvar xmlHttp;<br \/>\nfunction createXMLHttpRequest() {<br \/>\n\tif (window.ActiveXObject) {<br \/>\n\t\txmlHttp = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br \/>\n\t} else if (window.XMLHttpRequest) {<br \/>\n\t\txmlHttp = new XMLHttpRequest();<br \/>\n\t}<br \/>\n}<br \/>\nfunction doJSON() {<br \/>\n\tvar car = getCarObject();<br \/>\n\t\/\/ Use the JSON JavaScript library to stringify the Car object<br \/>\n\tvar carAsJSON = JSON.stringify(car);<br \/>\n\talert(&quot; Car object as JSON:n &quot; + carAsJSON);<br \/>\n\tvar url = &quot; JSONExample?timeStamp= &quot; + new Date().getTime();<br \/>\n\tcreateXMLHttpRequest();<br \/>\n\txmlHttp.open(&quot;POST&quot;, url, true);<br \/>\n\txmlHttp.onreadystatechange = handleStateChange;<br \/>\n\txmlHttp.setRequestHeader(&quot;Content-Type&quot;,&quot;application\/x-www-form-urlencoded&quot;);<br \/>\n\txmlHttp.send(carAsJSON);<br \/>\n}<br \/>\nfunction handleStateChange() {<br \/>\n\tif (xmlHttp.readyState == 4) {<br \/>\n\t\tif (xmlHttp.status == 200) {<br \/>\n\t\t\tparseResults();<br \/>\n\t\t}<br \/>\n\t}<br \/>\n}<br \/>\nfunction parseResults() {<br \/>\n\tvar responseDiv = document.getElementById(&quot;serverResponse&quot;);<br \/>\n\tif (responseDiv.hasChildNodes()) {<br \/>\n\t\tresponseDiv.removeChild(responseDiv.childNodes[0]);<br \/>\n\t}<br \/>\n\tvar responseText = document.createTextNode(xmlHttp.responseText);<br \/>\n\tresponseDiv.appendChild(responseText);<br \/>\n}<br \/>\nfunction getCarObject() {<br \/>\n\treturn new Car(&quot;Dodge&quot;, &quot;Coronet R\/T&quot;, 1968, &quot;yellow&quot;);<br \/>\n}<br \/>\nfunction Car(make, model, year, color) {<br \/>\n\tthis.make = make;<br \/>\n\tthis.model = model;<br \/>\n\tthis.year = year;<br \/>\n\tthis.color = color;<br \/>\n}<br \/>\n[\/js]<\/p>\n<p>\u4ee5\u4e0a\u53ef\u898b JSON \u5176\u5be6\u4e26\u4e0d\u9700\u8981\u5f9e\u6b63\u5e38\u7684 POST\/GET \u65b9\u5f0f\u4e0a\u50b3\u5230\u4f3a\u670d, \u53ea\u9700\u8981\u4f7f\u7528 XMLHTTPRequest.send(carAsJSON); \u76f4\u63a5\u4e1f\u4e0a server \u5373\u53ef, \u65bc JAVA \u7684 Lib\u00a0[superbutton link=&#8221;http:\/\/www.json.org\/java\/index.html&#8221; title=&#8221;JAVAObject&#8221; image=&#8221;&#8221; target=&#8221;&#8221; rel=&#8221;&#8221;]JAVAObject.java[\/superbutton] \u5247\u6703\u76f4\u63a5\u5f9e http header \u4e2d\u8b80\u53d6 JSON \u5b57\u4e32, \u518d\u9084\u539f\u70ba\u53ef\u8b80\u53d6\u7684 Object \u7269\u4ef6<\/p>\n<p><span style=\"font-family: Consolas, Monaco, 'Courier New', Courier, monospace; font-size: 12px; line-height: 18px; white-space: pre;\"> <\/span><\/p>\n<p>[java]<br \/>\npackage ajaxbook.chap3;<\/p>\n<p>import java.io.*;<br \/>\nimport java.net.*;<br \/>\nimport java.text.ParseException;<br \/>\nimport javax.servlet.*;<br \/>\nimport javax.servlet.http.*;<br \/>\nimport org.json.JSONObject; \/\/ JAVAObject.java Library<\/p>\n<p>public class JSONExample extends HttpServlet<br \/>\n{<br \/>\n\tprotected void doPost(HttpServletRequest request,<br \/>\n\t\t\tHttpServletResponse response) throws ServletException, IOException<br \/>\n\t{<br \/>\n\t\tString json = readJSONStringFromRequestBody(request);<br \/>\n\t\t\/\/ Use the JSON-Java binding library to create a JSON object in Java<br \/>\n\t\tJSONObject jsonObject = null;<br \/>\n\t\ttry<br \/>\n\t\t{<br \/>\n\t\t\tjsonObject = new JSONObject(json);<br \/>\n\t\t} catch (ParseException pe)<br \/>\n\t\t{<br \/>\n\t\t\tSystem.out.println(&quot; ParseException: &quot; + pe.toString());<br \/>\n\t\t}<\/p>\n<p>\t\tString responseText = &quot; You have a &quot; + jsonObject.getInt(&quot; year &quot;)<br \/>\n\t\t\t\t+ &quot; &quot; + jsonObject.getString(&quot; make &quot;) + &quot; &quot;<br \/>\n\t\t\t\t+ jsonObject.getString(&quot; model &quot;) + &quot; &quot; + &quot; that is &quot;<br \/>\n\t\t\t\t+ jsonObject.getString(&quot; color &quot;) + &quot; in color. &quot;;<\/p>\n<p>\t\tresponse.setContentType(&quot; text\/xml &quot;);<br \/>\n\t\tresponse.getWriter().print(responseText);<br \/>\n\t}<\/p>\n<p>\tprivate String readJSONStringFromRequestBody(HttpServletRequest request)<br \/>\n\t{<br \/>\n\t\tStringBuffer json = new StringBuffer();<br \/>\n\t\tString line = null;<br \/>\n\t\ttry<br \/>\n\t\t{<br \/>\n\t\t\tBufferedReader reader = request.getReader();<br \/>\n\t\t\twhile ((line = reader.readLine()) != null)<br \/>\n\t\t\t{<br \/>\n\t\t\t\tjson.append(line);<br \/>\n\t\t\t}<br \/>\n\t\t} catch (Exception e)<br \/>\n\t\t{<br \/>\n\t\t\tSystem.out.println(&quot; Error reading JSON string: &quot; + e.toString());<br \/>\n\t\t}<br \/>\n\t\treturn json.toString();<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>\u5c31\u500b\u4eba\u800c\u8a00, \u574a\u9593\u4f7f\u7528\u7684 Action?MyVar={&#8220;abc&#8221;:&#8221;xxxxx&#8221;} \u7b49\u4e0a\u50b3\u65b9\u5f0f\u9700\u7136\u53ef\u884c,<br \/>\n\u4f46\u7d42\u65bc\u9700\u8981\u984d\u5916\u8a02\u7acb\u4e00\u500b MyVar \u65bc\u5408\u4f5cProject\u4e2d\u4e0d\u5efa\u8b70\u4f7f\u7528.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5176\u5be6\u5728\u00a0http:\/\/www.json.org\/ \u4e2d\u6709\u8a73\u7d30\u7684\u8aaa\u660e\u5404\u7a2e\u8a9e\u8a00\u9593\u4ea4\u63db\u7684\u6a19\u6e96. \u6b64\u6587\u4e3b\u8981\u662f\u7d00 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[26,27,31,47],"class_list":["post-453","post","type-post","status-publish","format-standard","hentry","category-webdevelop","tag-java","tag-javascript","tag-json","tag-xmlhttprequest"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/453","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=453"}],"version-history":[{"count":0,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/453\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}