Redirect to New Location : Redirect « Servlets « Java






Redirect to New Location

 
/*
 *     <servlet>
        <servlet-name>RedirectNewLocation</servlet-name>
        <servlet-class>RedirectNewLocation</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>RedirectNewLocation</servlet-name>
        <url-pattern>/RedirectNewLocation</url-pattern>
    </servlet-mapping>

 * */

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 RedirectNewLocation extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    PrintWriter out = response.getWriter();

    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
    response.setHeader("Location", "http://www.java2s.com");

    response.setContentType("text/html");
    return;
  }
}

           
         
  








Related examples in the same category

1.Servlet redirect
2.Servlet: URL redirect
3.Servlet: URL rewrite
4.Redirect Servlet Call to Another URL
5.Forward Call from a Servlet
6.Client auto refresh