Tuesday, July 17, 2007

To pass more then one request from jsp page to java servlet

Request attributes only last the life of one request.A "request" is asking the server for one URLThe "response" is what the server sends back.So every click of a link, every form submission, every image downloaded - each of them is a seperate request.You are sending two different requests1 - load page with initial form (ie Send.jsp) (setting request attribute)2 - load page with result ( /Receive ) (trying to retrieve request attribute)Because you have two seperate requests, you can't use request attributes to pass data, and have to use session.If you use RequestDispatcher or to forward/include to other JSP resources. You are still on the same request but transferring the control to another resource. In this scenario as you have the same request, request attributes can be used.The classic example is - invoke a servlet- servlet loads data from database into request attributes- servlet calls RequestDispatcher.forward("/someJspPage.jsp");- jsp page retrieves request attributes.




request.setAttribute versus session.setAttribute:


My understanding is that request.setAttribute only make the key available in the following jsp page. But session.setAttribute will make the key available in many pages, as long as in the same session, and browser not close.For example, the flow like page1->page2->page3->page4. session.setAttribute will make the key available in all pages. But if we use request.setAttribute in page2, then only page3 can get the key value set in page2.

0 comments: