Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to through a java application to transfer data to the servlet
    primarykey
    data
    text
    <p>I want to pass a java application sent a string that is a json,it isn't get this json in servlet, how do I do, who can help me? This is my servlet code:</p> <pre><code>public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { BufferedInputStream in = new BufferedInputStream(req.getInputStream()); byte[] data = null; byte[] bts = new byte[1024]; int index; while ((index = in.read(bts)) &gt;= 0) { if (data == null) { data = new byte[index]; System.arraycopy(bts, 0, data, 0, index); } else { byte[] tmp = data; data = new byte[tmp.length + index]; System.arraycopy(tmp, 0, data, 0, tmp.length); System.arraycopy(bts, 0, data, tmp.length, index); } } String json = new String(data); System.out.print(json); } </code></pre> <p>and this is my java application:</p> <pre><code>String _url = "http://localhost:8080/jsf/test"; URL url = null; HttpURLConnection urlconn = null; String json = URLEncoder.encode(JSONObject.fromObject(req) .toString(), "UTF-8"); url = new URL(_url); urlconn = (HttpURLConnection) url.openConnection(); urlconn.setRequestMethod("POST"); urlconn.setDoOutput(true); urlconn.setDoInput(true); OutputStream out = urlconn.getOutputStream(); out.write(json.getBytes("UTF-8")); out.flush(); out.close(); BufferedReader rd = new BufferedReader(new InputStreamReader( urlconn.getInputStream(), "UTF-8")); StringBuffer sb = new StringBuffer(); int ch; while ((ch = rd.read()) &gt; -1) { sb.append((char) ch); } System.out.println(sb); rd.close(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload