To understand the advantages of servlets, yon must have a basic understanding of how Web browsers and servers cooperate to provide content to a user. Consider a request for a static Web page. A user enters a Uniform Resource Locator (URL) to a browser. The browser generates an HTTP request to the appropriate Web server. The Web server-maps this request to n specific file. That file is returned in an HTTp~ response to the browser.The HTTP header in the response indicates the type of the content. The Multipurpose Internet Mr\il Extensions (MIME) are used for this purpose. For.example, ordinary ASCII text has a MIME type of text Lapland, The Hypertext Markup Language (HTML) source code of a-Web par;e has n MIME type of text/html. Now consider dynamic content. Assume that an online bookstore uses :1 database to store information about its business, including book prices, availability, orders, and so forth. It wants to mnkc this information accessible to customers via web.pages.The contents of those Web pa~es must be dynamically Geller to reflect thl! latest information in’ the database. In the early days of the ‘INch, it server could dynamically construct a page by creating a separate process !o handle each client request. The “focus;, would open connections tune or more databases in order to obtain the necessary information. communicated with the Vcb server via an interface known as the Common Gateway Interface (eGl). CGI allowed the separate process to rend data from the request and write data to the HTTP response. A variety of different languages were used to build CGI programs, including C, C++. However, eGl suffered serious performance problems. Creating separate process For each client request Vl;;S expensive in terms of processor and memory resources. It was also expensive to open and close database connections for each client request. In addition, the CGI programs were not platform-in cpcn dcnt, Therefore.hor techniques were introduced, including servlets. Servlets offer several advantages over eel m Performance is significantly better. Servlets execute within chandross ccof a Wcb !;(‘rvcr. Creating a separate process to handle Servlets are platform-Independent, because they arc written in [nva. Several Web from vendors such Sun, Servlet API. Programs developed f(l~ the<;APr can b~ moved to an)’ of these environments without recompilation.
The Java Security Manager on the server enforces a set of restrictions to protect the resources on a server machine. You will see that some servlets are trusted and others arc untrusted fill The full functionality of java class libraries is available lo a servlet, It can communicate with applets, databases, or other software via the sockets and RMI mechanisms that you have seen already.