File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes forward method in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "forward method in jsp" Watch "forward method in jsp" New topic

forward method in jsp


Joined: Jan 25, 2001
Posts: 26
Is forward method in pagecontext is supposed to append to response object ???

Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1183
I'm not sure I understand exactly what you asked, but this code snippet shows how to forward a request.

public void service(HttpServletRequest req, HttpServletResponse res)
throws IOException


RequestDispatcher rd =

try {
} catch (ServletException e) {}


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
it does foward the response object

Joined: Jan 25, 2001
Posts: 26
i have two jsp - 1.jsp and 2.jsp
out.println("b4 forward");
out.println("after forward");
out.println("in 2.jsp");

what will be the output if i run 1.jsp
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If you are lucky, and you have not committed the response (by overflowing the output buffer), you will get a single line of output:
"in 2.jsp"

If the forwarding page was buffered, the buffer is cleared prior to the forward (so say goodbye to "in 1.jsp"). If the page is unbuffered, or you've overflowed the buffer, you will get an IllegalStateException when you try to forward.

You will never see "after forward" because a forward does not return control to the forwarding page. Including a page does though, and does not have the buffering problem either.

And here is the code to forward:
[This message has been edited by Mike Curwen (edited October 17, 2001).]
I agree. Here's the link:
subject: forward method in jsp
It's not a secret anymore!