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:

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:

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:

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:


If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.