2

As I understand it, when you wrap a servlet with a RequestDispatcher object and use the forward() method, you delegate that servlet to handle the request and produce the response. But what exactly is happening when you include an html page as an argument for getRequestDispatcher()?

This is from oracle:

RequestDispatcher getRequestDispatcher(java.lang.String path) Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path. A RequestDispatcher object can be used to forward a request to the resource or to include the resource in a response.

https://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getRequestDispatcher(java.lang.String)

So where in the http response is this resource contained, how does the page receiving the response know to navigate to this page? And if the request was sent with AJAX, will the rest of the JS within that page be executed before redirecting to the new page? (i.e. If I'm writing a script that stores data from the response in a session, will this execute before the client navigates to another page?)

qjonn
  • 31
  • 1
  • 3

1 Answers1

0

forward() means pass the request and response object to another servlet method.

The next servlet has 3 choices:

  1. fill the response and send it to the client (even if the client has tried to use the previous servlet, it will receive the answer from that last servlet)
  2. Make a redirect, which means that he sends to the client a message that says, please ask another url in order to be served.
  3. Make another forward to another servlet, where the cycle starts again.

As you see the point of the servlet is to write something to the response that the client is going to receive. What happens with forward() is that the state of the response and request object is not lost when the control moves to the next servlet. This means that the 1st servlet can write something to the request object and then the 2nd servlet can write something new or edit something on the same request object.

For response object be careful though. You can write to the response by getting a reference to the writer by response.getWritter() . If the 1st servlet writes something using that writter the client will receive that message even if the control continues to move to the 2nd servlet. So you normally edit only the request object in the 1st servlet and then the 2nd servlet writes to the response object.

If you write an html code the client will receive an html response. If the client uses a browser it will render a web page.

Html code will not be packed as a resource file on the response. Html code must be written on that response object like every other field of an object that you write in java.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
  • But what happens if you include the path to the html file itself? i.e. request.getRequestDispatcher("/index.html").forward(request, response)? – qjonn Mar 12 '21 at 00:22
  • it will not function like you think it will. getRequestDispatcher("/index.html") expects a path as a parameter. So it will find the servlet that is going to handle that path `/index.html` and then will forward the request to that servlet – Panagiotis Bougioukos Mar 12 '21 at 09:38