• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FileNotFoundException & Try/Catch

 
Mike Berna
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mike Berna
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64720
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic