Servlets in Java
Earlier in client- server computing, each application had its own client program and it worked as a user interface and need to be installed on each user's personal computer. In web applications we mostly use HTML/XHTML which is mostly supported by all the browsers and web page is displayed to the client as a static document. A web page can merely displays static content and it also lets the user navigate through the content, but a web application provides a more interactive experience.
Any computer running Servlets or JSP needs to have a container. A container is nothing but a piece of software responsible for loading, executing and unloading the Servlets and JSP. While servlets can be used to extend the functionality of any Java- enabled server., they are mostly used to extend web servers, and are the efficient replacement for CGI scripts. CGI was one of the earliest and most prominent server side dynamic content solutions, so before going forward it is very important to know the difference between CGI and the Servlets.
Common Gateway Interface (CGI)
The Common Gateway Interface, which is normally referred as CGI, was one of the practical technique developed for creating dynamic content. By using the CGI, a web server passes requests to an external program and after executing the program the content is sent to the client as the output. In CGI when a server receives a request it creates a new process to run the CGI program, so creating a process for each request requires significant server resources and time which limits the number of requests which can be processed concurrently. CGI applications are platform dependent. There is no doubt that CGI played a major role in the explosion of the Internet but its performance, scalability issues make it less than optimal solutions.
Java Servlet is a generic server extension that means a Java class that can be loaded dynamically to expand the functionality of a server. We use servlets with web servers and it runs inside a Java Virtual Machine (JVM) on the server so it is safe and portable. Unlike applets they do not require the support for Java in the web browser. Unlike CGI servlets don't use multiple processes to handle separate request. Servets can be handled by separate threads within the same process. Servlets are also portable and platform independent.
Advantages of Servlets
As we know that the servlets are written in Java and follow a well standardized API so they are highly portable across operating systems and server implementations. We can develop a servlet on Windows machine running the tomcat server or any other server and later we can deploy that servlet effortlessly on any other operating system like Unix server running on the iPlanet/Netscape Application server. So servlet is write once, run anywhere (WORA) program.
We can do several things with the servlets which were difficult or even impossible to do with CGI, for example the servlets can talk directly to the web server while the CGI programs can't do. Servlets can share data among each other, they even make the database connection pools easy to implement. They can maintain the session by session tracking which helps them to maintain information from request to request. It can do many other things which are difficult to implement in the CGI programs.
As compared to CGI the servlets invocation is highly efficient. When the servlet get loaded in the server, it remains in the server's memory as a single object instance. However with servlets there are N threads but only a single copy of the servlet class. Multiple concurrent requests are handled by separate threads so we can say that te servlets are highly scalable.
As servlets are written in Java, servlets inherit the strong type safety of the 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 exceptions occurs then it will throws an exception.
Servlets are tightly integrated with the server. Servlet can use the server to translate the file paths, perform logging, check authorization, and MIME type mapping etc.
The servlet API is designed in such a way that it can be easily extensible. As it stands today, the servlet API support Http Servlets, but in later date it can be extended for another type of servlets.
There are number of free web servers available for personal use or for commercial purpose. Web servers are relatively expensive. So by using the free available web servers you can add servlet support to it.