5
4

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.

JAVA サーブレットクラスでHTMLを出力

Last updated at Posted at 2016-02-05

サーブレットクラスでHTMLを出力

response.setContentType("text/html; charset=HTMLの文字コード");
PrintWriter out = response.getWriter();
out.println("…");

<重要>
※「java.io.PrintWriter」をインポートする必要がある。
※「response」はHttpServletResponseのこと。

▪️Test06Servlet.java

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Test06Servlet")
public class Test06Servlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter testPrint = response.getWriter();
		testPrint.println("<html>");
		testPrint.println("ServletでHTMLを出力テスト");
		testPrint.println("</html>");
	}

}

▪️Test06Servlet.java 実行結果
スクリーンショット 2016-02-10 18.18.20.png

5
4
0

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
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?