Hello Servlet

  • Extend HttpServelet and override doGet().
  • Get request information from the HttpServletRequest parameter.
  • Compose a response using the HttpServletResponse parameter.
Interface
javax.servlet.Servlet
+ init (ServletConfig)
+ destroy ()
+ service (ServletRequest, ServletResponse)
+ getServletConfig () : ServletConfig
+ getServletInfo () : String
javax.servlet.http.HttpServlet
+ HttpServlet()
+ service (ServletRequest, ServletResponse)
# service (HttpServletRequest, HttpServletResponse)
# doGet (HttpServletRequest, HttpServletResponse)
# doPost (HttpServletRequest, HttpServletResponse)
... more
# getLastModified (HttpServletRequest) : long
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Hello extends HttpServlet {

    public void doGet( HttpServletRequest  request, 
                       HttpServletResponse response) 
             throws ServletException, IOException {

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello Yourself</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello Servlet says Hello to " 
                    + request.getRemoteAddr() + "!</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}