what is sendredirect method in servlet


What is sendRedirect method in servlet

The sendRedirect() method of HttpServletResponse interface is used to redirect response to another resource, this resource may be Servlet, JSP or HTML file.

sendRedirect() method is used to redirect client request to some other location for further processing.

The response which is delegated by sendRedirect() request is visible in browser as a new request or fresh request.Some time this is also called as client side redirect.

sendRedirect() method is able to accept relative URL so that it can go for resource inside or outside of server.

Drawbacks of sendRedirect()

  1. The sendRedirect() method is slower because when new request will be created every time.
  2. The sendRedirect() method is provided under HTTP so it can be used only with HTTP clients only.

Points to Remember

  1. sendRedirect() will always send a new request.
  2. It can be used within same server or different server.

Example

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class MyServlet extends HttpServlet {

   protected void doPost(HttpServletRequest request,
                    HttpServletResponse response)
          throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        try { 
            response.sendRedirect("www.javagf.com");
        }finally {            
            out.close();
        }
    }






Full-stack web and mobile app development company Contact Us : Skype: indore.webcoder Email: info@w3sparks.com Sales: +91 8085506229