Guenevere
Guenevere

Reputation: 75

Add a source from another file in eclipse

Probably a ridiculously easy question here but I must be phrasing it weird in all of my search queries to find similar solutions.

So in my eclipse project I have a folder with some .jsp files in it. In another folder there are some .jpgs . I want to use one of these .jpg files in my .jsp but for some reason cannot get the classpath correct.

I tried right clicking and copying the qualified name and using that path but it wont link correctly for some reason...

my code looks like :

 <img src="pikachu.jpg" height="300" />

I've also tried:

 <img src="/My_Project_Name/WebContent/images/pikachu.jpg" height="300" />

Note: the Jsp is in:

 /My_Project_Name/WebContent/JSP_FOLDER/JSP.jsp

thanks in advance - I know this should be a simple thing...

Upvotes: 0

Views: 52

Answers (3)

Aaron Digulla
Aaron Digulla

Reputation: 328724

You need to use

<img src="<%= request.contextPath %>/images/pikachu.jpg" height="300" />

in your JSP.

<% request.contextPath %> will expand to the path under which your web server is serving your app.

Every resource inside of WebContent/ will then be accessible relative to this path.

Related:

Upvotes: 2

alfreema
alfreema

Reputation: 1338

Similar to Aaron's answer but I believe he has a typo in it (missing an equal sign).

Try this instead, which in my opinion is a little less messy:

<img src="${pageContext.request.contextPath}/images/pikachu.jpg" height="300" />

Aaron's solution would have been:

<img src="<%= request.getContextPath() %>/images/pikachu.jpg" height="300" />

If those don't work, view the page source and copy/paste us what the line looks like when you are using those.

Upvotes: 0

Ankit Sharma
Ankit Sharma

Reputation: 1565

Use

<img src="../images/pikachu.jpg" height="300" />

..(double dots) will move you to parent directory where you are having images folder.

Upvotes: 0

Related Questions