Solved

forward question

Posted on 2006-06-25
9
298 Views
Last Modified: 2010-04-01
if I have this :

[...]
      instruction1;
      method   (request,response,session);
      instruction2;
[...]
   


      public void   method   (HttpServletRequest request, HttpServletResponse response, HttpSession session)
      throws IOException, ServletException{

            request.getRequestDispatcher(urlPage0).forward(request,response);
      }


After instruction 1 I do a forward.
Will instruction2 be executed or not ?
(What happen when I forward?)
0
Comment
Question by:matthew016
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
9 Comments
 
LVL 4

Expert Comment

by:evnafets
ID: 16979855
What do YOU think?
Have you tried it?
It is easy enough to write a program that checks:

System.out.println("instruction1");
method(request, response, session);
System.out.println("instruction2");

Run that code as part of a servlet.  See what it prints out to the console/log.  Your question will be answered.

This smells like a homework question to me.  

0
 
LVL 9

Author Comment

by:matthew016
ID: 16979989
No I tested it and the instruction is executed,
but I have the code from an educational example,
and this example doesnt run because it supposes the instructions arent executed .
So I tought something was wrong with the IDE ...

So ... instruction2 is printed ?

0
 
LVL 9

Author Comment

by:matthew016
ID: 16982412
I found this when googleling

"The lines in the source JSP file after the <jsp:forward> element are not processed. "
(http://java.sun.com/products/jsp/tags/11/syntaxref11.fm9.html)

Well when I run it in Eclipse WTP the lines after it are executed,
that's why I get a NullPointerException somewhere when I run the educational example.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:Mr_Lenehan
ID: 16983331
Agree, smells like homework... if you were worried about it being executed or not you could just put instruction 2 before the method() and modify it to include an IF.
0
 
LVL 9

Author Comment

by:matthew016
ID: 16983349
.............

It's not homework, I'm not new to Java,

I just want to know what should happen when I do a forward,
I think u just don't know.

Well nevermind if u think I'm doing my homework.
0
 
LVL 9

Author Comment

by:matthew016
ID: 16983549
I ca
0
 
LVL 9

Author Comment

by:matthew016
ID: 16983557
I can read in the book
Sam - teach yourself JSP in 24 hours
"
You can transfer to another page without returning to the original. This technique is called forwarding. When you forward the request on to another page (or servlet), the forwarding page is no longer involved in handling the request.
"
And in the book : Advanced Java - Servlets And JSP
"
When RequestDispatchedr method forward is called, processing of the request by the current servlet terminates.
"


But when I execute this servlet instruction2; is executed.
Why ?
0
 
LVL 4

Accepted Solution

by:
evnafets earned 500 total points
ID: 16987549
What you have observed is correct.
You are executing java code.  You call a java method.  It calls the method and continues on down the program from that point.
That is what you would expect in any regular java program.
Why should calling the forward() method be different from calling any other java method?

> When you forward the request on to another page (or servlet), the forwarding page is no longer involved in handling the request.
What they have said here is technically true.  The forwarding page is no longer involved in handling the request.  Any output it has done to date is cancelled (if not already committed) and any other output it does will be ignored or create an error.  But that doesn't stop the code from running!

By the same token, calling response.sendRedirect() also does not halt execution of the current code.

After using requestDispatcher.forward() or response.sendRedirect() to pass the output responsibility off to another resource, you should make sure that your code does not do any more output, forwards or redirects, as it could create an IllegalStateException from the response already having been committed.
The easiest way to do in a servlet is to follow each forward/redirect with a "return" statement, or do it with an if/else statement.
ie
requestDispatcher.forward(.....)
return;

or

if (pageShouldForward){
  requestDispatcher.forward(...)
}
else {
  // continue output.
}


As an aside, the <jsp:forward> tag DOES halt execution of the page after that point.  The backing code for the tag passes control to the target page (using RequestDispatcher) and then halts processing on the current page. (just like any custom tag can).  It is a seperate case to the request dispatcher.

Sorry for being a bit snippy with you in that first response, but as I said, it was very easy to test.  It looked like you were just after an easy answer without doing any investigation yourself.  I'm of the school of thought that you learn by doing, and finding out yourself, rather than just asking for answers.  
With your second post you showed that you HAD done some work, and were confused by the results as they were not what you expected.  Hence my response now.

Hope this explains things,
evnafets
0
 
LVL 9

Author Comment

by:matthew016
ID: 16988762
Thank you for your answer
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
Invest in your employees with these five simple steps to improve employee engagement and retention.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question