I am new in Tomcat Server as well in Java as I take over code from other developer. I attached pdf file in web pages to be download from footer section. The footer was from another directory inside webapps ROOT/WEB-INF/../../contactUs.jsp and the file is in webapps/brochure.pdf so I called the pdf file and it display into web page but when I download then it say "Failed - No file"
src:
<aside class="widget widget_footer">
<h4 class="widget-title">Company</h4>
<ul class="ps-list--link">
<li>About Us</li>
<li>Career</li>
<li>Contact</li>
<li><a href="${pageContext.request.contextPath}/brochure.pdf" download>User Guide</a></li>
</ul>
</aside>
Thanks for corresponding my question in advance.
I want to the file to be downloadable for client.
The paths you mention look strange:
ROOT/WEB-INF/../../contactUs.jsp would mean contactUs.jsp is besides ROOT?
webapps/brochure.pdf would mean brochure.pdf is besides ROOT as well?
This is not where Tomcat would serve files, and your error message is likely related to that.
In the webapps directory you should have different web applications - each of them in either a directory or the zipped version, a .war file. ROOT is just a web application specially treated by Tomcat. Inside a webapp you can find content to be served directly, just like a htdocs directory on a webserver installation - or the special directory WEB-INF, which contains internal data not directly presented on the network.
So assuming your webapp's name is ROOT, place your pdf into the path webapps/ROOT/brochure.pdf and reference it as /brochure.pdf from JSP pages. Hint: it should reside in the same directory as about-us.jsp.
Related
I'm making a java web app, and I want it to display an image. However, it doesn´t find my image.
I've made a folder in /src/main/resources/images
Also, in the .jsp file, I´ve tried with the following sentences.
<img src="/src/main/resources/images/Head.png"> </img>
<img src="< c:url value='/src/main/resources/images/Head.png'/>"> </img>
Is there anything bad I'm doing?
Thanks
Edit:
The path of mi .jsp file is /src/main/webapp/WEB-INF/jsp/welcome.jsp
You can find the web app code in https://github.com/Santi-7/hello
Your app is a Spring Boot app. I think that you can also try to use the facilities provided by Spring Boot for serving static content. Anyway, you are doing it right now because you are using webjars for css and js libs!!! Be consistent with the tech that you are using.
The structure of a .war file is as follows:
/
/WEB-INF
/lib
/classes
/META-INF
Now, your application has the following structure (I assume, given the folder structure, you are using Maven)
/
/src
/main
/java
/resources
/webapp
Now, the Maven war plugin will copy everything in the classpath to /WEB-INF/classes during compilation - this is /src/main/java and /src/main/resources by default.
The crux of the matter is that nothing under /WEB-INF or /META-INF can be accessed by requests - this is for security as otherwise someone could simply download /WEB-INF/web.xml for example.
So, in order to add a resource that is accessible by a browser, you need to place it into /src/main/webapp - this will become the root of the application.
So if you place Head.png into /src/main/webapp/images then in the JSP you would use:
<c:url value='/images/Head.png'/>
In short, you need to read up on how the directory structure of a .war works and how that relates to your code.
The path to the image must be relative to the path to the .jsp file.
Because the path to your image is: /src/main/resources/images/Head.png, and the path to your jsp file is: /src/main/webapp/WEB-INF/jsp/welcome.jsp, in your image tag you need to write:
<img src="../../../resources/images/Head.png" />
The ../../../ is for getting out from the jsp folder to the main folder, and the resources/images/Head.png is the path from the main folder to the image.
Thanks everybody, i could resolve my problem.
Changes I made:
So, in order to add a resource that is accessible by a browser, you need to place it into /src/main/webapp - this will become the root of the application.
Now, my images are in /src/main/webapp/images.
The path to the image must be relative to the path to the .jsp file.
Now, the sentence of my .jsp file is
<img src="images/Head.png" />
Edit [1]:
¡ I made a mistake. The path to the image is relative to the /webapp classpath !
Is there a way to upload an image not located in Eclipse project to a jsp?. I have searched that it is possible if image directory is placed in Application Server but that's not what I want, I would like to use a c:\directory in Windows 7. Please find below the code in jsp file I'm using:
<body>
<h1>Image below:</h1>
<img alt="No Image"
src="C:/MauricioFiles/Proyectos_JavaEE7/MusicStore/pics/guitar.png"
width="100" height="100">
</body>
Thank you!!!
The <img src> must refer a public web URL, not a local disk file system path. Your images have to be available by a HTTP request. It's namely the webbrowser who has got to download them, not the webserver who has got to include them somehow.
Yes you can upload it. But I think what you want to do is to access the local file without uploading it.
But as JSP says, JavaServer Pages. That means that you need to have all your files uploaded and ready in your server. What you do at the moment is, that you use a file which is on your computer and not inside your Server-directory. But you can't access local files outside of your server-environment.
My WebApp uses Java annotations to map the URL to the servlet (#WebServlet) and I don't have a web.xml file. I want to foward the request to another HTML file and I did it like so:
request.getRequestDispatcher("/WEB-INF/test/testpage.html").forward(request, response);
It works, but I can't get additional resources (javascript) from the server.
<script type="text/javascript" src="testsuite.js"></script>
testsuite.js is located in /WEB-INF/test/testsuite.js, the same folder as testpage.html. When requesting testsuite.js I get 404. How can I configure Tomcat to serve all the resources in my work tree without a web.xml? Worst case I will simply embed JS.
Additionally I let eclipse install the web server on my local machine so I have no idea how they are deployed.
You have to move resources outside of your WEB-INF folder. It is a special folder for compiled Java code, jars, configuration files etc.
The WEB-INF folder is restricted in application servers and servlet containers for security reasons, so for example a request for /appname/WEB-INF/web.xml will be deined of course.
Static resources such as *.html files, *.css files, *.js files, images etc. should be placed outside of WEB-INF folder, in your case the test folder should be moved up next to WEB-INF.
I'm doing a web application where I can upload and download pdf files from server.
In this I created a pdf files and stored its path on the database.
What I want is when I search by some criteria (like filename) I should get list of files from db. When I click open link it should be opened in jsp.
Downloading part was done by me. What I need is just to open a pdf in jsp.
is it possible to open server file from jsp?
Place those files outside WEB-INF folder and access. WEB-INF folder is secure in a Java EE environment. By placing your files inside the WebContent folder and outside the WEB-INF folder will help you to access those files for download. You need to mention the full path for that file. E.g for an image your path will be https://localhost:9444/AppName/resources/theme/images/logo.png
Can anybody provide some instruction on how to setup a war file to show a favicon.ico in the browser address bar?
You can also use the following HTML markup in your HTML:
<link rel="icon" type="image/gif" href="/img/image.gif">
Most newer browsers should support it and I think it's generally a more clean way since you can use any image type/name/location you want.
This might be different in different application servers. For tomcat, the favicon comes from the directory your root context is mapped to. So if your application is mapped to the root context [/], just place the favicon.ico file in the top level folder in your war file.