Sunday, April 26, 2009

The Servlet API

Servlet is the main Interface. GenericServlet implements Servlet and is an abstract class having overloaded init methods.

init()
init(ServletConfig sc)

HttpServlet extends GenericServlet and is also an abstract class and inherits both the overloaded init methods. MyServlet extends HttpServlet and thus it also inherits the overloaded init methods.

The need for 2 init methods in the GenericServlet class. 

With only one Init

public class GenericServlet
{
ServletConfig sc;
public void init(Servletconfig sc)
{
this.sc = sc;
}
}

Now this servletconfig object is available to HttpServlet and to all the classes which extends httpServlet.

public class MyServlet extends HttpServet
{
// now i have to write my own init functionality so i override the init method.
// by doing this, I have lost the functionality of the GenericServlet's init method and thus
     // lost the ServletConfig object. 

public void init(ServletConfig sc)
{
// my own functionality 
}
}

Thus to avoid this and to have the servletconfig object always. Programmers then need to write their own servlet class and call super to save the servletconfig object.

The above code can be re written as 

public class MyServlet extends HttpServet
{
// now i have to write my own init functionality so i override the init method.
// I retain the servletconfig object by calling super.init(servletconfig), thus i still have 
// access to the servletconfig object 

public void init(ServletConfig sc)
{
super.init(servletconfig);
// my own functionality 
}
}

Thus every time I write my own servlet class, in the init method I need to call super.init(servletconfig). To avoid this, an init method was added in the genericservlet class and design changes were as follows.

public class genericservlet
{
servletconfig sc;
public void init(servletconfig sc)
{
this.sc = sc;
init();
}

public void init()
{
// a do nothing method, waiting to be overridden
}

}

public class myservlet extends httpservlet 
{
public void init()
{
// init method overridden and the programmer still has the access to the servletconfig   // object. since the container calls the init(servletconfig sc) method everytime it need to  
      // initialize the servlet.
}
}

Saturday, April 25, 2009

What are servlets, its fow control and life cycle

In general, Servlet is an Interface consisting of methods which every servlet needs to implement.
GenericServlet is an abstract class which implements some of the method of the Servlet Interface. Since the communication between the client and the server happens with the HTTP protocol, the generic servlet was further sub classed to HttpServlet. An httpServlet is an abstract class consists of all the methods which an servlet is required for Http Communication. 

Any HttpServlet which is written by the programmer should obey the following rules.
  1. The servlet written should be public ( since the container calls the servlet and its not called by any one else, since it does not have a Main class)
  2. The servlet written should extend the httpservlet class.
LifeCycle of a Servlet

There are in all 3 states in which a servlet can be
  1. init()
  2. service()
  3. destroy()
however there is a little more to this theory.
  1. When a container gets a request for the servlet, the container first loads the servlet class.
  2. Once the servlet class is loaded, the container executes the default constructor and instantiate the servlet class.
  3. The container calls the init() method to initialize the servlet. The initialization of the servlet happens only once in the life cycle of the servlet. This can be done at deploy time or at run time. 
  4. Once the servlet has been initialize the service method is called. At this stage the request is getting processed.
  5. Once all the request has been processed, the container calls the destroy method and the thread, the request, response objects and the servlet is being sdestroyed.
With the understanding of the life cycle of the servlet lets go and have a look at the servlet API. 

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.
 

Wednesday, April 22, 2009

Webservers and Static corelation

Web servers accepts requests from web clients, looks for the resource and returns the response to the client. However web servers only send static data. In other words web server itself cannot process anything. It can just look for resources and if found sends across the resource to the client. 

However if that is the case we require much more than a web server then. 

What if, if we require that when a user requests for a page, the current date and time is to be displayed to the user. This dynamicity can be achieved by the web server with the help of a Web application.

A web application has the ability to process a request and return the result. Thus when a user requests for a dynamic data, the web server accepts the request and then forwards it to the web application. The web application processes the request and returns the result. This result is then returned to the user via the web server

Thus for the client, its the web server who is processing the request and actually its the web application who has done the job of processing the request for the web server.

HttpRequest and HttpResponse

The HttpResponse Object

When the webserver locates the resource it returns the response as HTML ( can be other than HTML). In the response, the webserver adds a HTTP header info and then attaches the response. The response contains HTML.
When the webclient reads the Http response ie http header and when it encounters the HTML, it goes into the HTML rendering mode and displays the HTML to the client. On reading the HTML when the client encounters an IMG SRC tag it again makes a request to the server, gets the resource ( in this case the image) and displays the response to the client.

The MIME type

When a server sends a response, it also sends the information about the type of the response which is send by the server. The webclient on reading this request undustands that the response is of a particular type and displays it in the required manner

eg Content-type : text/html

The HttpRequest Object

The HttpRequest header consists of the following
  • Name of the method used to communicate between the client and the server
  • The Resource Path
  • The parameters required for the resource 
  • The protocol name
Name of the method:
There are two methods (generally 2 ways) to pass request from the client to the server
  1. Get
  2. Post
Get
  1. The number of parameters passed in GET are limited
  2. Get is generally used to querry the webserver and get the output, although get can be used to send request data
  3. URLs used with GET cannot be bookmarked
  4. The parameters passed in GET can be seen at the address bar when the request is made, thus its not safe.
  5. Get requests are idempotent, means that a user can do the same thing again and again.(besides Get, Head and PUT are idempotent)
  6. If a method name is not specified in the method attribute in the form tag then its a get request
POST
  1. You can pass number of parameters in the POST request.
  2. The parameters passed in PosT is send as the body of the request.
  3. POST is used to send the data to the webserver
  4. URL with post can be bookmarked.
  5. Post is non idempotent.
  6. Need to explicitly specify the post request.

Tuesday, April 21, 2009

Webservers and Webclients

Webservers
Webservers accepts requests from webclients, looks for the resource and returns the response to the client.

Webclients
Webclients sends a request to the webserver and displays the response to the user. In order to communicate betweeen webservers and webclients a protocol is required. A protocol is nothing but an aggrement between the 2 parties. The protocol in this case is HTTP.

Thus the above statement can be refrased as

Webservers accepts Http requests from webclients, looks for the resource and returns the Http response to the client while Webclients sends a Http request to the webserver and displays the Http response to the user.