❮ Previous Next ❯

sendRedirect() Method

sendRedirect() is a such type of method which redirects the response to another resource. This method 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.

The main difference between a redirect and a request dispatching is that, redirect method makes the client(browser) create a new request to get to the resource, the user can see the new URL while request dispatching get the resource in same request and URL does not changes. Also, another very important difference is that, sendRedirect() works on response object while request dispatch work on request object.

Let's see a demo program:

When to use sendRedirect() method?

MyServletDemo.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class MyServletDemo extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html”);
PrintWriter pw = response.getWriter();
try {
response.sendRedirect("http://www.java8s.com");
}finally {
pw.close();
}
}
}
❮ Previous Next ❯

Follow Us





  Tutorials

Popular Links

Contact Us

address Plot No-741,2ND Floor
Opp. Bhagabati Temple,Jayadev Vihar
      Bhubaneswar-751013
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved