Tuesday, December 7, 2021

Http Request and Http Response

 Differences between Servlet and CGI

Servlet

CGI

In    Servlets      each      request       is    handled       by lightweight Java Thread(Multi threaded)

In CGI each request is handled by heavy weight OS process.

Servlets are Platform Independent.

CGI is not Platform Independent.

Servlets are portable.

CGI is not portable.

Servlets can link directly to the Web server

CGI cannot directly link to Web server.

Servlets can handle cookies

CGI cannot handle cookies

Servlets can track sessions

CGI cannot track sessions

Servlets are inexpensive than CGI

CGI is more expensive than Servlets

Servlets are more secure than CGI

CGI programs can be more vulnerable to attacks than Servlets











CGI (Common Gateway Interface)

 CGI (Common Gateway Interface)

The common gateway interface (CGI) is a standard way for a Web server to pass a Web user's request to an application program and to receive data back to forward to the user. These programs allow a Web developer to deliver dynamic information (usually in the form of HTML) via the browser. A CGI program can be written in any language, including Java that can be executed by your Web server.

The performance issues in the platform-dependent CGI programs let to the introduction of Servlets. Servlets are platform-independent. The main purpose of a servlet is to collect the request from the client and generate the requested web page dynamically for a corresponding request and send it back to the client. Java Servlets often serve the same purpose as programs implemented using the Common Gateway Interface (CGI).

Server has to create a new CGI process for every client request. For example, If 100 users are accessing the web application, and then the server has to create 100 CGI processes to handle the request made by them. Since a server has limited resources, creating new process every time for a new request is not a viable option, this imposed the limitation on server, due to that the server cannot handle more than a specified number of users at the same time. The CGI programs are memory intensive programs.

CGI Environmental Variables:

Environment variables are a series of hidden values that the web server sends to every CGI program you run.


Key

Value

HTTP_COOKIE

The visitor's cookie, if one is set

HTTP_HOST

The hostname of the page being attempted

HTTP_USER_AGENT

The browser type of the visitor

PATH

The system path your server is running under

REMOTE_ADDR

The IP address of the visitor

REMOTE_HOST

The hostname of the visitor

REMOTE_PORT

The port the visitor is connected to on the web server

REMOTE_USER

The visitor's username (for .htaccess-protected pages)

REQUEST_METHOD

GET or POST

SERVER_NAME

Your server's fully qualified domain name (e.g.

www.cgi101.com)

SERVER_PORT

The port number your server is listening on


Important Terms in Servlets

Java Servlets are programs that run on a Web or Application server and act as a middle layer between requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server.

 Some terminologies about Java and server:

 JDK: This is needed if we wanted to just compile standard java programs.

 JRE: This is run time environment under which a java program compiled above and execute.

 JSE: Java Standard Edition. It is basically JDK+JRE.

 JEE: This is ‗Enterprise version‘ of Java, which meant support for server-side technologies (Servlets and JSP).

 WWW: Designed by Tim Berners Lee in 1991, It is an information space where documents and other web resources are identified by URL, interlinked by HyperText links and accessible by internet.

 TCP: Defines how to establish and maintain a network conversation between client and server. It works with IP.

 URL: Uniform Resource Locator. HTTP: It is the underlying protocol used by the World Wide Web and defines how messages are formatted and transmitted, and what actions Web servers and browsers should take in response to various commands(Stateless Protocol).

Introduction to Servlets

 Introduction to Servlets

A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. Applet and Servlet are the small Java programs or applications. But, both get processed in a different environment. The applet is usually embedded in an HTML page on a Web site and can be executed from within a browser .

following are the differences between applets & servlets.

APPLETS

SERVLETS

Applet is a Java programme which is executed on the client machine.

Servlet is a Java programme that runs inside JVM on the web server.

Life cycle of Applets are:

init(), stop(), paint(), start(), destroy().

Life cycle of servlets are:

init( ), service( ), and destroy( ).

Applets are more prone to risk as it is on the client machine.

 

Servlets are under the server security.

Applets may have Graphical User Interface

Servlets have no Graphical User Interface

Applets extend the web browser.

Servlets extend the web server.


A simple Java program to find the inverse of a given matrix

  import java.util.Scanner; public class MatrixInverse { public static void main (String[] args) { Scanner scanner =...