Thursday, April 23, 2009

C2 - Servlets and Containers

HTML is not a programming language. When a user inputs a form, the server needs to do some processing to return the output. Since HTML is not a programming language, the task in Java to process the request and submit a response is done by JSP, Servlets.

When a user submits a request, the request is submitted to a servlet/JSP. The name of the servlet/JSP is in the action attribute of the form. 

JSP/Servlets are the web components to execute some reuseable task with a request and response objects. To be more precise JSP/servlets are nothing but java classes. 

The request from the HTML form does not directly calls the servlet. In fact the servlet cannot be called, it does not have a main method. It is the container which calls the servlet. More about containers next.

Container

Inside a web server, we have something called as a Web Container. The web container is the one which manages both servlets and JSP.  Request from the user in HTML --> webserver --> webcontainer --> gives it to webapp --> gives it to the servlet -- > servlet processes the request --> generates the response --> this response is then send back to the client. 

A container makes the work of the programmer much lighter, In fact its the container which provides the underlying services mentioned below to communicate with the jsp and servlets

Multi threading
For every new request, the container creates request and the response objects and creates a new thread, Once a    thread has been created, the container calls the service method (which intern calls the doget/dopost method of the servlet) and passes the request and the response objects to the service method.
 
Provides Life cycle Methods: 
The servlets/JSP does not have a main method, thus it cannot be instantiated. The work of calling the servlets and the jsp's is done by the container thus managing the life cycle of servlets/jsp

Generates the request and the response objects
For every new request, the container creates request and the response objects and creates a new thread, Once a    thread has been created, the container calls the service method (which intern calls the doget/dopost method of the servlet) and passes the request and the response objects to the service method.

Provides security to the webapp ie JSP and servlets.
more about this later. Security is provided here descriptively. Ie the source code does not gets affected by adding security.

Communication support
Its is the work of the container to read the data which is send by the client by (either RMI, bytestream , etc ) and submit the data to the servlet. A programmer does not have to write the code to communicate between the client and the server.
 

No comments:

Post a Comment