SendRedirect in servlet

SendRedirect in servlet:

The sendRedirect() method ofHttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file.

It accepts relative as well as absolute URL.

It works at client side because it uses the url bar of the browser to make another request. So, it can work inside and outside the server.

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class DemoServlet extends HttpServlet{

public void doGet(HttpServletRequest req,HttpServletResponse res)

throws ServletException,IOException

{

res.setContentType(“text/html”);

PrintWriter pw=res.getWriter();

response.sendRedirect(“http://www.google.com”);

pw.close();

}}

Leave a Reply

Your email address will not be published. Required fields are marked *