The Purpose and Use of Servlets

A servlet is a server-side component. It can be used to extend the functionality of any Java-enabled server, but most commonly servlets are used to write Web applications in a Web server, as shown in Figure 12.1. They are often used to create Web pages where the content is not static. Web pages whose content can change according to input from the user or other variable data are called dynamic pages. Servlets are particularly suited to creating dynamic Web pages.

Figure 12.1. Client/server diagram showing servlets.


The following are the key features and benefits of Java servlets:

  • The servlet API provides an interface that is tailored for Web applications.

  • Servlets are server and platform independent. This makes servlets portable and reusable.

  • Servlets are efficient and scalable.

  • Servlets run within the server, so they can delegate certain functions to be performed by the server on its behalf, such as user authentication.

Tailored for Web Applications

A servlet is an instance of a class that implements the javax.servlet.Servlet interface. However, most servlets extend one of the implementations of this interface—javax.servlet.GenericServlet or javax.servlet.http.HttpServlet.

The Servlet interface declares methods that manage the servlet and its communications with clients. As the servlet developer, you override some or all of these methods to develop your servlet.

Generic servlets have a limited use, so in today's lesson, we will only discuss the more useful HttpServlet class. This is an abstract class that is sub-classed to create an HTTP servlet suitable for a Web site. To accomplish this, an HTTP servlet has access to a library of HTTP-specific calls.

Server and Platform Independence

Java servlets are highly portable between different operating systems and server implementations. A servlet written on a Windows-based PC running the J2EE RI can be deployed on a high-end Unix server without any change at all. For this reason servlets have been described as “write once, serve everywhere.”

Servlets have no client interface. That means they avoid all the portability issues associated with different display interfaces. An application on the client (typically a browser) takes care of the user interface on behalf of the servlet.

Efficient and Scalable

After being loaded, a servlet will generally stay resident in the server's memory. In most circumstances, only a single servlet object will be created, and to support concurrent page accesses this servlet is run multi-threaded. This avoids the overhead of constructing a new servlet process for every access, saves memory, and makes page access efficient.

Because servlets stay in memory, they can retain references to other Java objects.

For example, if your database server includes sufficient simultaneous connection licenses, a database connection can be shared between threads, thereby reducing the overhead associated with establishing and maintaining the connection.

Caution

Multithreading aids efficiency and scalability, but the servlet code must be written to be re-entrant. This means that the servlet must handle concurrent access to instance data, and care must be taken to synchronize write access to shared resources.


Servlets Integration with the Server

Because a servlet is tightly integrated with the server, it can utilize capabilities of the server to perform certain actions. It can, for example, use the server's logging capabilities and get the server to authenticate users.

Caution

It may not be possible to take advantage of all server capabilities if you want your servlet to be portable to other platforms and environments.


Although useful for the servlet programmer, the tight coupling of servlets has a safety implication for the Web server. To protect itself, the server will often run servlets in a controlled environment, called a sandbox, that is designed to protect the server from a malicious or poorly written servlet.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.196.172