What is the difference between response.sendRedirect() and request.getRequestDispatcher().forward(request,response)

roger

I have got a problem with my page jump when I use JAVA, if I use:

response.sendRedirect("login.jsp")

then I get this url: http://localhost:8080/login.jsp

But if I use

request.getRequestDispathcer("login.jsp").forward(request, response)

then I get this url: http://localhost:8080/Shopping/login.jsp (the "Shopping" is the name of my module).

What's the difference?

Keerthivasan

To simply explain the difference,

  response.sendRedirect("login.jsp");

doesn't prepend the contextpath (refers to the application/module in which the servlet is bundled)

but, whereas

 request.getRequestDispathcer("login.jsp").forward(request, response);

will prepend the contextpath of the respective application

Furthermore, Redirect request is used to redirect to resources to different servers or domains. This transfer of control task is delegated to the browser by the container. That is, the redirect sends a header back to the browser / client. This header contains the resource url to be redirected by the browser. Then the browser initiates a new request to the given url.

Forward request is used to forward to resources available within the server from where the call is made. This transfer of control is done by the container internally and browser / client is not involved.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

response.sendRedirect Vs getRequestDispatcher

From Dev

what's the difference between request.on('error') and response.on('error')

From Dev

response.sendRedirect does not preserve request attributes?

From Dev

Difference between Event Aggregator, Commands and Request/Response

From Dev

Difference between GET and POST in an HTTP response (not request)

From Dev

Difference between GET and POST in an HTTP response (not request)

From Dev

What is the difference between interrupt latency and interrupt response?

From Dev

response sendRedirect() processing

From Dev

JSTL with response.sendRedirect

From Dev

response.sendRedirect() shows error

From Dev

What is the difference between ScriptManager.RegisterClientScriptBlock and Response.Write?

From Dev

What is the difference between returning a response object and echo output?

From Dev

What's the difference between response.body() and object returned by the callback?

From Dev

What is the difference between type = "response" and type = "scores" in R

From Dev

What is the difference between Responce.Redirect and Response.RedirectLocation?

From Dev

What is the difference between ScriptManager.RegisterClientScriptBlock and Response.Write?

From Dev

What is the difference between returning a response object and echo output?

From Dev

What's the difference between console.log and response.write?

From Dev

Difference between created response in Silverstripe

From Dev

What is the difference between a request payload and request body?

From Dev

What is the difference between a request payload and request body?

From Dev

What is the http request and response body for this URL request?

From Dev

response.sendredirect not working in external application

From Dev

Why "response.sendRedirect" doesn't work?

From Dev

Understanding the difference between "Response Headers" and "Request Headers" in Firefox Developer Console (network tab) for image cache control

From Dev

Understanding the difference between "Response Headers" and "Request Headers" in Firefox Developer Console (network tab) for image cache control

From Dev

Restful - same request but difference response on different scenarios

From Dev

Difference between response.setHeader and response.writeHead?

From Dev

Difference between response.send and response.write in node js

Related Related

  1. 1

    response.sendRedirect Vs getRequestDispatcher

  2. 2

    what's the difference between request.on('error') and response.on('error')

  3. 3

    response.sendRedirect does not preserve request attributes?

  4. 4

    Difference between Event Aggregator, Commands and Request/Response

  5. 5

    Difference between GET and POST in an HTTP response (not request)

  6. 6

    Difference between GET and POST in an HTTP response (not request)

  7. 7

    What is the difference between interrupt latency and interrupt response?

  8. 8

    response sendRedirect() processing

  9. 9

    JSTL with response.sendRedirect

  10. 10

    response.sendRedirect() shows error

  11. 11

    What is the difference between ScriptManager.RegisterClientScriptBlock and Response.Write?

  12. 12

    What is the difference between returning a response object and echo output?

  13. 13

    What's the difference between response.body() and object returned by the callback?

  14. 14

    What is the difference between type = "response" and type = "scores" in R

  15. 15

    What is the difference between Responce.Redirect and Response.RedirectLocation?

  16. 16

    What is the difference between ScriptManager.RegisterClientScriptBlock and Response.Write?

  17. 17

    What is the difference between returning a response object and echo output?

  18. 18

    What's the difference between console.log and response.write?

  19. 19

    Difference between created response in Silverstripe

  20. 20

    What is the difference between a request payload and request body?

  21. 21

    What is the difference between a request payload and request body?

  22. 22

    What is the http request and response body for this URL request?

  23. 23

    response.sendredirect not working in external application

  24. 24

    Why "response.sendRedirect" doesn't work?

  25. 25

    Understanding the difference between "Response Headers" and "Request Headers" in Firefox Developer Console (network tab) for image cache control

  26. 26

    Understanding the difference between "Response Headers" and "Request Headers" in Firefox Developer Console (network tab) for image cache control

  27. 27

    Restful - same request but difference response on different scenarios

  28. 28

    Difference between response.setHeader and response.writeHead?

  29. 29

    Difference between response.send and response.write in node js

HotTag

Archive