15
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

サーブレットでリクエストボディを取得する

Posted at

リクエスト本文(リクエストボディ)を取得するメソッドって特に提供はされていないと思いつつ、Java8ならば少し簡単に記述できます。

BodySampleServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String result = request.getReader().lines().collect(Collectors.joining("\r\n"));
}

1行でまとめているところ分割すると、次のように。

BodySampleServlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	BufferedReader reader = request.getReader();
	Stream<String> lines = reader.lines();
	String result = lines.collect(Collectors.joining("\r\n"));
}
15
13
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?