forward question

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?)
LVL 9
matthew016Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
evnafetsConnect With a Mentor Commented:
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
 
evnafetsCommented:
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
 
matthew016Author Commented:
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
matthew016Author Commented:
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
 
Mr_LenehanCommented:
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
 
matthew016Author Commented:
.............

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
 
matthew016Author Commented:
I ca
0
 
matthew016Author Commented:
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
 
matthew016Author Commented:
Thank you for your answer
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.