[Java Servlet]Hello Worldのサンプル

書式
public void doGet(HttpServletRequest request,
HttpServletResponse response)
使用例

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

//  HttpServlet継承
public class HelloWorld extends HttpServlet {
 
  private String message;

  public void init() throws ServletException
  {
      // 初期化
      message = "Hello World";
  }

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // コンテンツタイプ
      response.setContentType("text/html");

      // メッセージ出力
      PrintWriter out = response.getWriter();
      out.println("<h1>" + message + "</h1>");
  }
  
  public void destroy()
  {
      //do nothing
  }
}

 

Servlet

Posted by arkgame