[ Team LiB ] Previous Section Next Section

Summary

Servlets are the foundation for JavaServer Pages. Whereas JSPs are composed mostly of HTML, servlets are pure Java. They use the Java class libraries and other platform libraries to perform functions, such as writing output, that are related to working with HTTP requests and responses. They must be compiled and then deployed into a container. Usually, they are accompanied by a deployment descriptor that provides the container with information such as how the servlet can be accessed by a Web browser.

A servlet, as might be expected, implements the Servlet interface. Servlet has a few methods for initializing and deactivating a servlet, for obtaining information about the servlet or its environment, or for servicing requests. The service method is the workhorse method and is invoked each time a servlet is used. When writing a servlet, programmers can extend GenericServlet or HttpServlet to take advantage of built-in functionality such as managing the ServletConfig object or methods that make working with HTTP requests easier.

Deciding when to use JSPs or servlets can be made easier by determining whether you are working primarily on producing "template" output, such as an HTML page, or whether you need to be more programmatic. It's also valuable to try to reduce the mingling of presentation and low-level code as much as possible.

    [ Team LiB ] Previous Section Next Section