Professional Documents
Culture Documents
Listing 2.1
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletTemplate extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException{
PrintWriter out = response.getWriter(); // use 'out' to send content to
the browser.
}
For simple servlets, most of the effort is spent on the (writer.)println statements
that generate the desired page.
doGet ad doPost throw two exceptions so you have to include them in the
declaration.
you import java.io (for PrintWriter etc), javax.Servlet (for HttpServlet etc) and
javax.servlet.http for HttpServletRequest, HttpServletResponse etc
Strictly speaking HttpServlet is not the only starting point for servlets.
Servlets could, in principle, extend mail, FTP (etc) servers, Servlets for these
environments would extend GenericServlet, the parent class of HttpServlet. In
practice though, servlets are used exclusively for servers that communicate with
Http (web and app servers) and in this book, we focus on these.
import java.io.*;
import javax.servlet.*;
import javax.servlet.htpp*;