Servlets – Introduction

Types of Applications

By Using Java programming language we are able to prepare the following two types of applications.

Standalone Applications

Enterprise Applications

Standalone Application

If we design and Execute any application without using Client-Server Arch and without distributing Application Logic over Multiple Machines then that Application is called a Standalone Application.

There are two types of Standalone applications.

CUI Application.

GUI Application

CUI Application v/s GUI Application

CUI Application is a Standalone application, it will be designed in such a way that to take input data from the user and to provide output data to the user through a command prompt, where the command prompt is acting as an user interface and it supports only characters data.

To prepare CUI Applications, JAVA has provided a set of predefined libraries in the form of the packages like java.io, java.lang, java.util,…. 

GUI Application is a standalone application, it will be designed in such a way that to take input data from the Users and to provide output data to the Users through a set of Graphic components , where the set of Graphics Components are acting as an user interface and it supports graphics data.

To prepare GUI applications, Java has provided some predefined libraries in the form of the packages like java.awt, java.applet, javax.swing,… 

Enterprise Applications

If we design and execute any application on the basis of the Client-Server arch or by distributing application logic over multiple machines then that application is called an Enterprise Application.

There are two types of Enterprise applications.

Web Applications

Distributed Applications

Web Application v/s Distributed Application

  1. Web Application is a Server side application, where the complete application logic is distributed at a single server machine.

Distributed application is a server side application, where the complete application logic is distributed over the local machine and remote machine.

  1. To prepare web applications we will use a set of technologies called Web Technologies.

EX: CGI, Servlets, JSP,….

To prepare Distributed applications we will use a set of technologies called Distributed technologies.

EX: Socket programming, RMI, CORBA, EJBs, Web Services,….

  1. Web applications will focus more on generating the dynamic response from Server to the client.

Distributed applications will focus more on establishing connection between local machine and remote machine and to consume remote services provided by the Remote machines.

  1. Web applications are executed by both Web Servers and Application Servers.

Distributed Applications are executed by the Application Servers only, not by Web Servers.

  1. Web Application is the combination of the web resources like Servlets, Jsps,….which are executed by the Web Containers like Servlet container, JSP Container,….

Distributed Application is a combination of the Distributed components like Java beans, Entity Beans, Session Beans,…. Which are executed by the Distributed containers like EJB Container,JMS COntainer….

  1. In Web Applications, the Client is Fixed, that is Browser.

In Distributed applications, Client is not fixed, it may be a normal J2SE application with main() method, a GUI application, a Web Component like Servlet, JSP,… , a Framework application like Struts application, JSF application,…..


Types of response in Web Apps

In web applications, there are two types of responses that we are able to generate from Server side applications.

Static Response

If the Server side application generates the response to the client without performing any action or without executing any resource at server machine then that response is called a Static response.

EX: When we send  requests with the url www.gmail.com then all the users are getting a common login page as response is a Static Response.

Dynamic Response

If the Server side application generates the response to the client by performing a particular action or by executing a particular server side resource then that response is called a Dynamic Response.

EX: After getting the login pages , after entering the user credentials , if we submit a request to the Gmail server then GMAIL Server will send a Gmail account page to all the clients with the clients personal data called Dynamic Response

In the server side applications, to generate dynamic response from the Server side application servers must execute a program at server side application, where the application which is provided at server machine and which must be executed by the server software to generate dynamic response is called a Web Application, here to prepare web applications we will use the web technologies like CGI, Servlets, JSPs,….

Generate Dynamic Responses – Why Servlets ?

The main purpose of the web technologies like CGI, Servlets, JSPs,… is to prepare web applications in order to generate the Dynamic response from the Server machine.

Q) To generate the dynamic response from the Server side applications already we have CGI then what is the requirement to use Servlets?

CGI [Common Gateway Interface] is a Server side technology, it was designed on the basis of C programming language and Scripting languages, it is a Process based Server side technology, its container will generate a separate process for each and every request in order to handle, here process is heavy weight, it will take more time and more memory to handle a request, it will reduce the application performance.

Servlet is a Server side technology, it was designed on the basis of JAVA programming language, it is a Thread based server side technology, its container will generate a separate thread for each and every request which are provided by the clients, where thread is lightweight, it will less memory and less execution time to handle the requests, It will improve the performance of the Server side application.

Servlets v/s JSPs

  1. Servlet is a Server side technology to prepare web applications, it is full of Java code, where developers must have  very good knowledge on Java programming language to prepare web applications by using Servlets. 

JSP is a Server side Technology, its main intention is to reduce Java code as much as possible in web applications, to prepare web applications by using JSP technology developers are not required to have Java knowledge. 

  1. In web applications, Servlets are mainly for the processing of the requests.

In Web applications JSPs are mainly for the Presentation purpose.

  1. In Web applications, Servlets are utilized to take the requests from clients and to process the requests.

In Web applications, JSPs are utilized to generate dynamic responses with a very good look and feel.

  1. IN MVC based web applications, we will use a Servlet as Controller.

IN MVC based web applications, we will use a set of JSP pages as the view part. 

EX: Struts is a Web Framework designed on the basis of MVC Design pattern, where ActionServlet is a Servet acting as a Front Controller and a set of JSP pages are acting as View Part.

EX: JSF is a Web Framework design on the basis of MVC design Pattern, where FacesServlet is a Servlet acting as a Front Controller and a set of JSP pages are acting as View Part.

EX: Spring WEB MVC is a web framework designed on the basis of MVC design pattern, where DispatcherServlet is a Servlet class acting as a FrontController and a set of JSP pages acting as View Part.  

  1. If we perform modifications on any Servlet then we must perform recompilation, reloading and redeployment of the application into the Server explicitly.

If we perform Modifications on any JSp file then it is not required to perform recompilation and reloading of the program, because JSp pages are auto-compiled and auto-loaded.

  1. If we prepare web applications by using only Servlets then there is no clear cut separation between presentation logic and Business logic, it provides tightly coupled design.

If we prepare web applications by using only JSPs then there is a clear cut separation between the presentation and Business logics, because in JSP pages, to prepare a presentation we will use HTML tags and to prepare Business logic we will use JSP own tags.