aspose file tools*
The moose likes Servlets and the fly likes Can we define multiple forward on single servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can we define multiple forward on single servlet?" Watch "Can we define multiple forward on single servlet?" New topic
Author

Can we define multiple forward on single servlet?

ronney singh
Ranch Hand

Joined: Feb 10, 2012
Posts: 40

What I am trying to do is defining one forward inside IF statement and one after that statement ended.
But it is not working. Please tell me why?
When I am defining only one forward after the IF statement it is working alright.


Live Life King Size
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

ronney singh wrote:But it is not working. Please tell me why?


Start by reading our FAQ documents at these links: ItDoesntWorkIsUseless, TellTheDetails.
ronney singh
Ranch Hand

Joined: Feb 10, 2012
Posts: 40

What I am expecting to happen is if variable value is 'e' then some code will run and forward it to "edit" action
If variable value is not 'e' then servlet should forward it to "failure" action
For better understanding code

This servlet is forwarding the page to "failure" action even when the String mode is 'e'.
when I have removed "forward=mapping.findForward("failure");" then error came that website has programming error or it is under maintenance.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

Well, yeah. If the mode is 'e' then eventually line 19 is executed. And right after that, line 22 is executed. What is the end result?

This is Java code you have posted there. It runs in a Java runtime environment. It's Java. Don't assume that just because you have a variable named "forward" that it will be treated differently than any other variable.
Krystian Gor
Ranch Hand

Joined: Jan 11, 2011
Posts: 30
Ever heard of else?
ronney singh
Ranch Hand

Joined: Feb 10, 2012
Posts: 40

Now I have written last line-"forward=mapping.findForward("failure");"
in else but still when the value of mode is 'e' it is forwarding to failure action. Why it is so?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

Show us, don't tell us. And be sure to use proper indentation in your code. It's really hard to see the conditional structures when your code is poorly written.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
ronney singh
Ranch Hand

Joined: Feb 10, 2012
Posts: 40

Paul Clapham wrote:Well, yeah. If the mode is 'e' then eventually line 19 is executed. And right after that, line 22 is executed. What is the end result?


But When I removed line 22 and value of mode is 'e' then why it is showing "website has programming error" because now the last value of forward must be 'edit'?
ronney singh
Ranch Hand

Joined: Feb 10, 2012
Posts: 40

Here is the code-
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

If line 25 is executing, then mode isn't 'e'.

Why aren't you using logging statements or a debugger to debug this?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

ronney singh wrote:why it is showing "website has programming error"


How should we know? We don't even know what the "it" is which showing that. (You didn't TellTheDetails.) At any rate let's assume that it's correct. Then the website (yours) has a programming error. Quite possibly that's because some code threw an exception, although that's just a guess. In which case it's quite likely that there is a log which will contain a stack trace of that exception; you should look for that.
ronney singh
Ranch Hand

Joined: Feb 10, 2012
Posts: 40

I have checked the value of mode on jsp page where "failure" is forwarding by using scriptlet

It is showing that value of mode is 'e'.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

That means nothing. It's the value in the servlet that matters.

So:
  • Why aren't you using logging or a debugger to debug this?
  • Why do you have discredited scriptlets in your JSP? That's a really poor practice.
  • Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18985
        
        8

    Bear Bibeault wrote:If line 25 is executing, then mode isn't 'e'.


    Actually the expression

    will always return false regardless of the value of the mode variable (except if it's null). It would be less confusing and more accurate to use this instead:
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61756
        
      67

    Doh! Missed the single quotes! Good catch!
    ronney singh
    Ranch Hand

    Joined: Feb 10, 2012
    Posts: 40

    Thanks and Great catch sir
    I am Beginner in java I don't have any idea how to use logging so it would be great if anyone can provide me link how to use logging.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61756
        
      67

    java.util.logging.Logger or Log4J.
    ronney singh
    Ranch Hand

    Joined: Feb 10, 2012
    Posts: 40

    Thank you very much sir.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Can we define multiple forward on single servlet?