Introduction to Servlet for Beginner

In this post, you will get to know the Architecture of Servlet in detail with example.

Servlets are modules that run inside request/response-oriented servers, such as Java-enabled web servers. 

Functionally they operate in a very similar way to CGI(Common Gateway Interface) scripts, however, being Java-based they are more platform-independent. 

Servlet Technology is used to create web applications. 

Web applications are helper applications that reside at the web server and build dynamic web pages. 

A dynamic page could be anything like a page that randomly chooses pictures to display or even a page that displays the current time.


Working of Servlet:

After receiving any request from the client first web container checks whether the instance for the requested servlet exists or not. 

If the servlet instance exists then the container pass that request to the instance. 

If the instance does not exist container loads the servlet file and creates the instance of the requested servlet.

After receiving any request from the client first web container checks whether the instance for the requested servlet exists or not. 

If the servlet instance exists then the container pass that request to the instance. 

If the instance does not exist container loads the servlet file and creates the instance of the requested servlet.

 Difference between CGI and Servlets:

CGI

Servlet

Platform Dependent

Platform independent

Every request is processed by own heavy process

Every request is processed by a thread which is lightweight

More vulnerable to attacks than servlets

Less vulnerable to attacks as it in java language

Not much stronger as Servlets

Stronger than CGI because of Strong memory management and exception handling

CGI does not provide sharing property.

Servlets can share data among each other.

CGI cannot read and set HTTP headers, handle cookies, tracking sessions

Servlets can read and set HTTP headers, handle cookies, tracking sessions.


 Applications of Servlet:

• Processing data POSTed over HTTPS using an HTML form, including purchase order or credit card data: A servlet like this could be part of an order-entry and processing system, working with product and inventory databases, and an online payment system.

• Allowing collaboration between people: A servlet can handle multiple requests concurrently; they can synchronize requests to support systems such as on-line conferencing. 

• Forwarding requests: Servlets can forward requests to other servers and servlets. This allows them to be used to balance load among several servers that mirror the same content. It also allows them to be used to partition a single logical service over several servers, according to task type or organizational boundaries. 

 Advantages of using Servlet:

• Servlet technology was introduced to overcome the shortcomings of CGI technology. 

• It provides better performance than CGI in terms of processing time, memory utilization because servlets use benefits of multithreading and for each request, a new thread is created, that is faster than loading creating new Objects for each request with CGI. 

• Servlets are platform-independent, the web application developed with Servlet can be run on any standard web container such as Tomcat, JBoss, Glassfish servers and on operating systems such as Windows, Linux, Unix, Solaris, Mac, etc. 

• Servlets are robust because the container takes care of the life cycle of the servlet and we don’t need to worry about memory leaks, security, garbage collection, etc. 

• Servlets are maintainable.


Servlet Architecture:

All servlets implement Servlet interface, either directly or, more commonly, by extending a class HttpServlet. HTTPServlet indirectly implements the Servlet interface. 

The Servlet interface provides the following methods that manage the servlet and its communications with clients. init, service, getServletConfig, getServletInfo, destroy  

When a servlet accepts a service call from a client, it receives two objects, ServletRequest and ServletResponse. 

The ServletRequest class encapsulates the communication from the client to the server, while the ServletResponse class encapsulates the communication from the servlet back to the client. 

The ServletRequest interface allows the servlet access to information such as the names of the parameters passed in by the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it. 

It also provides the servlet with access to the input stream, ServletInputStream, through which the servlet gets data from clients that are using application protocols such as the HTTP POST and PUT methods. 

Subclasses of ServletRequest allow the servlet to retrieve more protocol-specific data. For example, HttpServletRequest contains methods for accessing HTTP-specific header information. 

The ServletResponse interface gives the servlet methods for replying to the client. It allows the servlet to set the content length and mime type of the reply, and provides an output stream, ServletOutputStream, and a Writer through which the servlet can send the reply data. 

Subclasses of ServletResponse give the servlet more protocol-specific capabilities. 

For example, HttpServletResponse contains methods that allow the servlet to manipulate HTTP-specific header information.

HTTP servlets have some additional objects that provide session-tracking capabilities. 

The servlet writer can use these APIs to maintain state between the servlet and the client that persists across multiple connections during some time period. 

 


Recommended Topics:

 1. Web Server and Working of CGI

2. Life Cycle of Servlet



Previous
Next Post »