VR23 WEB TECHNOLOGY-Servlets UNIT-3

 







Introduction to Servlet




Servlets are protocol and platform independent server-side software components, written in Java. They run inside a Java enabled servlet or application server, such as the WebSphere Application Server. Servlets are loaded and executed within the Java Virtual Machine 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 a Web 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.

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.

  1. The clients send the request to the webserver.
  2. The web server receives the request.
  3. The web server passes the request to the corresponding servlet.
  4. The servlet processes the request and generates the response in the form of output.
  5. The servlet sends the response back to the webserver.
  6. 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.

Common Gateway Interface




The Common Gateway Interface (CGI) is the standard process that uses a set of rules to propagate the user´s request to the web resources such as web server  or web application program and respond to the user through the web interface. CGI includes several working scripts and programs for web communication. CGI is the mechanism that is part of the Hypertext Transport Protocol (HTTP). One of the examples of CGI flow is the Web browsers send the forms data to the backend server, and CGI connects to the application program on the web-server and the program response to the web browser.

CGI Architecture




Features of CGI

  • It is a very well defined and supported standard.
  • CGI scripts are generally written in either Perl, C, or maybe just a simple shell script.
  • CGI is a technology that interfaces with HTML.
  • CGI is the best method to create a counter because it is currently the quickest
  • CGI standard is generally the most compatible with today´s browsers

Advantages of CGI

  • The advanced tasks are currently a lot easier to perform in CGI than in Java.
  • It is always easier to use the code already written than to write your own.
  • CGI specifies that the programs can be written in any language, and on any platform, as long as they conform to the specification.
  • CGI-based counters and CGI code to perform simple tasks are available in plenty.

Disadvantages of CGI

  • In Common Gateway Interface each page load incurs overhead by having to load the programs into memory.
  • Generally, data cannot be easily cached in memory between page loads.
  • There is a huge existing code base, much of it in Perl.
  • CGI uses up a lot of processing time.

Common Gateway Interface(CGI) vs Servlet


Common Gateway Interface

The Common Gateway Interface (CGI) provides the middleware between servers and external databases or information sources. The Web server typically passes the form information to a small application program that processes the data and may send back a confirmation message. This process or convention for passing data back and forth between the server and the application is called the common gateway interface. The following image describes how a web server acts as an intermediate between the CGI program and the client browser.




A new process is started for each client request. The creation of the process for every such request requires time and significant server resources which limits the number of requests a server can handle

Servlet

Servlets are server based java application that can link directly to the Web server. Servlets are thread based applications and work on server level and they share data among each other. All the programs of Servlets are written in JAVA and they get to run on JAVA Virtual Machine. The following image describes how a request from clients is served with the help of threads:





CGI

Servlet

The CGI programs are written in native OS.Servlet is written in java class & its runs in JVM.
CGI creates a process base for each request.Servlet creates a new thread to process each request.
The CGI is a language-independent interface that allows a server to start an external process.The main purpose of servlets is to add up the functionality to a web server.
A CGI program needs to be loaded and started on each CGI request.Servlets stay in the memory while serving the requests.
CGI is not able to read the HTTP servers.Servlets are useful to read and set the HTTP servers.
CGI are plateform dependent.Servlets are plateform independent.
Servlets are more secure and useful for Data sharing.CGI is less secure and is not useful for data sharing.




Servlet Life Cycle

The Life Cycle of a Servlet

Life cycle of a servlet contains the following stages:
  • Instantiation
  • Initialization
  • Servicing
  • Destruction



Initialization

 

After instantiation of the servlet, the servlet container initializes the servlet object. Servlet container invokes the init(ServletConfig) method on the servlet object. This stage may raise ServletException or UnavailableException.

 

Servicing

 

After initialization, servlet container creates the objects of HTTPServletRequest and HTTPServletResponse and invokes the service(request, response) method to serve the request of the client This stage may raise UnavailableException or IOException.

 

Destruction

 

A servlet is destroyed (unloaded) in the following cases:

  • When the context is unloaded (server shutdown).
  • When the service() method throws UnavailableException.

 

When the container decides to destroy the servlet, it allows all the current running threads to complete and stop any further requests. Finally destroy() method is invoked.

 

Note: In the servlet life cycle, init() and destroy() execute only once and service() method executes multiple times.

















No comments

File Uploading in PHP

  File Uploading in PHP PHP allow you to upload any type of a file i.e. image, binary or text files.etc..,PHP has one in built global variab...