# Page Navigation Techniques

## 2 Ways

### 1. Client Pull

* **1.1 Client Pull**
  * Taking the client to the next page in the NEXT request
    * User takes some action
      * eg : clicking on a button or link & then client browser generates new URL to take user to the next page.
* **1.2 Redirect Scenario**
  * User doesn't take any action. Client browser automatically generates new URL to take user to the next page. _(next page can be from same web application, or different web application on same server or any web page on any server)._
  * Here a temporary redirection happens, web container creates a response such that,
    * body: Empty _(printwriter buffer emptied before redirection)._
    * location header: Location of the next page.
    * status code: 302
    * Cookie header: name: ..., value...(If any)
  * Client browser on getting the `302` status code redirects the client to the location based on location header, on next request-response cycle. This newly created request by the client browser will have,
    * Cookie: name...,value...
    * method: GET _(default http method)_ _Note: URL on client browser URL bar will change as per location on the location header received in previous response._

```java
    //API of HttpServletResponse
    public void sendRedirect(String redirectURL)

    //eg : For redirecting client from Servlet1('/s1') to Servlet2('/s2'), use
    //written ins /s1 servlet page
    response.sendRedirect("s2");

```

_Note: If the response already has been committed(printwriter flushed or closed), this method throws(WC) an IllegalStateException.(since WC can't redirect the client after response is already committed)._

_Note: If session is not created before flushing or committing the response, then it throws IllegalStateException ._

### 2. Server Pull _(Request Dispatching)_

* Taking the client to the next page in the same request. Also known as resource chaining or request dispatching technique.
* Client sends the request to the servlet/JSP. Same request can be chained to the next page for further servicing of the request.

1. Forward Scenario

*   Steps

    * Create Request Dispatcher object for wrapping the next page resource, which can be static or dynamic.

    ```java
        //API of ServletRequest
        javax.servlet.RequestDispatcher getRequestDispatcher(String path)
    ```

    ```java
        //API of RequestDispatcher
        public void forward(ServletRequest rq,ServletResponse rs) throws ServletException, IOException, IllegalStateException 
    ```

    * This method allows one servlet to do initial processing of a request and another resource to generate the response. _(i.e division of responsibility)_
    * Uncommitted output in the response buffer is automatically cleared before the forward just like send redirect scenario.
    * If the response already has been committed _(by flushing the print writer or closing)_, this method throws an IllegalStateException as client cannot be forwarded after commiting the response.
    * If a post request is received from the client to server, the `forward()` scenario will forward to the next resource and call the next resource's `doPost()` method. Like wise for other http methods.

    ![RD-forward](https://574639531-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJDnjsv52I3fJ56WbLgHu%2Fuploads%2Fgit-blob-ee78d76f6d314d1b3a5e12e1fe9ab5b288a1ff46%2FRD_forward.png?alt=media)

    * Limitation
      * Only last page in the chain can generate dynamic response.

1. Include scenario

* Same as above, get a request dispatcher object from the request object and then on that call the `include()` method.

```java
    // API of RequestDispatcher
    public void include(ServletRequest rq,ServletResponse rs) throws ServletException, IOException
```

* Includes the content of a resource @run time (servlet, JSP page, HTML file) in the response. -- server-side includes.
* If the response already has been committed _(by flushing the print writer)_, the container doesn't need to clear the print writer buffer, rather when `flush` is called, the container just sends the reponse. It doesn't close the buffer and further responses get added to this buffer. Hence, no `IllegalStateException` is encountered.
* If a post request is received from the client to server, the `forward()` scenario will forward to the next resource and call the next resource's `doPost()` method. Like wise for other http methods.
* Limitation
  * The included servlet/JSP cannot change the response status code or set headers; any attempt to make these changes are ignored.

![include-scenario](https://574639531-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJDnjsv52I3fJ56WbLgHu%2Fuploads%2Fgit-blob-8b879df500a27ebb6a772ab17c06bff1220210ac%2FRD_include.png?alt=media)

## Forward v/s include

![](https://574639531-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FJDnjsv52I3fJ56WbLgHu%2Fuploads%2Fgit-blob-aabfa2f2757be9be8ffc2bc1eabe49071a7ee822%2Fpage-navigation-techs.jpg?alt=media)
