JAVA Servlet Program to implement sendRedirect() method(using HTTP Servlet Class)
Posted by MohammadYusuf
Last Updated: October 28, 2016
  799

a. sendredirect.java File

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class sendredirect extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.sendRedirect("sendredirect2");
}
}


b. sendredirecct2.java File
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class sendredirect2 extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html><title>SendRedirect Demo</title>");
pw.println("<body>");
pw.println("<b><h3>Please Notice Your URL has changed</h3></b><br />");
pw.println("<h3>Response generated by 2nd servlet</h3><br />");
pw.println("<b><h3>Welcome</h3></b>");
pw.println("</body></html>");
pw.close();
}
}