?
Solved

forward question

Posted on 2006-06-25
9
Medium Priority
?
305 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

The following information will get you familiar with your new DV server, including the (mt) Account Center, the Plesk Control Panel, our world-renowned support department and the rest of the (mt) tools that come with your new service.
Cron is one of the most popular and basic utilities found on Unix systems. Combined with other tools, cron makes it exceptionally easy to automate a broad range of tasks on your server.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Suggested Courses

615 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