Wednesday, December 14, 2011

What is HTTP ? How does HTTP work ?

 " Hypertext Transfer Protocol " is a request - response protocol which is used by webmasters and other clients to access a web server.
Basics of HTTP

  • HTTP ( Hypertext Transfer Protocol ) is a protocol which is used by a www ( e.g a Browser ) to send a request to a web server.
  • HTTP is a Request - Response oriented protocol.
  • A HTTP request consists of 
  1. Request method
  2. URI
  3. Header fields
  4. Body ( which can be empty )
  •  A HTTP response consists of
  1.  Result code
  2. Header fields
  3. Body
 Overall Process

  • The service method of HttpServlet dispatches a request to different java methods for different HTTP request methods.
  • It recognizes the standard HTTP/1.1 methods and It should not be overridden in subclasses unless you need to implement additional methods.
  • The recognized methods are GET, HEAD, PUT, POST, DELETE, OPTIONS and TRACE. Other methods are answered with a Bad request HTTP error.
  • An HTTP method xxx is dispatched to a Java method doxxx ie. GET ==> doGET.
  • The doOptions & doTrace methods hava suitable default implementations & are usually not overridden.
  • The HEAD method ( similar to GET method ) is performed by calling doGet & ignores any o/p that is written by this method.
  • This leaves us with doGet, doPut, doPost & doDelete. whose default implementations return a Bad request in HttpServlet.
  • HttpServlet overrides one or more of the remaining methods by its subclass to provide a meaningful implementation.
The Request data is passed to all methods through the first argument of type HttpServletRequest ( A subclass of ServletRequest class ).
The Response data created is created with methods of the second argument of type HttpServletResponse ( A subclass of ServletResponse class ).
 How GET and POST methods work ?

when a client request a URL through a web browser, the GET method is used for the request.
 GET method

  • A GET request does not have a body ( i.e  the body is empty ).
  • where as the response should contain a body with the response data & header fields which describe the body ( like content-type & content-encoding )
  • when we send a HTML form, using GET method( request) the parameters are encoded in the URL ( i.e The data is sent through URL ).
PUT method

  • PUT method(request) is used by HTML editors & Upload tools to upload resources to a web server.
  • when we send a HTML form, using PUT method(request) the parameters are transmitted in the body ( i.e The data is sent through Body ).



Tuesday, December 13, 2011

Advantages of Servlets over CGI

  • Servlets are server side components that provides a mechanism for developing server web applications  for server side.
  • Earlier CGI was developed to provide server side Capabilities to the web applications, But due to its Performance, Scalability and Reusability  issues, Servlets are preferred.
  • Java Servlets overcome all the issues of CGI ( Common Gateway Interface ).
  • Servlets are built from ground up using Sun's Write one run anywhere technology, java servlets provide excellent framework for server side processing.
  • Web Developers can create fast & efficient server side applications using java servlets and can run it on any web server which is servlet compatible.
  • servlets run entirely inside the java Virtual Machine.
  • since servlets runs on server side, it does not check for Browser Compatibility.
Servlets mainly have 5 Advantages over CGI, they are

1) Platform Independent

  • Platform Independence plays a major in the field where there are numerous number of web servers available to choose from.
  • Servlets are written entirely in java, due to which they are platform independent.
  • Servlets can run on any servlet enabled web-server.
Ex: web applications developed in windows machine running java web server, can easily run the same on apache web server ( if Apache server is installed ), without any modification or compilation of code.

2) Performance

  • Due to interpreted nature of java, programs written in java are slow. But java servlets run very fast. it is because the way servlets run on web server.
  • For any program Initialization takes significant amount of time in its life cycle. But in case of servlets initialization takes place only once when it receives the first request & remains in memory till times out or server shuts down's.
  • Once servlet is initialized & loaded, to handle a new request it simply creates a new thread and runs service method of servlet.
  • In case of CGI scripts, always a new process should be created to serve a request.
3) Extensibility

  • Servlets are developed in java which is robust, well-designed & Object oriented language which can be extended or polymorphed into a new object
  •  java servlets take all java's advantages to provide the ideal solution.
 4) Safety

  • Java provides very good safety feature's like Memory management, Exception handling etc.
  • Servlets inherit all these features & had emerged as a very powerful web server extension.
5) Security

  • Since servlets are server side Components, it inherits the security of server.
  • Servlets are also benefited with java security manager, provided by web server.

Monday, December 12, 2011

What is a Java Servlet ?

Servlet Definition:

Servlet is a small java program that runs on Server-side, which serves as an Intermediate layer between HTTP ( Hyper Text Transfer Protocol) request of a Client and Application's in the Web Server.

A Servlet is a small, pluggable extension to a server that enhances the server's functionality.

Servlets are server side components that provide a powerful mechanism for developing server side programs.

Servlets are both server and platform-independent, so they have no performance limitations.

Servlets run entirely inside Java Virtual Machine at Server.

Servlets have become an alternative to CGI ( Common Gateway Interface ), Due to the performance limitations of CGI programs.

Servlets are used for building Interactive web applications.

Difference Between CGI and java Servlet?

The main difference between the two is that, once Servlet is Initialized it stays in the server container until there are no requests to serve ( ie : it performs multiple requests ). where as, a CGI program gets destroyed once it has fulfilled a request.

Since servlet are initialize once and serve many type, this makes them faster over CGI, Because CGI should always go through the initialize state for every request made.


Browser Compatibility?

Since the Servlet runs at server side , It does not check the Browser for Compatibility.Servlets run entirely inside Java Virtual Machine.

Servlet Classes

Servlets uses the classes in the java packages

  • javax.servlet
  • javax.servlet.http
HTTP Protocols

Servlets are not designed for a specific protocols, but they are often used with the HTTP protocols.

HTTP Servlet are typically used for:
  • Dynamic Content ( like getting the result of a database query and returning to the client).
  • Process or store the data submitted by the HTML.
  • Manage info about an stateless HTTP ( e.g. an online shopping cart manages request for multiple concurrent customers). HTTP as a protocol is stateless.