Return to Servlet index

What are Servlets ?


Servlets are java modules that run on Java enabled Web Servers and provide extra functionality to the server, similar to how applets extend the functionality of a browser. However, servlets do not require, and so do not have, a user interface.

A web browser allows the user to select pages to view - which the web server returns. The protocol for this request/response model is HTTP (HyperText Transfer Protocol). Browsers use HTML (HyperText Mark-up Language) to display formatted text and images - and clickable links to request new pages.

As HTML and HTTP have evolved, forms were created to allow the user to send more information to the server than just a page request. And small programs implementing CGI (the Common Gateway Interface) were developed to handle these requests and to select or construct the HTML based information to return.

Servlets are the Java equivalent to CGI programs. They provide a way to generate dynamic HTML documents that is both easier to write and faster to run. Servlets also address the problem of doing server-side programming with platform-specific APIs: they are developed with the Java Servlet API, a standard Java extension. So servlets are platform independent and will run on any Java-enabled web server.

Based on the user-provided information, a servlet can do some processing and construct a response. For example, a user might request the purchase of a specified number of a specified book; the servlet can check the book's availablity by JDBC calls to a database and send back an HTML form for the user to provide payment details. Through a series of such request-response interactions, complex tasks can be undertaken.

A servlet could perform many actions (functions), with one of the parameters specifying what action is required by a particular call. However, it is probably more efficient and easier to maintain if you have one servlet for one function and so have several servlets rather than one big one. However, you may choose to have one 'gateway' servlet that then calls other servlets in order to centralise user authentication, for example.

Servlets need not communicate in HTML, though this is the usual case, and often sufficiently powerful for most purposes. For example, servlets can communicate with applets and transfer byte streams, which could be java objects; XML is another possibility. Also, servlets can call other servlets, even on other servers, and interact with databses on the server side through JDBC. The possibilities are many and the potential is great.

We have seen that servlets are very useful. Servlets are also quite easy to program (if you are a Java programmer). It is possible to separate business logic from presentation with servlets by making use of templates, substitution and other techniques.

An alternative to servlets is 'Java Server Pages' (JSP), which is more easily used by presentation-focussed developers; Java programmers can work on the business logic separately in the form of custom tags and Java Beans. In either case, good separation of business logic and presentation is not inherent in the choice of approach. The use of JSP does not guarantee better separation of logic and presentation; although it does provide ready-made techniques to do so, this still requires a good design and careful implementation.

JSP pages are 'translated' to servlets by the JSP engine the first time that they are requested. So in a sense JSP is an alternative way to write servlets. JSP does have some advantages over writing servlets directly. One is that elements of the code - custom tags and Java beans - can be easily reused. However, well-written servlets can make use of specially written utility classes. Hence the issue of which is best - servlets or JSP - is not a clear-cut choice. Which approach is best depends on where your skills lie (and those of your team) and what you are trying to do.

 

Return to Servlet index