Introduction to Java Servlets

by Master Mind | 1:53 AM in |

Introduction to Java Servlets

Now you have the basic understanding of the HTTP protocol, web containers, and J2EE web application structure. Before you start learning Servlet API, this tutorial provides the basic understanding of the Java Servlets.
What is a servlet?

Servlets are Java classes that process the request dynamically and generate response independent of the protocol. Servlets are defined in Java Servlet API specification. At the time of writing this tutorial, the latest Servlet API specification is 2.5 available here .

Servlets are server side Java programs which extends the functionality of web server. Servlet are protocol independent that means it can be used virtually with any protocol to process the request and generate the response. However in practice Servlets are used to process the HTTP requests and generate the HTML response.

I don’t know if the implementation is available for any other protocol like FTP or SMTP! Let me know if you know about the Servlet implementation for any other protocol.

If you are not in web development already and not aware of server side programming languages, a question which may arise in your mind is: if the Servlets generates the HTML than why do we need the Servlets? We can simply create the HTML pages instead and deploy them in any web server. The answer is in the term dynamic request handling. If you create the HTML pages which are directly served by web server, it is not a dynamic web application. A HTML document is not dynamic. It is a simple static content which is served as it is whenever a request is received. Servlets are used to generate the response dynamically based on the user request. The dynamic response generation is achieved by embedding the application logic in HTTP request-response process.

The initial goal of the HTTP protocol was to enable the sharing of information over the internet. HTTP defines what makes a valid request and valid response, how clients can request information and how server can send response, but it does not define how the response could be generated. That means HTTP protocol does not define any way to incorporate the application logic in response generation. Here the server-side technologies like PHP, ASP, Java Servlets and Java server pages come into picture. These technologies provide the way to write the application logic which sits in between the request handling and response generation phases and generate the response dynamically based on the information present in the request.

Let’s see how it is achieved with Servlets:

Whenever the web container receives a HTTP request for a resource, based on the URL of the incoming request, web container determines if the request should be handled by a servlet. If the request is to be handled by a servlet, the container checks to see if the instance of the servlet is already available. If an instance is available, the container delegates the request to that instance. If no instance of the servlet is available, container creates a new instance of the class of servlet and delegates the request to that newly created instance.

When delegating the request to a servlet for processing, web container creates the objects of class HttpServletRequest and HttpServletResponse which represents the HTTP request and HTTP response and passes these objects to the Servlet instance. The instance of HttpServletRequest provides servlet the access to incoming HTTP request information, like HTTP headers, form parameters, query string etc. The instance of HttpServletResponse provides the way to send the response to the client. It provides the methods to write the HTML to the output stream to the client, add response headers, add cookies etc.

During the request processing, the code within the servlet reads the request information, executes the application logic, and generates the response. Once the processing has been completed the response is returned to the client by writing it to the HttpServletResponse.

0 comments: