Servlet development: a skeleton Servlet
This page shows sample Servlet code. Once you have set up your Servlet environment,
the first step is generally to write a test Servlet using code such as the skeleton shown in this example.
To write a basic Servlet, you generally:
- overwrite HttpServlet;
- overwrite the doGet() and doPut().
You Servlet therefore responds to two methods: doGet() and doPut(). These methods
will be called in response to GET and POST requests from the user's web browser.
Unless you specifically need your web application to respond differently to the two types of request,
one common pattern is to simply have one method call the other, and allow your web page to be served
in response to either type of request.
Basic Servlet code therefore looks as follows:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BasicServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
// ... output page to pw...
}
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
doGet(req, res);
}
}
So far, this example doesn't actually output any HTML, but it shows the
basic anatomy of a servlet class. We extend HttpServlet and must
provide implementations of the two methods. These methods correspond to
HTTP get and post methods respectively. As
discussed in the latter link, you can generally make doPost()
simply pass the request to doGet(). To the servlet, both types of
request essentially look the same. For example, parameters will be extracted
from either a URL or POSTed data.
HttpServletRequest and HttpServletResponse
Via these objects, you can find out about the user's request, and
write the response. The request object allows you to query things such as:
- parameters passed in the request;
- cookies, with the help of the Java Servlet API's Cookie object;
- request headers, which contain things such as
the referrer string, preferred language and character encoding,
and other information about the uesr's browser.
You can also query the request method, in case your program "forgets" whether
it was called via doGet() or doPost() and subsequently needs to know.
With the response object, you can:
- obtain the PrintWriter or OutputStream where the web page or data must be output;
- set the content type of the page or data being output, which
for a normal web page will be text/html as in the examples here;
- set the HTTP status code,
if it is something other than the default of "OK";
- set response headers such as the content length.
Outputting an HTML page
The simplist way to output a page of HTML is using the PrintWriter as
follows:
private String message =
"Welcome to the Orthogonal Terwilliger Accordion Association!";
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
pw.println("<html>");
pw.println("<body>");
pw.println("<p>" + msg + "</p>");
pw.println("</body>");
pw.println("</html>");
}
Obviously, in practice we would want to send a <head> section just like
any other web page.
And for very basic web pages, that's it!
What to read next
For non-trivial Servlets, you will generally want to do at least some of the following: