Tomcat - The requested resource () is not available, file mp3 - java

I just want to finish a simple HTML 5 example.
<audio src="2.mp3" controls autoplay>
Your browser does not support the audio element.
</audio>
but it can't complete and this is the description from Tomcat:
"The requested resource (/DemoPlaying/2.mp3) is not available."
My folder structure:

It's obvious that your mp3 file is not available at the URL you're trying to request it. Correct your 'src' to point to the correct url.

I think you must remember:
1) If your app is wellformed, then the typical structure is like:
http://myserver:8080/myapp
2) Then your file must be published under the contexto of your app
http://myserver:8080/myapp/mp3file
3) So, in development time your structure must be:
Webcontent
|
|--index.jsp
|--mymp3.mp3
|--WEB-INF
4) And war file must content the same of Webcontent (or root folder of your app)
Only in that way your file will be detectable by the server, and dont forget to registrer the extension on Tomcat Server

Related

Why doesn't tomcat see certain folders?

In my tomcat webapps directory I have various projects with servlets in them ..
say named test,beer etc
My question is irrespective of the web.xml or contents when i type localhost:8080/folderName I must be able to view the contents right? but it doesnt work for all folders , for some it says resource not found ,
Example my test folder opens in the browser and my beer folder doesnt , I restarted the browser and tomcat after adding or modifying folders , why can this happen , please explain
My dear friend there is a certain process/configuration/rule that tomcat follows in order to render any web content.It is not some magic happening.
Tomcat has a way of reading web deployments within the /webapp directory.
When we type some URL On the browser tomcat does the following:
Example.
URL : http://localhost:8080/foldername/xyz
Here tomcat takes the part of the URL after http://localhost:8080,that is foldername/xyz.
So here the first part which is foldername means name of the folder present in the /webapps folder.
So reading this tomcat goes inside that folder.Later tomcat is at the mercy of a file called web.xml.All mapping from /foldername/ i.e. /xyz in our case, onwards are present in web.xml.
In your case , if you type http://localhost:8080/foldername/ , tomcat knows that browser refers to webapps/foldername but does not know which resource html/jsp/servlet to forward the request so as to be able to generate a response.
Hence it gives a resource not found exception.
If you want to run the above URL (http://localhost:8080/foldername) then you need to configure a <welcome-file-list> tag in the web.xml file.
So for the folders which are working in your case with the above URL, just open their web.xml file and you shall find the <welcome-file-list> tag.
Actually this is what happened
I had a folder test which had some files (NO WEB-INF) , tomcat listed its contents on typing localhost:8080/test
My other folder beer had a WRONG WEB-INF web.xml configuration
So the following is clear to me now
Without a WEB-INF folder and web.xml tomcat will just list the contents of the directory but when you have a WEB-INF and web.xml and something is wrong in them it doesnt even list the directory contents .
Just my understanding of it .
Thanks!

cant read files from resources

Im trying to read files(xml, images) from src/main/resources. But it doesnt work.My resources folder is Source folder. For example, when i m trying to read log4j.xml tomcat looking for it in C:/bin... And i also cant read images from resources. I can read it only from webapp. I ve read, that tomcat automatically replace files from resources to webinf, but i think that it doesnt work in my case.
Please, help. I dont have any idea.
Even if the png is added to the WEB-INF folder it wouldn't be accessible from the JSP page in your case.
On runtime a JSP page renders into a servlet that return HTML code in the response. Then a browser will parse your tag <img src="/Pajero.png" ... /> and send a request onto YOUR_HOST/Pajero.png url to access the image and get the error code 404, because content of the WEB-INF folder is not accessible for a client side in a java web application.
Perhaps it will be better to place images on the webapp folder at your case.
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("log4j.xml").getFile());
You can use something like this to get resources from classpath.
Regards

Unable to access Images inside a War file

I have a war file that I have deployed which contains an images folder in the WEB-APP directory. I tried accessing the images stored in the folder using
"localhost:8080/testapp/images/image1.jpg" but I am getting a 404 response. Can someone please help me with this?
Thanks in advance.
Your images directory needs to be a sibling of WEB-INF, not a child of it. Try this:
your-war-file.war
|-images/
| |-image1.jpg
| |-image2.jpg
| `-image3.jpg
|-WEB-INF/
| |-classes/
| |-lib/
| `-web.xml
|-index.html
`-404.html
Assuming you have your folder 'images' in the base directory for this web app (on the same level as 'META-INF' and 'WEB-INF' catalogs), the path seems correct. Maybe check that you haven't put 'images' folder inside 'WEB-INF' where it is not accessible? If that doesn't work, try accessing some other file inside your web app to check that it was deployed on server.
Clients may not directly access artifacts under WEB-INF.
Put them in a location directly accessible if you're not streaming them from an app endpoint.
how to read from path ?
use ("."+File.seperator+"some else") path of File for current path into your code or html file

Cannot Reach Html File on Google App Engine

I have an Eclipse Java project, and I added a "folder" off of the root called "webfiles". I then proceeded to create a file called form.html. Once I run the project locally or publish it, I cannot reach the .html file. I get a Not_Found error.
Is it possible in Google App Engine to use HTML files (other than index.html) and where do I have to place them in the project to access them by a browser? What path should I use in the browser.
You need to add include path="/**.html" " in your static files definition in appengine-web.xml else html files other than index.html would not even be uploaded.
If you have /war/webfiles/form.html in your project than path to acces it from browser:
http://your_app_name.appspot.com/webfiles/form.html
To make reference from other jsp pages use:
link to form

Servlet/Tomcat relative file path

Where to put files in a Tomcat Servlet application, such that they are relatively visible to the page??
More detail:
I am developing a servlet page while using an external library. That library depends massively on external loaded XML files (relative file paths). So I need to put these XML files in the running directory of the servlet.
Is there a way in Tomcat server, where files can be accessible relatively?
When a web application is deployed to Tomcat, the root of the web application ends up being $CATALINA_HOME/webapps/YOUR_WEB_APP/
As such, if using a servlet to access an XML file located on a path within the root of your web application, you can just use the following:
request.getServletContext().getResourceAsStream("PATH/TO/YOUR/XML_FILE.xml")
This will load the XML file as an InputStream. Of course, if you want access to the file itself, you can always use the getResource(String resource) method to obtain a URL, from which a File object can be obtained like so (alternative methods included):
File f;
try {
f = new File(url.toURI());
} catch(URISyntaxException e) {
f = new File(url.getPath());
}
EDIT: To make them relatively visible to a web browser, simply keep them out of the ./WEB-INF and ./META-INF directories.
If this library you are talking about is going to search for the file on the classpath (like Hibernate or Log4J would do), you will have to put your XML file in WEB-INF. However, I suggest you do not do this manually. You can put the file in a source directory of you application, which will make sure the file gets deployed on the right spot.
This is an old question. I'm working on Tomcat 9. I've been quite successful with taking the Tomcat installation directory as the base. (CATALINA_HOME) The relative path to a file in ROOT for example is then, "webapps/ROOT/someDir/fileName"
The place to complain about repeated answers is to deal with repeated questions.

Categories

Resources