- MaaJanki Web tech
- Posts
- What is The Servlet in Java Program.
What is The Servlet in Java Program.
A web-application is an application program that is usually stored on a remote server, and users can access it through the use of Software known as web-browser.A web application are generally coded using the languages supported by almost every web-browsers such as HTML, JavaScript because these are the languages that rely on the web browsers to render the program executable.
Servlets are protocol and platform independent server-side software components, written in Java. They run inside a Java enabled server or application server, such as the WebSphere Application Server. Servlets are loaded and executed within the Java Virtual Machine (JVM) of the Web server or application server, in much the same way that applets are loaded and executed within the JVM of the Web client. Since servlets run inside the servers, however, they do not need a graphical user interface (GUI). In this sense, servlets are also faceless objects.
The Power of Servlets
What makes servlets a viable choice for web development? We believe that servlets offer a number of advantages over other approaches, including: portability, power, efficiency, endurance, safety, elegance, integration, extensibility, and flexibility.
What is Servlet
Servlets are small programs that execute on the server side of a Web connection and dynamically extend the functionality of aWeb server just as applets.
• Servlet is a technology which is used to create a web application.• Servlet is an API that provides many interfaces and classes including documentation.• Servlet is an interface that must be implemented for creating any Servlet.• Servlet is a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any requests.• Servlet is a web component that is deployed on the server to create a dynamic web page.
CGI (Common Gateway Interface)
CGI technology enables the web server to call an external program and pass HTTP request information to the external program to process the request. For each request, it starts a new process.

Disadvantages of CGI
There are many problems in CGI technology:
If the number of clients increases, it takes more time for sending the response.
For each request, it starts a process, and the web server is limited to start process.
It uses platform dependent language e.g., C, C++, perl.
Advantages of CGI

There are many advantages of Servlet over CGI. The web container creates threads for handling the multiple requests to the Servlet. Threads have many benefits over the Processes such as they share a common memory area, lightweight, cost of communication between the threads are low. The advantages of Servlet are as follows:
Better performance: Because it create a thread for each request, not process.
Portability: Because it uses Java language.
Robust: JVM manages servlets, so we don’t need to worry about the memory leak, garbage collection, etc.
Secure: Because it uses Java language.
Servlet Architecture
The architecture is the communication interface, protocol used, requirements of client and server, the programming with the languages and software involved. Basically, it performs the below-mentioned tasks.
The clients send the request to the webserver.
The web server receives the request.
The web server passes the request and generates the response in the form of output.
The servlet processes the request and generates the response in the form of output.
The servlet sends the response back to the webserver.
The web server sends the response back to the client and the client browser displays it on the screen.

Servlets more closely resemble Common Gateway Interface (CGI) scripts or programs than applets in terms of functionality. As in CGI programs, servlets can respond to user events from an HTML request, and then dynamically construct an HTML response that is sent back to the client.
Life Cycle of The Servlet
What is The Life Cycle of a Servlet
Three methods are central to the life cycle of a servlet. These are init( ), service( ), and destroy( ). They are implemented by every servlet and are invoked at specific times by the server. Let us consider a typical user scenario to understand when these methods are called.

First, assume that a user enters a Uniform Resource Locator (URL) to a Web browser. The browser then generates an HTTP request for this URL. This request is then sent to theappropriate server.
Second, this HTTP request is received by the Web server. The server maps this request to a particular servlet. The servlet is dynamically retrieved and loaded into the address space of theserver.
Third, the server invokes the init( ) method of the servlet. This method is invoked only when the servlet is first loaded into memory. It is possible to pass initialization parameters to the servlet so it may configure itself.
Fourth, the server invokes the service( ) method of the servlet. This method is called to process the HTTP request. You will see that it is possible for the servlet to read data that has been provided in the HTTP request. It may also formulate an HTTP response for the client.
The servlet remains in the server’s address space and is available to process any other HTTP requests received from clients. The service( ) method is called for each HTTP request.
Finally, the server may decide to unload the servlet from its memory. The algorithms by which this determination is made are specific to each server. The server calls the destroy()method to relinquish any resources such as file handles that are allocated for the servlet. Important data may be saved to a persistent store. The memory allocated for the servlet and its objects can then be garbage collected. Creating and Running a Servlet Program
The post What is The Servlet in Java Program. appeared first on TEKLOG.