For brevity and clarity, many of our code samples contain code snippets for Java Extensions rather than entire Java Extension classes.
A Java Extension is a class derived from the GenericServlet or HttpServlet class. The class should override either the service() method (GenericServlets) or the doGet() and doPost() methods (HttpServlets). It may also override the init() method.
A Java Extension source file will contain a full declaration for a Java Extension class; for example:
Generic Servlets
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class Example extends GenericServlet {
private static final long serialVersionUID = 1L;
public void service( ServletRequest req, ServletResponse res )
throws IOException
{
// Do stuff...
}
}
HTTP Servlets
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Example extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{
// Do stuff...
}
public void doPost( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{
// Do stuff...
}
}
Often, an HTTP servlet will handle GETs and POSTs in an identical fashion, so the doPost() method simply calls the doGet() as follows:
public void doPost( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException
{
doGet( req, res );
}
Code Samples
Many of the code samples you will see here just provide the implementations for the service() or doGet() methods, and any anciliary methods required. You'll need to insert this code into the full class code (copy and paste from this page) and add the necessary import statements in order to create the full source file.
Most IDEs will help you insert the correct imports - Ctrl-Shift-O in Eclipse will do the trick for example.
References