ServletSample.java
// ServletSample.java
// Step 1: Add the necessary import statements.
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Step 2: Extend HttpServlet.
public class ServletSample extends HttpServlet
{
// Step 3: Specify the required methods.
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// Step 4: Get the HTTP request information, if any.
Enumeration keys;
String key;
String myName = "";
keys = request.getParameterNames();
while (keys.hasMoreElements())
{
key = (String) keys.nextElement();
if (key.equalsIgnoreCase("myName")) myName = request.getParameter(key);
}
System.out.println("Name = ");
if (myName == "") myName = "Hello";
// Step 5: Create the HTTP response.
response.setContentType("text/html");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Just a basic servlet</title></head>");
out.println("<body>");
out.println("<h1>Just a basic servlet</h1>");
out.println ("<p>" + myName + ", this is a very basic servlet.");
out.println("</body></html>");
out.flush();
}
}