Solved

forward question

Posted on 2006-06-25
9
293 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 2

Expert Comment

by:Mr_Lenehan
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 9

Author Comment

by:matthew016
Comment Utility
.............

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
Comment Utility
I ca
0
 
LVL 9

Author Comment

by:matthew016
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for your answer
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

This article explains in simple steps how to renew expiring Exchange Server Internal Transport Certificate.
HOW TO: Upload an ISO image to a VMware datastore for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere Host Client, and checking its MD5 checksum signature is correct.  It's a good idea to compare checksums, because many installat…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now