Dispatching Requests
First, call the getRequestDispatcher method
of ServletContext
Supply a URL relative to the Web application root
Example
String url = "/presentations/presentation1.jsp";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
Second
Call
forward
to completely transfer control
to destination page. See following example
Call include to insert output of destination page
and then continue on. See book.
105
JSP
www.corewebprogramming.com
Forwarding Requests:
Example Code
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String operation = request.getParameter("operation");
if (operation == null) {
operation = "unknown";
}
if (operation.equals("operation1")) {
gotoPage("/operations/presentation1.jsp",
request, response);
} else if (operation.equals("operation2")) {
gotoPage("/operations/presentation2.jsp",
request, response);
} else {
gotoPage("/operations/unknownRequestHandler.jsp",
request, response);
}
}
106
JSP
www.corewebprogramming.com
53