Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

checking whether HTML file exists  RSS feed

 
dhriti joshi
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how do i write an if condition in jsp to check whether a particular html exists in a folder or not.
i have to display that html if it exists(should go in if condition),if it does not exist at that location i have to do something else(show another link,this is else condition)

Thanks,
dhriti.
[ June 08, 2005: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dhriti, "please help" is a terrible subject line. I have changed it to something more meaningful on your behalf.

Please read this for further information.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one thought...
It's ugly but could probably be implemented as a custom tag pretty easily.

Interesting note: I first tried this with ServletContext.getRealPath("...") but it wasn't working. It appears that getRealPath doesn't try to verify that the file actually exists, it just finds the application's path and tacks the argument string to it.

If there is a JSTL tag for this, I'm unaware of it.
[ June 08, 2005: Message edited by: Ben Souther ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To do this with the JSTL you'd use the <c:if> tag. You could code up a bean that it could reference to do the check, but pesonally I'd make all such computations and determinations in the servlet controller and pass simple data values to the JSP telling it what to do.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. And there's always the custom tag route which I would opt for if doing this fairly frequently.
[ June 08, 2005: Message edited by: Bear Bibeault ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
To do this with the JSTL you'd use the <c:if> tag. You could code up a bean that it could reference to do the check, but pesonally I'd make all such computations and determinations in the servlet controller and pass simple data values to the JSP telling it what to do.


Both ideas assume an MVC architecture.
In a perfect world.....
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call me elitist, but I never recommend anything else.
[ June 08, 2005: Message edited by: Bear Bibeault ]
 
dhriti joshi
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for using:


<%if(null == application.getResource("/index.jsp"))
{%>

FILE NOT FOUND...<%}

else{%>
Show link to file..

<%}%>

do I have to have my file in webinf or it could be in any path within the application.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
do I have to have my file in webinf or it could be in any path within the application.


What does the javadoc for the getResource() method have to say?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:


What does the javadoc for the getResource() method have to say?


http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html#getResource(java.lang.String)
 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This method returns null if no resource is mapped to the pathname.

The above statement in Java Docs states that this should have a mapping in the web.xml file.

Is there any solution if we don't have any mapping in web.xml as it seems drishti wants to see html file and it won't have a mapping in web.xml.

Drishti in this case the only option which comes to my mind is writing an I/O scriplet and checking whether the file exists or not.

Cheers,
Rahul
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66141
141
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The above statement in Java Docs states that this should have a mapping in the web.xml file.


No, that's not what it is saying. The term 'map' here is loosely used. Perhaps 'resolve' would have been a better term.

The resource does not need to be mapped in web.xml to use this method.
 
Rahul Juneja
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be I am not correct as I haven't tried that. But yeah I just said what I understood from the terms used in the API description.

Cheers,
Rahul
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That example that I pasted in works as a stand alone JSP.
Try it. Play with it on your server. You'll get a feel for what it's doing pretty quickly.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!