Tuesday 3 September 2013

Request and Response objects in Servlets

Request and Response

We know that when send the request to the server, the Web Container invokes and calls the servlet methods and servlet sends the response to the client. But now assume that the servlet does not have the ability to serve the request of the client , now what happens.


Fig: Response.jpg

You just see the Figure: the request comes and the servlet serves the request and it sends the resource to the client.
Now lets see the if the servlet does not serve the request, now the servlet can not serve the request,


For Example: i have created a website called abc.com and i have got a lot of users for the web site, after some couple of years i want to change my name of that website abc to xyz.com. But i have developed lot of clients and i dont want to loose those clients so what I want to do is now my abc servlet can not serve the request from the client but whenever a request comes to abc servlet and I want the request can be redirected to xyz servlet so i dont want the client go get error page for my previous name abc.com. I know that abc servlet can not serve the request, and I know that xyz servlet can serve the request. I want people to talk xyz servlet now instead of abc servlet.

My abc servlet can not serve the request but "does the servlet know who can serve the request?" it knows because xyz servlet can serve the request, it now sends the response back to the client and it talks the xyz servlet and sends the response back to the client.
There are two ways which can do this process:
we can use the following ways for that purpose
1. send redirect
2. request dispatcher
Now lets talk the difference between them:

When you implement a Send Redirect the servlet actually sends back response to the client (web Browser) and says that it can not serve the request but it knows the xyz servlet can serve the request, so please send the request to xyz servlet so the web browser now gets this response from servlet abc and sends another request to servlet xyz and finally gets the resource from xyz servlet. But really being a end client dont know that response has come back from abc and web browser has send another request to xyz, but you dont know that these two steps process have happened.
From your(end client) prospective you only types abc.com and you got some response, but you dont knw that these two steps process have been taken to get the response so that is send redirect.

What happens Request Dispatcher?
Ans: In Request Dispatcher basically the servlet itself (the abc servlet itself) will forward the request to xyz servlet and the response will send back to xyz servlet to the client, so here there is no failure response send back to the browser and browser send back another request to the xyz servlet but the abc servlet itself forwards the request to xyz servlet and xyz servlet forwards the response to the client so that is Request Dispatcher.

When should we use Send Redirect and Request Dispatcher?
Ans: Basically you can use Request Dispatcher only when the other servlet which can serve the request of the client and lies in the same web application, if the other servlet in the same web applicaiton only in that scenario you can use the request dispatcher. But if the other servlet is same in the web application only in that scenario you can use a Request Dispatcher. But if the other servlet is outside this web applicaiton, or if the servlet is in other webapplication then you have to use send Redirect because it has to make another request, it really communicate with the other servlet which lies outside the web application. And finally the servlet abc really know who can serve the request sent by the client, then it sends the Error page to the client.

For example: we know that sunmicrosystem was merged in Oracle organisation, but still if you type the sunmicrosystem.com in the web browser it will be redirected to oracle.com.

No comments:

Post a Comment