Java Web Application. Problems in locating images - java

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 !

Related

Servlets: can't link css from jsp: cannot resolve directory resources: ${pageContext.request.contextPath} links to root instead of src/main/webapp

I have this project structure (WEB-INF is inside src/main/webapp/):
I try to access a login.css from login.jsp using
<link rel='stylesheet' href="${pageContext.request.contextPath}/resources/css/login.css">
However Idea underlines it as cannot resolve directory:
I use servlets for this project, but however in the other project that uses Spring MVC, the same css link works perfectly. I suspect the ${pageContext.request.contextPath} links to a different folder in this case: to the project root (../src) instead of the root/src/main/webapp folder.
Is there a way to change where ${pageContext.request.contextPath} links to? Or some other way to fix it?
"resources" looks like a directory for maven or concept/grouping in your IDE. They are probably combined at the root of your WAR> My guess would be that your you should remove "resources" from the link as they really reside at the root of your web app.
${pageContext.request.contextPath}/css/login.css
Take a look at the WAR that is actually produced and where they would be in that.

Spring mvc + maven: cannot access images

I'm using a standard maven archetype of webapp, so I put my images folder under src/main/resources directory and in the war package it goes under WEB-INF/classes/ directory.
My project tree is in the following image
So the images dir is in the classpath and i load it via getResource
final String path ="/resources/images/disegno.svg";
URL imagePath = this.getClass().getClassLoader().getResource(path);
but i get
Could not complete request
java.lang.NullPointerException
when declaring path
Also in .jsp, I try to access the image in this way:
<img src="<%=request.getContextPath() %>/WEB-INF/classes/images/disegno.svg"/>
but no image is displayed, even if it links to
http://localhost:8080/svgTest/WEB-INF/classes/images/disegno.svg
I cannot understand how maven works in this situation, and cannot find a complete guide, because everywhere all looks so easy, so I don't know which error i'm doing...
You're making 3 mistakes.
First: ClassLoader.getResource() expects a path that does NOT start with a /.
Second: what is under src/main/resources in the source project goes at the root of the classpath. There is no resources package, as your screenshot of WEB-INF/classes shows. So your path should be
this.getClass().getClassLoader().getResource("images/disegno.svg")
Third: by design, everything undex WEB-INF is not accessible from the browser. Everything out of WEB-INF is. So if you want the browser to be able to download your image, it should be in the src/main/webapp folder, not in src/main/resources. Of cource, once it's there, you won't be able to load it with the ClassLoader anymore, since it won't be in the classpath anymore. If you need to load it in addition to make it directly available to the browser, then use ServletContext.getResource().

How to include JSP from jar lib

I have a web-app with a lot of lib projects, which are common to another apps.
I'd like to put some JSP files in one of those libs and then make a <jsp:include... or <%include... But I'm not able to make it working.
Is there a way to achieve this?
If there is, which is better, to do a include directive (#include file..) or action (<jsp:include page...)?
Thanks
I know of only one way.
Keep the JSP in /META-INF/resources folder.
Create a 'lib' folder inside the WEB-INF folder in your WAR.
Place the JAR (containing the JSP in the /META-INF/resources folder) in the lib folder of the WAR.
In the main JSP where you want to include this external JSP residing in the JAR, use the following statement: <jsp:include page="/myJSP.jsp" ></jsp:include>. Note that the '/' is required in front of the jsp file name to make the container look for the JSP in an absolute path (and hence in the jar in the lib folder) rather than in the current path where the main JSP is lying.
This will surely work.

Where to store static files like html/css/javascript in a jetty project?

I have a maven project that I run using jetty:
$ mvn run:jetty
Where in my project should I be storing my static files like HTML, CSS, Javascript, images?
My layout is using a simple web app arch type:
/src/main/java/webapp/web-inf/views/
Should I just create a folder there named e.g. 'assets' ?
And then my view pages will reference the /assets folder somehow? I'm confused as to what path I will use in my html pages to reference an image like:
/assets/images/logo.png
This isn't so much a Jetty question as it is a general Java webapp question. If you plan to serve them out directly (like *.css, *.css, images, etc), put them somewhere above WEB-INF but below your docroot. Java WebApps are all the following basic directory structure.
<docroot>
+WEB-INF/
+lib/
+classes/
Anything in <docroot> is reachable directly through straight up http. Anything WEB-INF and below is not. A really simple webapp with one page (index.jsp), one image in an images directory, and its configuration file (web.xml) would look like this.
index.jsp
images/bob.jpg
WEB-INF/
web.xml
lib/
classes/
In index.jsp you could reference bob.jpg like...
<img src="images/bob.jpg"/>
This is really a Maven question rather than a Jetty question.
Typically you would put your images (etc) in the maven webapp directory - i.e. source/main/webapp/ (not under web-inf)
How you structure things underneath that is up to you, but it will mostly depend on how much content you are expecting to put in, and how you think it is best to organise it.
source/main/webapp/assets/images is fine, but so is source/main/webapp/images or source/main/webapp/static/.
Then, within your HTML, you reference the images using whatever path you put in beneath the webapp bit.
The general answer is - the root of your web application is webapp. Dynamic resources (as JSP pages or Freemarker templates) would better off be in a web-inf/ subfolder (they are accessible through classloader but not from a direct browser request).

Accessing a web application's static resources

I've just built a very simple Java web application using the Wicked framework. The current layout of the project is:
src/
main/
java/
net/myapp/
Homepage.java
Homepage.html
reources/
scripts/
script.js
In the Homepage.html file I am trying to load the JavaScript file:
<script src="scripts/script.js"></script>
I deployed the application, but the browser doesn't find the JavaScript file.
The WAR file is being packaged using the maven-war-plugin. I looked into the WAR file, and I see the following layout:
WEB-INF/
classes/
net/myapp/
Homepage.class
Homepage.html
scripts/
script.js
What am I doing wrong? What am I missing?
The web-related resources should be placed in src/main/webapp
Your directory structure should be:
WEB-INF/
classes/
net/myapp/
Homepage.class
Homepage.html
net/myapp/scripts/
script.js
and your markup should be:
<wicket:link><script src="scripts/script.js"></script>&lt/wicket:link>
Resource sitting behind WEB-INF folder are not publicly available. If Homepage.class forwards to the Homepage.html, file you should be seeing that fine. But in the HTML page you have your reference to the javascript file, which is not publicly available. You need to move the scripts outside of the WEB-INF. The structure should look like
WEB-INF /
classes /
net/myapp/
Homepage.class
Homepage.html
scripts/
scripts.js
This way a refernce in the html file to
<script src="scripts/script.js"></script>
will work properly. When the HTML page is rendered on the user side, they will make the call back to get the JavaScript resource. At this point, the file needs to be visible.
An update of your build script, or app layout should take care of this for you.
Edit: See Bozho's answer, it will fix the build for Maven. see This link for Maven
While the other answers are correct in general, they don't quite take Wicket into account. With Wicket, you can have resources on the classpath, and in some cases they are better than a static file.
You can use Application.mountSharedResource() to assign a url to a shared resource, which can come from anywhere, your classpath included.
Spelling resources without the s?
reources/

Categories