2. request:
A request implicit object is nothing but an instance of HttpServletRequest class. We know that for every servlet , HttpServletRequest and HttpServletResponse objects are passed by the Web container to every servlet. Since a jsp is nothing but a servlet at the end. You should have access to all the objects which you have in a servlet so for that reason you have access to the Http request object in your jsps and you can access the using implicit object request.
And you use these attributes stored in request object for display/basic validation in jsp.
- We also have response object like request object.
- You also have the access to HttpServletResponse in your jsps and that is through the response implicit object. Basically the response implicit object is used to send the response back to the client . Sending response back to the client is nothing but a html page but we also know that apart from html there are lot of other types of files we can send back to the client (we can send jar, zip, jpj, avi, excel spreadsheet and lot of other things)
- In order to send all these different types of files to the end client you should use the "response" implicit object. So basically when you are sending these different types of files to the client , you are going to set the MIME type first in your response, so by default MIME type is nothing but html, but if you want to send a jar file then you have to change the MIME type . If you want to send a movie file you have to change the MIME type and you send appropriate response in the response object.
Second advantage:
- One thing we need to talk about the response object is, the use send redirect, while discussing servlets we came across the user request dispatcher, and I said that when a particular servlet in your application can not serve the request any more , then it can send request to some other servlets which can handle that request. In a similar manner even for a jsp you can send that request some other jsp page or some other servlet which can handle the request.
- So lets say you have a page, that you have a shopping card web page in your application, you have changed the where you want to display the shopping card application, you changed the lot of things around that, and you also changed the jsp page for the shopping card and it is a new jsp page. So whenever people come back to that old url for shopping card and they would not see anything in that page because you moved everything to the new page. Now you want to redirect your clients to the new page, in that case you can use "response" implicit object. Pass the new url to the sendRedirect() method. So when you are using sendRedirect() you are actually sending the request back to the client, in the case of Request dispatcher you are not sending the request back to the client and here the client is nothing but the browser but not the end client.
requestDispatcher() and sendRedirect():
- Now we have requestDispatcher and sendRedirect(), in requestDispatcher it is directly forward to servlet2 without any kind of interruption or sending back request to the client but when you are using sendRedirect() jsp1 can not serve the request, it is going to send the response back to the client which is nothing but the browser. Jsp1 says that I can not serve the request but I know that particular "url" , this jsp can serve a request, please make a request again to this new url, and your browser now is going to make a second request to this new url (i.e sendRedirect("url")) and new JSP is displayed to the end user.
- So for end user prospective, using requestDispatcher(), sendRedirect() does not really make any sense because all he is sees a new jsp page he does not know what internally happens whether the application send back the response to the browser or directly forward to the request to the new page, really he does not know.
Difference between sendRedirect() and requestDispatcher()
1. when you are using sendRedirect() you can redirect the page to completely different application which is out side your application, that is you can send a request from yahoo to google because it is nothing but a url.
1. when you are using requestDispatcher(), you can only send the request to, you can only forward request to another page within your same application
No comments:
Post a Comment