permaculture playing cards*
The moose likes JSP and the fly likes JSP throwing exception problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP throwing exception problem" Watch "JSP throwing exception problem" New topic
Author

JSP throwing exception problem

Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi all,

I have two JSPs. The first JSP accepts input from a html form and the second JSP is an error page. The first JSP page calls a helper class that checks to see if the one of the parameters of the html form is a certain string (checkCommand). If the parameter is a certain string then an exception should be thrown and the secound JSP page should be displayed. I have the error page directive set in both JSPs and it seems to workok. The problem is that it displays the error page(2nd JSP) no matter what the input of the form is. So I am guessing the exception is not being eaten when I want it two. I am not that great at exception handleing. Here is what the code looks like in my helper function that the first JSP calls. Any thoughts?
Thanks,

Jutah

public static void checkCommand(String cmd) throws Exception
{
try
{
if(cmd.equals("sendToErrorPage"))
{
//throw new Exception(); ?
}
}
catch(Exception )
{
//eat exection method throws
}
}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

"John Utah",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender
[ October 21, 2005: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Personally I avoid all such problems by not dealing with exceptions at all on the pages. I just let the errors propogate out to the container where they are handled by the error handling directives I have instituted in the web.xml.

This keeps a lot of "goo" off the pages, and allows for a high degree of centralized control over how errors are handled.
[ October 21, 2005: Message edited by: Bear Bibeault ]
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
sorry for the name mix up

AM
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Hi Bear,

I would use the way you suggested, but this is a project and I have to use the error page directive. I think I am close because I know it is hitting the error page when it has an exception. The problem is that I can't eat the exception when there is not really an error. Any thoughts?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

What exception is propogating that you feel should be "eaten"? Is your catch block really firing?
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
The checkCommand method throws Exception. The problem is that I need to "eat" the exception(if the string is not the one Iam looking for) in the method before it returns back to the JSP otherwise the error JSP page will still display.

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

I'm still not quite getting your problem. Are you trying to catch the exception inside or outside of the method? Your description seems to indicate the latter, yet your code example shows the catch inside the method.
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
I would like to catch the exception inside the method if the string off the form does not equal the one in the method. That way the expection will not get back to the JSP. If the string coming in off the form does equal the one in the method then I want the exception to be thrown all the way back up to the JSP. Sorry if it is hard to understand or I am explaining bad.

Thanks,

AM
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

If you don't want the exception to be propogate out of the method, why throw it in the first place? Or is it lower-level code that you have no control of that is throwing the exception?
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
I need an exception to be thrown back up to the calling JSP only if the the string off the form is equal to the one I am looking for. Then the error page will be displayed. If the string is not the one Iam looking for then I don't want an exception thrown up to the JSP. Here is an example.

JSP1 has calls <% Helpers.checkString(request.getParamatrer("someString"))%>
JSP1 has an error page directive that goes to JSP2(error JSP) if an exception is thrown.

Helpers.checkString(String someString)
{
String stringToBeChecked = "aaaaaa";
if(someStrin.trim().equals(stringToBeChecked))
(
//Throw an exception so it goes back up to the calling JSP
}
}
This is where I am confused if the method throws it then I need the exception that the method throws to be eaten if someStrin != stringToBeChecked. If I try to throw the exception myself then it has to be caught in the method and I can not get it all the way back to the JSP. Is this any better?

Thanks,

AM
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

This is where I am confused if the method throws it then I need the exception that the method throws


But, looking at your code, your method only throws the exception when the strings are equal and so you don't need to be worried about "eating" anything in the case where they are not equal since an exception will not be thrown at all in such a case.
[ October 22, 2005: Message edited by: Bear Bibeault ]
Andrew Mcmurray
Ranch Hand

Joined: Sep 24, 2005
Posts: 188
Bear,

Thanks for your replies and patience. You are right, I had another exception being thrown in another method that the JSP was calling that was causing the mix up. It seems to work ok now that I fixed the other method.

Thanks,

AM
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP throwing exception problem
 
Similar Threads
Page inclusion error
Why is struts calling reset???
Problems while inserting record
I need help with Checkboxes
ClassCastException pulling list from session