GET and POST Test Servlet - 2.0 kg/May/2012
Submits  either a Post or Get query to a servlet running on an external web site.
The servlet displays the entered text plus some other info the servlet has access to
including this machines IP address.
For the GET query notice your text ends up in the URL after the "?" and also
the Query String parameter is shown by the servlet.

The compiled testServlet.class file is typically placed in this example: cit.karlgrabe.com/WEB-INF/classes/testServlet.class and accessed by http://cit.karlgrabe.com/servlet/testServlet

Enter a Message

Enter a Message



Here is the HTML code for the 2 forms above:
<form action="http://cit.karlgrabe.com/servlet/testServlet" method="post">
  <p>Enter a Message <input name="karlMessage"
 id="firstTF" value="" maxlength="35" type="text">
  <input value="do a POST" name="the Post Button"
 type="submit"></p>
</form>

<form action="http://cit.karlgrabe.com/servlet/testServlet"
 method="get">
  <p>Enter a Message <input name="karlMessage"
 id="secondTF" value="" maxlength="35" type="text">
  <input value="do a GET" name="the GET Button"
 type="submit"></p>
</form>



The servlet source code:
/*
 * testServlet 2.0
 *
 */

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

/**
 *
 * @author karl.grabe 18-Feb-09
 */
public class testServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    private int counter = 0;
    Date startDate = new Date(System.currentTimeMillis());
    Date requestDate = null;
    private String message = "no message yet";

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head><title>testServlet</title></head>");
        out.println("<body bgcolor=\"#00ffff\">");
        out.println("<p> testServlet v2.0a");
        out.println("<BR/>");
        out.println("Started: " + startDate);
        out.println("<BR/>");

        out.println("Received a " + request.getMethod() + ".");  // doGet, doPost etc
        out.println("<BR/>");
        out.println("getRemoteAddr: " + request.getRemoteAddr());
        out.println("<BR/>");
        out.println("getRemoteHost: " + request.getRemoteHost());
        out.println("<BR/>");
        out.println("Query String: " + request.getQueryString()); // i.e. ?message test in query of doGet
        out.println("<BR/>");
        if (request.getParameter("karlMessage") != null) {
            message = request.getParameter("karlMessage");
            requestDate = new Date(System.currentTimeMillis());
        }
        out.println("Message is: " + message);
        out.println("<BR/>");
        out.println("Updated: " + requestDate);
        out.println("<BR/>");
        out.println(("page counter is: " + counter++));
        out.println("<BR/>");
        out.println("<a href=\"/testServlet/testServlet\">Reload</a>  ");
        //out.println("<a href=\"http://localhost:8084/testServlet/testServlet\">Reload</a>  ");
        //out.println("<a href=\"http://localhost:8084/testServlet\">Input" + "</a>");
        out.println("<a href=\"/testServlet\">Input" + "</a>");
        out.println("<a href=\"/\">Web Server Root" + "</a>");
        out.println("</p>");
        out.println("</body>");
        out.println("</html>");
        out.close();

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
}