-1

I'm still puzzled on the usage of a RequestDispatcher.

If i have a javascript file which internally using a url to call a servlet as shown below:

var url = "../../../../FeesServlet?selectedCode="+selectedCode+"&searchNameOrCode="+searchNameOrCode

req.open("GET", url, true);
req.onreadystatechange = someFunction();
req.send(null);

Why do we need a RequestDispatcher when i can get the above job done without it? The above code still called a servlet (resource) where my business logic resides and still get a job done.

I just don't see the need for a RequestDispatcher.

yapkm01
  • 3,590
  • 7
  • 37
  • 62

1 Answers1

0

But how do you forward in the servlet to the new url/servlet? You need RequestDispatcher for that:

RequestDispatcher dispatcher = aRequest.getRequestDispatcher(aResponsePage.toString());
dispatcher.forward(aRequest, aResponse);

if you don't need to forward you don't need a dispatcher.

Roman Goyenko
  • 6,965
  • 5
  • 48
  • 81
  • IC. Now from javascript file, of course i can call a servlet as shown above but there is no way to call another servlet within a servlet (like JS) unless you use a RequestsDispatcher? Did i get it right? – yapkm01 Nov 09 '11 at 20:47
  • Yes, you can either call forward or redirect using HttpServletResponse redirect function. – Roman Goyenko Nov 09 '11 at 20:54
  • @Ramario A redirect function - the execution will not continue in the calling program while a forward/include on a RequestsDispatcher will. Bingo? – yapkm01 Nov 09 '11 at 20:57
  • read here about the differences of forward vs redirect: http://www.javapractices.com/topic/TopicAction.do?Id=181 – Roman Goyenko Nov 09 '11 at 21:01