Solved

forward question

Posted on 2006-06-25
9
295 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
logging jar 1 110
tomcat users xml 7 119
love6 challenge java 31 133
using if condition without JSTL 2 108
This tutorial shows how to create a greeting card by combining two image layers and a text layer on a PC using a free image editing app.
Employees depend heavily on their PCs, and new threats like ransomware make it even more critical to protect their important data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

770 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