• Post Reply Bookmark Topic Watch Topic
  • New Topic

Checking for url existence  RSS feed

 
Shaun Appleton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to check to see if an image exists on a webserver. If it does then I will display it if it does not the I will display a default.
I have tried the following

According to my documnentation (JSP the Complete reference by Phil Hanna)
code should equal 404 if the url does not exist. Even if it does exist I always get back 401.
What am I missing? Is there an easier way to does this.
Any help much appreciated,
Shaun
 
Sue Spielman
Author
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shaun Appleton:
[QB]I want to check to see if an image exists on a webserver. If it does then I will display it if it does not the I will display a default.
QUOTE]
Shaun: You can use the JSTL to do this very easily with code simliar to:

HTH.
<br>
Sue
Author ofJSTL: Practical Guide for JSP Programmers
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66153
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you'd rather not set up for using JSTL just to get this working, I think your problem is that after openConnection() you need to call connect() to actually establish contact and read headers.
Check out the Javadoc on UrlConnection.
Another thought: if the image is on the same server on which your code is running, it might be a tad more efficient to just check the file system (servletContext.getRealPath() will help you out there) rather than going through another network connection.
hth,
bear
 
Shaun Appleton
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for this. The code was actually working! It correctly gave 401 - unauthorised because the files were held on an IIS machine which required NT authentication. I could view the files as a user because I was allowed. JBoss/Tomcat could not view the files as it was running under a service account which which unauthorised. My solution now is to bundle up the images into a war and deploy them. Now everything is fine.
Shaun
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!