GreetingServlet.java

 


/**
 * 
 * GreetingServlet.java 
 * 
 * This servlet responds to the GET method of an HTTP protocol
 * and writes a jovial response back to the client
 */

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

public class GreetingServlet extends HttpServlet 
{ 

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

      response.setContentType("text/html");
      response.setBufferSize(8192);

      PrintWriter out = response.getWriter();


      out.println("<html>" + "<head><title>Hello</title></head>");

      out.println("<body  bgcolor=\"#ffffff\">" 
                + "<img src=\"duke.waving.gif\">" 
                + "<h2>Open the pod bay doors, HAL.</h2>" 
                + "<form method=\"get\">" 
                + "<input type=\"text\" name=\"username\" size=\"25\">" 
                + "<p></p>" 
                + "<input type=\"submit\" value=\"Submit\">" 
                + "<input type=\"reset\" value=\"Reset\">" 
                + "</form>");

      String username = request.getParameter("username");

      if ( username != null && username.length() > 0 ) 
      {
            RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/response");
                                    
            if (dispatcher != null)
                dispatcher.include(request, response);

      }

      out.println("</body></html>");
      out.close();
    }

    public String getServletInfo() 
    {
        return "The Hello servlet says hello.";

    }
 }