Contents / Previous / Next


Servlets

A servlet can almost be thought of as an applet that runs on the server side -- without a face.
Servlets take over the functionality of CGI scripts.


Servlets versus CGI Scripts

What can be done with a servlet can also be done with a CGI script, but servlets have a distinct, important advantage--once a servlet is invoked, it is NOT necessary to start a new server process with each request since the servlet stays in memory.

CGI scripts, on the other hand, use heavyweight server processes for each request. Each time a CGI script is called, an entire operating system process is created.

Sites with heavy traffic loads have a noticeable performance improvement by using servlets instead of CGI scripts.


Implementing Servlets

The javax.servlet package and the javax.servlet.http package provide the classes and interfaces to define servlets.

HTML servlet classes extend the javax.servlet.http.HttpServlet abstract class, which provides a framework for handling HTTP protocol.


The Servlet's Service Methods

Because HttpServlet is abstract your servlet must extend it and override at least one of the following interaction methods: These methods are the heart of the servlet, where instructions and the purpose of the servlet are carried out.


Servlet Methods (Life Cycle)

init(): Called once by the server for initialization and not again for each user request.

service(): Next, the service method is called, performing the work of the servlet, and passing ServletRequest and ServletResponse objects as needed.

When a client calls a servlet by typing the URL in the browser, submitting a form, or clicking a button on a menu, the servlet's service method checks the HTTP request type, such as POST or GET. This in turn calls doGet, doPOST, doPUT, or doDelete as needed.

destroy(): Remove a previously loaded servlet instance.
This gives the servlet a chance to close database connections, save information to a log file, or perform other clean-up tasks before it is shut down.


Evaluation HTML Forms with Servlets

The following servlet example processes data POSTed by an HTML form: <html> <body> <h1>Send Form Parameter to Servlet</h1> <form method="GET" action="evalFormServlet"> <PRE> First Name: <input type="text" name="firstname"> Last Name: <input type="text" name="lastname"> <input type="submit"> <input type="reset"> </PRE> </body> </html> This is the source of the evalFormServlet that is called in the form: import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class evalFormServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<title>Eval Form Servelet</title>" + "<body>"); // Retrieve key-value pairs by key lookup. // String firstname = request.getParameter ( "firstname" ); String lastname = request.getParameter ( "lastname" ); out.println("<h2>Greetings</h2>"); out.println("<br>"); out.println("Hello " + firstname + " " + lastname + "."); out.println("<br>"); out.close(); } } The HttpServletRequest 'request' and HttpServletResponse 'resonse' arguments are the essence of the dynamic content generation.

All data you need is contained in the 'request' argument:

    String firstname = request.getParameter ( "firstname" );

Instead of writing output to standard output, output is sent back to the server, and then to the client browser by writing to the output stream received from the 'response' argument:

    PrintWriter out = response.getWriter();

Be sure to close the output stream before exiting to make sure the stream buffer is flushed:

   out.close();


Installing and Testing the Servlet

To test the servlet, you can set up the 'servletrunner' or install Tomcat on a local Apache server. You have to place the class file of your servlet in the servlet-directory of Apache ( usually /srv/www/servlets ).