[Question] What's the difference between "redirect" & "forward" in servelt?
2005-05-25 22:57
351 查看
Question:
What's the difference between "redirect" & "forward" in servelt?
Answer:
Suppose you have a servlet that just does another redirect to another URL:
This servlet is accessible through the URL /redirectServlet.
When the client browser makes a request for /redirectServlet, it receives a response (an HTTP 302 response) that tells it that the document it requested can actually be found at /anotherURL. The client uses the URL it gets and makes a new request to /anotherURL.
The redirect URL doesn't have to be on the same server as the original URL. If the redirect URL (/anotherURL) points to another servlet, new request and response objects will be created to handle the new request.
Suppose our redirect servlet is now changed to do a forward instead of a redirect:
This modified servlet is accessible through the URL /forwardServlet.
When the client makes a request to our forwarding servlet this time, it no longer receives the redirect response. The dispatcher.forward line causes the servlet container to run the servlet associated with the url /anotherURL directly. The client will receive the result of /anotherURL, even though they requested /forwardServlet.
A few more notes:
* Redirect is a two step process. The web server tells the browser to request a second URL.
* If the user reloads, the second URL will be reloaded (/anotherURL instead of /redirectServlet).
* Redirect is always slower than a forward because it requires a second client request.
* Attributes placed in the request scope of the redirecting servlet are not available in the request scope of the second rrequest.
* Forwards are performed internally to the web server.
* The browser is completely unaware that the forward took place - the original URL (/forwardServlet) remains in the address bar.
* If the user does a reload, the browser will repeat the original request with the original URL (/forwardServlet).
What's the difference between "redirect" & "forward" in servelt?
Answer:
Suppose you have a servlet that just does another redirect to another URL:
protected void doGet(HttpServletRequest request, HttpServletResponse response) { response.sendRedirect("/anotherURL"); }
This servlet is accessible through the URL /redirectServlet.
When the client browser makes a request for /redirectServlet, it receives a response (an HTTP 302 response) that tells it that the document it requested can actually be found at /anotherURL. The client uses the URL it gets and makes a new request to /anotherURL.
The redirect URL doesn't have to be on the same server as the original URL. If the redirect URL (/anotherURL) points to another servlet, new request and response objects will be created to handle the new request.
Suppose our redirect servlet is now changed to do a forward instead of a redirect:
protected void doGet(HttpServletRequest request, HttpServletResponse response) { ServletContext context = getServletContext(); RequestDispatcher dispatcher = context.getRequestDispatcher("/anotherURL"); dispatcher.forward(request, response); }
This modified servlet is accessible through the URL /forwardServlet.
When the client makes a request to our forwarding servlet this time, it no longer receives the redirect response. The dispatcher.forward line causes the servlet container to run the servlet associated with the url /anotherURL directly. The client will receive the result of /anotherURL, even though they requested /forwardServlet.
A few more notes:
* Redirect is a two step process. The web server tells the browser to request a second URL.
* If the user reloads, the second URL will be reloaded (/anotherURL instead of /redirectServlet).
* Redirect is always slower than a forward because it requires a second client request.
* Attributes placed in the request scope of the redirecting servlet are not available in the request scope of the second rrequest.
* Forwards are performed internally to the web server.
* The browser is completely unaware that the forward took place - the original URL (/forwardServlet) remains in the address bar.
* If the user does a reload, the browser will repeat the original request with the original URL (/forwardServlet).
相关文章推荐
- What is the difference between a = a * (2^3) and a = a << 3?
- What is the difference between getWidth/heigth() and getMeasuredWidth/Heigth() in Android SDK?
- Whats is the difference between train, validation and test set, in neural networks?
- What is the difference between Views and Materialized Views in Oracle?
- whats is the difference between train, validation and test set, in neural networks?
- What are the differences between a pointer variable and a reference variable in C++?
- What Is the Difference Between a Block, a Proc, and a Lambda in Ruby
- what is the difference between definition and declaration in c
- Sql 中having 和where的区别 SQL hardest question What is the difference between the WHERE and HAVING claus
- In PHP5, what is the difference between using self and $this? When is each appropriate?
- What exactly is the difference between “pass by reference” in C and in C++?
- What is the Difference Between Type and Class in
- Java-What is the difference between <? super E> and <? extends E>?
- What is the difference between a soft reference and a weak reference in Java?
- What is the difference between wc -1 < mydata.dat and wc -1 < mydata.dat ?
- What are the differences between a pointer variable and a reference variable in C++?
- What is the difference between SVD and matrix factorization in context of recommendation engine?
- What is the difference between angle bracket < > and double quotes " " while including header files
- what is the difference between "isempty"and "isnull"in the qt bool QString::isEmpty () const Retur.
- what is the difference between _source and _all in Elasticsearch