0% found this document useful (0 votes)
48 views1 page

Introduction To Sendredirect Method: Sendredirect and Request Dispatcher

The sendRedirect() method redirects the client's browser to a new resource by having the client make a new request. It accepts relative URLs so it can redirect to resources inside or outside the server. The main difference between redirection and request dispatching is that redirection causes the client to make a new request, changing the URL in the browser, while dispatching serves the resource in the same request without changing the URL. An example demonstrates using sendRedirect() to redirect from one servlet to an external website.

Uploaded by

ajaychakku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views1 page

Introduction To Sendredirect Method: Sendredirect and Request Dispatcher

The sendRedirect() method redirects the client's browser to a new resource by having the client make a new request. It accepts relative URLs so it can redirect to resources inside or outside the server. The main difference between redirection and request dispatching is that redirection causes the client to make a new request, changing the URL in the browser, while dispatching serves the resource in the same request without changing the URL. An example demonstrates using sendRedirect() to redirect from one servlet to an external website.

Uploaded by

ajaychakku
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

03/05/2016 Using sendRedirect() Method | Servlet Tutorial | Studytonight

Introduction to sendRedirect() Method
sendRedirect()  method redirects the response to another resource. This method actually makes the
client(browser) to create a new request to get to the resource. The client can see the new url in the browser.

sendRedirect() accepts relative URL, so it can go for resources inside or outside the server.

sendRedirect() and Request Dispatcher

The main difference between a redirection and a request dispatching is that, redirection makes the
client(browser) create a new request to get to the resource, the user can see the new URL while request
dispatch get the resource in same request and URL does not changes.

Also, another very important difference is that, sendRedirect() works on responseobject while request dispatch
work on request object.

Example demonstrating usage of sendRedirect()

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
 
public class MyServlet extends HttpServlet { 
 
   protected void doGet(HttpServletRequest request, HttpServletResponse response) 
          throws ServletException, IOException { 
        response.setContentType("text/html;charset=UTF‐8"); 
        PrintWriter out = response.getWriter(); 
        try {  
            response.sendRedirect("http://www.studytonight.com"); 
        }finally {             
            out.close(); 
        } 
    } 

http://www.studytonight.com/servlet/sendredirect­method 1/1

You might also like