Meaningless Drivel is fun!*
The moose likes JSP and the fly likes FileNotFoundException & Try/Catch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "FileNotFoundException & Try/Catch" Watch "FileNotFoundException & Try/Catch" New topic
Author

FileNotFoundException & Try/Catch

Mike Berna
Greenhorn

Joined: Aug 25, 2004
Posts: 4
Hi. I am brand new to Java (mostly ASP background) and have had a few Java projects thrown at me and am totally confused...

I need to detect a file not found exception before ever going to a page. This file exists on a different server so I cannot use a relative URL (I think this would eliminate using jsp:forward or jsp:include?) My plan is to use response.sendRedirect in a try/catch statement but I recieve all kinds of errors. What types of (java specific)statements need to be included to set this logic up? I appear to be missing several things. HELP!
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Hi, Mike, and welcome to JavaRanch!

While I can't say for sure what's wrong without looking at your code, you should be aware that once you write somethign to a JSP output stream, you can't redirect it. Thus, any redetection code must be done first. This might be the problem that you are experiencing. (Then again, it might not be...)

I'm going to move this to our JSP forum, where I think that you'll find more people focused specifically on JSP issues.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Mike Berna
Greenhorn

Joined: Aug 25, 2004
Posts: 4
Joel,
Thanks for the reply. I have just started working on this but from what I have found on the web, I have been headed in this direction:

try {
response.sendRedirect("http://SOME URL");
}
catch (FileNotFoundException fnfe) {
System.out.println("File Not Found");
}
}

I am totally new to this and may be completely off base. I think I need to declare several things prior to this. I am paging through an old Java text as I write this. My hope is to add this as an include file to all .jsp files in an application.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

You shouldn't be doing this from a JSP page at all. Your request should 'hit' a servlet which can perform the file check for you. Because the file exists on another server your best bet would be to try to establish a connection to it using java.net.URLConnection. if that succeeds, you can then forward on to the appropriate JSP page. If not, take whatever action is appropriate.
[ August 25, 2004: 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: 61066
    
  66

P.S. The redirect approach could never work because all a redirect does is to set up the response headers appropriately so that the when sent back to the browser, the browser initiates another request to the redirect URL. So whether the redirected URL resource exists would not be detected until long after your try/catch block goes out of scope.
Mike Berna
Greenhorn

Joined: Aug 25, 2004
Posts: 4
Back to the drawing board, I guess...

Thanks for the response. I will look into the servlet resolution that you have suggested.
[ August 25, 2004: Message edited by: Mike Berna ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

Now that you know where we are, feel free to drop by with any problems you have getting this working. (Though non-JSP-specific questions should go in the Servlets forum from this point.)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: FileNotFoundException & Try/Catch