After the class declaration, add the method declaration (in black text below):
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletSample extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
The important parts of the above code snippet are:
The doGet() method
Classes that extend the HttpServlet class should override at least one method of the HttpServlet class. SimpleServlet overrides the doGet() method.
Note: Since doGet and doPost throw two exceptions (javax.servlet.ServletException and java.io.IOException), you must include them in the declaration.
HttpServletRequest and HttpServletResponse
When the server calls an HTTP servlet's service() method, it passes the following two objects as parameters:
HttpServletRequest: the request object
HttpServletRequest represents a client's request. This object gives a servlet access to incoming information such as HTML form data and HTTP request headers.
HttpServletResponse: the response object
HttpServletResponse represents the servlet's response. The servlet uses this object to return data to the client such as HTTP errors (200, 404, and others), response headers (Content-Type, Set-Cookie, and others), and output data by writing to the response's output stream or output writer.
The servlet communicates with the HTTP server and ultimately with the client through these objects. The servlet can invoke the HttpServletRequest object's (request) methods to get information about the client environment, the HTTP server environment, and any information provided by the client (for example, HTML form information set by GET or POST).
The servlet invokes the HttpResponse object's (response) methods to send the response that it has prepared back to the client. These same objects are also passed to the service() method.