• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

confustion regarding forward

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two jsp files

The first file is baseFile.jsp



The second file is includeFile.jsp



When I am using http://localhost:8080/mywebapplication/baseFile.jsp

only the contents of includeFile are printed,
there is one line before the pageContext.forward("includeFile.jsp") is called
but neither of them are printed

The final output is

This line is in includeFile

Why other two lines are not getting printed?
Please clarify my doubt.
[ September 21, 2007: Message edited by: paritosh ranjan ]
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The forward() method does what it says, it 'forwards' the control to the resource given to it as a parameter i.e., the forwarding resource stops whatever it's doing and the resource being forwarded to is executed. The control is not returned to the forwarding resource.

Implications:
[1]. Any code after the forward call will not be executed.
[2]. Any output buffered before the forward call will be cleared ( and hence will not be visible at the output )
[3]. As a side-effect of [2], if the buffer has been flushed and the output has been 'committed', the forward call will cause an IllegalStateException to be thrown. This is because you've already given the client some response, but now you want to give control to another component and give another response which isn't allowed.

So, in your case, obviously [3] isn't happening, since your output hasn't filled up the buffer yet. So, [2] is the reason why your output isn't visible, it is cleared when forward() is called.

And you don't see the line after the forward call because of [1].

It seems to me what you really need is include(); this would 'include' the response from the included resource by first executing up to the call, and then giving control to the included resource and then restoring control to the next line, after the include(). This way, all three lines would get printed.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tarun Yadav:

[1]. Any code after the forward call will not be executed.



That part is not true.
A call to forward does not stop execution of the method that called it.
 
Tarun Yadav
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ben Souther:


That part is not true.
A call to forward does not stop execution of the method that called it.



It isn't? But I thought that was how it worked; a forward hands over control to the forwarded resource and everything else after the call is skipped. This how I've seen it explained and even in the Head First book, there is an example which shows a conditional forward and points out that nothing will show up after the forward call if it was valid ( Page 410 ).

So what happens exactly then? Could you elaborate? Perhaps an example? In any case, you can't do anything with the response since that would throw an exception. Do you mean that apart from that you can have something else happen in the method?
 
Sheriff
Posts: 28333
97
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tarun Yadav:
So what happens exactly then? Could you elaborate? Perhaps an example? In any case, you can't do anything with the response since that would throw an exception. Do you mean that apart from that you can have something else happen in the method?

What happens exactly? Well, it's Java code. So after one line of code is executed, the next line of code is executed. It's still Java even though it's being run in a servlet container. Don't let that distract you; it's just Java code and follows the same rules as any other Java code.

So in particular, if you want to do something after you call the forward() method, go ahead and do that. But as you say, you can't do anything with the response.
 
Tarun Yadav
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Clapham:
What happens exactly? Well, it's Java code. So after one line of code is executed, the next line of code is executed. It's still Java even though it's being run in a servlet container. Don't let that distract you; it's just Java code and follows the same rules as any other Java code.

So in particular, if you want to do something after you call the forward() method, go ahead and do that. But as you say, you can't do anything with the response.



Right, that clears it up. It's so easy to lose sight of the basics with all this stuff sometimes Thanks
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want print the other two lines,just use include method.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"LuckyPig Ray",
Please check your private messages.
 
paritosh ranjan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As any output buffered before the call is cleared.

How will I achieve the functionality of redirecting the user to different pages depending on the user input?

I think this code will make clear what I want to achieve.



I want to redirect the user to different pages according to user input.
However, the page is redirected without even asking the user input(I think the reason is that the buffered output is cleared before the forward).

So, how will I achieve that functionality.
 
Tarun Yadav
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by paritosh ranjan:
As any output buffered before the call is cleared.

How will I achieve the functionality of redirecting the user to different pages depending on the user input?

I think this code will make clear what I want to achieve.



I want to redirect the user to different pages according to user input.
However, the page is redirected without even asking the user input(I think the reason is that the buffered output is cleared before the forward).

So, how will I achieve that functionality.



You seem to be a little confused about how this works. Your JSP/ Servlet code executes on the server side, before the client ever gets to see the output, the user input is on the client side after all your JSP/ Servlet code has finished executing. You've seen what happens when you put your forward() call on the same page as the input fields. You need to wait for the user to give input and then make a decision.

You could probably think of creating this page w/o the forwards and then submit it to a servlet which will then make the decision. Basically, you need to split your input and forward() calls into two separate resources.

Or, if you must have it on the client side and in one JSP, you should look into JavaScript. But that won't give you the functionality of forward() or include(); only redirects and submits.
 
paritosh ranjan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Tarun,

Thanks a lot,that client side stuff really helped me to understand the problem.

Thanks again,

Paritosh
 
Tarun Yadav
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps you may also find this article helpful.
 
paritosh ranjan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tarun

Its not necessary to split input and forward in different resources.
The whole thing can be encapsulated in a single jsp page by applying a little bit of logic.

See this code:
I used the same jsp page for taking input and forwarding.




Bear

Thanks for the article.
 
Tarun Yadav
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by paritosh ranjan:
Tarun

Its not necessary to split input and forward in different resources.
The whole thing can be encapsulated in a single jsp page by applying a little bit of logic.

See this code:
I used the same jsp page for taking input and forwarding.




Bear

Thanks for the article.



Oh, of course it can, I was just trying to keep it simple

Except, I don't think this is a very good way of doing it, you should keep your JSP for display only ( it's the View in MVC right? ) and put your business logic and redirections in a servlet or Action classes ( if you're using Struts ).
 
paritosh ranjan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK

I got it.
 
author & internet detective
Posts: 42027
916
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jack,
Now you are halfway there - certainly better than "LuckyPigRay". Please add a last name as well.

Thanks
Jeanne
JavaRanch Sheriff
 
Won't you be my neighbor? - Fred Rogers. tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic