Introduction to Servlets

 

Servlets are Java technology’s answer to Common Gateway Interface (CGI).They are programs that run on a Web server, acting as a middle layer between a request coming from a Web browser or other HTTP client and databases or applications on the HTTP server. Servlets are used to do:

 

Ø  Read any data sent by the user.

            This data is usually entered in a form on a Web page, but could also come from a Java          applet or a custom HTTP client program.

Ø  To See any other information about the request that is embedded in the HTTP request.

            This information includes details about browser capabilities, cookies, the host name of          the requesting client, and so forth.

Ø  Generate the results.

            This process may require talking to a database, executing an RMI or CORBA call,      invoking a legacy application, or computing the response directly.

Ø  Format the results inside a document.

      In most cases, this involves embedding the information inside an HTML page.

Ø  Set the appropriate HTTP response parameters.

      This means telling the browser what type of document is being returned (e.g., HTML),         setting cookies and caching parameters, and other such tasks.

Ø  Send the document back to the client.

            This document may be sent in text format (HTML), binary format (GIF images), or even        in a compressed format like gzip that is layered on top of some other underlying format.

 

servlets are not restricted to Web or application servers that handle HTTP requests, but can be used for other types of servers as well. For example, servlets could be embedded in mail or FTP servers to extend their functionality.

 

The Advantages of Servlets

Java servlets are more efficient, easier to use, more powerful, more portable, safer, and cheaper than traditional CGI and many alternative CGI-like technologies. Following are the advantages

 

Efficient

With CGI, a new process is started for each HTTP request. So the overhead of starting the  process can dominate the execution time. In  servlets, the Java Virtual Machine stays

running and handles each request using a lightweight Java thread, not a heavyweight operating system process. if there are N simultaneous requests to the same CGI program, the code for the CGI program is loaded into memory N times. With servlets, however, there would be N threads but only a single copy of the servlet class.

 

Convenient

Servlets have an extensive infrastructure for automatically parsing and decoding HTML form data, reading and setting HTTP headers, handling cookies, tracking sessions, and many other such high-level utilities.

 

Powerful

Servlets support several capabilities that are difficult or impossible to accomplish with regular CGI. Servlets can talk directly to the Web server, where as regular CGI programs cannot, at least not without using a server-specific API. Communicating with the Web server makes it easier to translate relative

URLs into concrete path names, for instance. Multiple servlets can also share data, making it easy to implement database connection pooling and similar resource-sharing optimizations. Servlets can also maintain information from request to request, simplifying techniques like session tracking and caching

of previous computations.

 

 

Portable

Servlets are written in the Java programming language and follow a standard API. Consequently, servlets written for, say, I-Planet Enterprise Server can run virtually unchanged on Apache, Microsoft Internet Information Server (IIS), IBM WebSphere, or StarNine WebStar. In fact, servlets are supported directly or by a plug-in on virtually every major Web server. They are now part of the Java 2 Platform, Enterprise Edition , so industry support for servlets is becoming even more pervasive.

 

Secure

As servlets are written in java, servlets inherit the strong type safety of java language. Java's automatic garbage collection and a lack of pointers means that servlets are generally safe from memory management problems. In servlets we can easily handle the errors due to  Java's exception handling mechanism. If any exception occurs then it will throw an exception.

 

Inexpensive

There are a number of free or very inexpensive Web servers available that are good for “personal” use or low-volume Web sites. However, with the major exception of Apache, which is free, most commercial-quality Web servers are relatively expensive. Nevertheless, once we have a Web server, we can  add servlet support to it. This is in contrast to many of the other CGI alternatives, which require a significant initial investment to purchase a proprietary package.

 


Enter your email address:

Delivered by FeedBurner



Sandeep Joshi
Mathematics, Technology and Programming are my passion. I am a part of Java Ecosystem and through this blog, I contribute to it. I am here to blog about my interests, views and experiences.
I am on Google+ and Facebook.
I feel proud to be listed as a "National Memory Record Holder" in the Limca Book of Records, 2009 and have attempted for an International Memory record in the Guiness Book of Records. I can remember the value of PI upto 10,000 digits after the decimal (3.1415.....). You can contact me on javagenious.com(At)gmal.com ; I would like to hear from you :)