Java Tutorial/Servlet/Forward
Forward Call from a Servlet
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Main extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher dispatcher = request.getRequestDispatcher("/p.jsp");
dispatcher.forward(request, response);
}
}
Forward to another location
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class MyServlet extends HttpServlet {
static final String NEW_HOST = "http://www.jexp.ru";
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String newLocation = NEW_HOST;
res.setHeader("Refresh", "10; URL=" + newLocation);
out.println("The requested URI has been moved to a different host.<BR>");
out.println("Its new location is " + newLocation + "<BR>");
out.println("Your browser will take you there in 10 seconds.");
}
}