Sun Java System Web Server 7.0 Developer's Guide to Java Web Applications

Creating a JSP Response Page

Servlets can invoke JSP files in two ways, the include() method and the forward() method.


RequestDispatcher dispatcher =
       getServletContext().getRequestDispatcher("JSP_URI");
 dispatcher.include(request, response);
   ... //processing continues

Note –

You cannot use the forward() method if you have already defined a PrintWriter or ServletOutputStream object.


This example shows a JSP page using forward():


RequestDispatcher dispatcher =
       getServletContext().getRequestDispatcher("JSP_URI");
 dispatcher.forward(request, response);

You identify which JSP noun to call by specifying a Universal Resource Identifier (URI). The path is a String describing a path within the ServletContext scope. You can also use the getRequestDispatcher() method in the request object that takes a String argument indicating a complete path.


Note –

Identify


RequestDispatcher dispatcher = req.getRequestDispatcher(“foo/bar”);


Note –

In the previous example getRequestDispatcher does not start with “/”. In Web Server 6.1, this syntax resulted in a URI such as /webapp/foo/bar. In Sun Java System Web Server 7.0, the URI appears as /webapp/current_servlet_path/foo/bar


For more information about JSP pages, see Chapter 5, Developing JavaServer Pages.