http url 接続を介して、あるサーブレットから別のサーブレットに文字列を送信しています。
final HttpURLConnection http = (HttpURLConnection) url.openConnection(); // here url is the url of the second servlet
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setDoInput(true);
http.setUseCaches(false);
final OutputStream outstr = http.getOutputStream();
outstr.write(sb.toString().getBytes());
outstr.flush();
outstr.close();
私が直面している問題は、これを別のサーブレットからのリクエストとして読み取ることです。2 番目のサーブレットの getPost メソッドに次のコードを記述しようとしましたが、うまくいきません。
try {
int len = req.getContentLength();
byte[] input = new byte[len];
ServletInputStream sin = req.getInputStream();
int c, count = 0;
while ((c = sin.read(input, count, input.length - count)) != -1) {
count += c;
}
sin.close();
String inString = new String(input);
String decodedString = URLDecoder.decode(inString, "UTF-8");
log.info("Response received - ");
log.info(decodedString);
resp.getWriter().write(decodedString);
resp.getWriter().flush();
resp.getWriter().close();
} catch (IOException e) {
}
最初のサーブレットから送信された文字列を取得して表示する正しい方法を教えてください。