Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forward method in jsp

 
GK
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is forward method in pagecontext is supposed to append to response object ???

Gunjan
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 =
req.getRequestDispatcher("hello.jsp");

try {
rd.forward(req,res);
} catch (ServletException e) {}


}
Cheers,
Dan
 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it does foward the response object
 
GK
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have two jsp - 1.jsp and 2.jsp
1.jsp
-----
out.println("b4 forward");
pageContext("2.jsp");
out.println("after forward");
2.jsp
-----
out.println("in 2.jsp");

what will be the output if i run 1.jsp
Gunjan
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic