Java Servlets are programs that run on a web server and used for developing web applications. Lets take an example to understand it better: Consider a scenario where we need to log on to a web site to access our e-mail account. We are required to submit the login details such as username and password for authentication. The website accepts the login information and validates it using server side programs. These server side programs can be written using different technologies, such as Common Gateway Interface (CGI), Active Server Pages (ASP) and Servlets.
Limitations of CGI
CGI scripts are written in C, C++ or Perl programming languages. In case of an application server using CGI script to process client request, the server creates a separate instance of the CGI script to process the request. As a result, the efficiency of the server is affected when there is large number of concurrent requests.
Limitations of ASP
ASP is a server side technology that enables a developer to combine both HTML and scripting language in the same page. ASP objects are multithreaded and can therefore concurrently service multiple requests. The limitation of ASP is that it is not compatible with all the commercial Web servers.
How Servlet is better than CGI and ASP
Servlets are server side programs written in Java. Unlike CGI scripts, the servlet initialization code excutes only once. Moreover, separate threads in the server handle each client request. This prevents the creation of unnecessary processes, thus enhancing the performance of the server. In addition, Servlets inherit all the features of the Java programming language. For example, like all standard Java classes, a servlet is platform independent and can be used across different operating systems. Similarly, a servlet can make use of the extensive power of Java, such as networking, multithreading, Java Database Connectivity (JDBC), internationalization, and Remote Method Invocation (RMI).
Java Servlet Technology
Servlets enable you to extend the functionality of the application server for generating dynamic content in an application. Besides inheriting all the features of Java, Servlets also have the following features:
Features and Advantages of Servlets
The advantages of servlets are as follows:
Servlets are supported on all platforms that support java and servlets work with all the major web servers. They are highly portable across operating systems and across server implementations. A servlet can be developed on a Windows NT machine running the tomcat server and later deploy it effortlessly on a high end Unix server running the iPlanet/Netscape application server.
Servlets has full power of the core Java APIs: Networking and URL access, multithreading, image manipulation, data compression, database connection (JDBC), object serialization, remote method invocation (RMI), and legacy integration (CORBA). Servlets can also take advantage of the J2EE platform.
3. Efficiency and endurance
Sevlet stays in server’s memory as a single object instance, it automatically maintains its state and can hold on to external resources.
As they are written in Java, servlets inherit the strong type safety of java language.
Servlets are highly integrated with the server. This integration allows a servlet to cooperate with the server in ways that a CGI program cannot.
6. Extensibilty and flexibility
Servlets could be extended and optimized for another type of servlets.They can create simple content using out.println() statements, or they can generate complicated sets of pages using a template engine.