All You Need to Know About Java Servlets

Java servlets is a class of the Java programming language that is used to create scripts for the server side. Basically, these are the lines of code that communicate with the server and utilize its capabilities. Although servlets can be used for normal application programming also, it works best only when it is used with web pages. Essentially, servlets are Java applets but they work exclusively on web servers. If you are aware of web-based dynamic content creation languages like PHP or ASP.NET, servlets are the Java equivalent of the same.

The Need for Servlets

Before the advent of servlets, Common Gateway Interface, also known as CGI, was used to develop web based content. There was no need of a specific programming language to develop script for CGI. Any programming language could have been used provided the relevant protocols were satisfied. However, there were a lot of problems associated with CGI. The performance of the CGI protocol was not so good, and questions were raised on the scalability factor too.

Also, security was one of the major concerns. Perl was one of the most widely used programming languages for developing web scripts then. But the programming had a number of loopholes. If a programmer was smart enough, he/she could take advantage of the loopholes. They could make the script shell execute OS commands. This is the basic level of control, something that most hackers dream of.

Enter the Servlets

To counter these loopholes, developers came up with what they called Java servlets. These were based on the Java programming language and patched up most of the loopholes in CGI. The principle is that a servlet can aid in the communication between the client and the server. Basically, the client here is the user and the server is the device on which the web application is hosted on.

How Does A Servlet Work?

For example, say you are clicking on a link that will allow you to download a file on a server. Basically, when you click on the download link, a request to the server is generated. The request is to download the file to your hard drive. Servlets aid in communicating this request to the server. Based on the request, the server generates a response, which in this case might be something like ‘wait for 10 seconds’. So, this response has to be communicated to the client and that is where servlets come in again.

Type of Information Communicated

Since servlets basically communicate to web servers, the communication protocol used is HTTP, short for Hypertext Transfer Protocol. So, servlets are sometimes also called HTTP servlets. Naturally, content that is generated during the communication is also native web information, which will usually be in HTML format. However, with expansion in the usage of other programming and markup languages, servlets sometimes generates XML and PHP information too. Also, this information is contained in what is called a web container or a servlet container. Without a container, a servlet cannot function.