Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher  RSS feed

 
Raj Maheshwari
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying some stuff with the RequestDispatcher to generate a page and am having a few problems (and I think it might be because I dont understand RequestDispatcher) completely, but here is what I'm doing:

public void doGet(HttpServletRequest req, HttpServletResponse resp){

String url="/BaseServlet";

ServletContext sc = getServletContext();

RequestDispatcher rd = sc.getRequestDispatcher(url);
resp.setAttribute(myAttbr,"HEADER");
rd.forward(req, res);
resp.setAttribute(myAttbr,"FOOTER");
}

When I hit my servlet, only the response from the first call to BaseServlet (where I passed in the attribute HEADER) gets displayed on the page. The second call does not. Why is this, and how can I get both the calls to be displayed?

thanks
Raj
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your servlet will not compile because myAttbr is not defined.

how can I get both the calls to be displayed


I'm not at all sure what you mean by "calls".

If you mean the scoped variables created by the calls to setAttribute, then you need to give them different names. In your code, they will both be named whatever "myAttbr" evaluates to and the 2nd will replace the first.

Also, you should never do anythig but return after a call to the request dispatcher forward method.

[ March 17, 2006: Message edited by: Bear Bibeault ]
[ March 17, 2006: Message edited by: Bear Bibeault ]
 
Kino Lobo
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Once you foward the response there will be no more lines executed on your
servlet!
Try it, and check by yourself;
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kinildson Gessio Persegueiro:
Once you foward the response there will be no more lines executed on your
servlet!


That is incorrect. Servlets are java classes just like any other, and follow the same rules. There is no magic about the forward() method. When control returns from the forward() method, any code after it will be executed. The only case where this will not be true is if an exception is thrown.

Conventionally, code after the forward is discouraged since it's a dangerous and error-prone thing to do (as the response has been comitted), but any code there is not magically removed.
[ March 17, 2006: Message edited by: Bear Bibeault ]
 
dema rogatkin
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread.stop(0 is rising an exception, however I am not sure about System.exit()
 
Raj Maheshwari
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

Thanks a lot for the replies. So I figured out my problem. In my BaseServlet I was doing an out.flush() and out.close(). I commented it out and stuff works as expected.

And yes, the control returns to the next statement after every call to rd.forward(). I tried debugging it and it works.

Thanks
Raj
 
geeta lalchandani
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Servlets are java classes just like any other, and follow the same rules. There is no magic about the forward() method. When control returns from the forward() method, any code after it will be executed.


Bear... as per my understanding.. forward takes you to the dispatcher specified jsp.
it is as gud as a new request...
so in ths case.. it should not excute the code after the forward statement...

i tried this... and the only code i can see is that in the forwarded jsp
you can look at the code..


and the contents on only the 'includedJsp.jsp' are seen..
nothing in the above jsp is visible
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear... as per my understanding.. forward takes you to the dispatcher specified jsp.
it is as gud as a new request...
so in ths case.. it should not excute the code after the forward statement...


Your understanding is incorrect.
Try putting a System.out.println("Hello"); line just after your forward statement. Run it and check your logs.

As a rule of thumb, I always put a return; statement after a forward or sendRedirect call (unless I actually want some cleanup code to be run).
 
geeta lalchandani
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in taht case, whats in the including jsp after forwarding.. should have been on the browser
 
geeta lalchandani
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben you are correct. I put a system.out.print statement, after forward and it is print
but the browser only displays the contents of the jsp it is forwarded to.. not the jsp taht is forwarding ... why is taht so?
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!