Add a source from another file in eclipse - java

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...

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:
http://kodejava.org/how-do-i-get-web-application-context-path-in-jsp/

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.

Use
<img src="../images/pikachu.jpg" height="300" />
..(double dots) will move you to parent directory where you are having images folder.

Related

Render image in SpringMvc

I want my image show up in receipt_failure.html via such thymeleaf expression :
<img th:src="#{/src/main/resources/images/icons-alert-circle.png}" />
Image itself has settled in such SpringMVC folder structure:
I do not get where issue comes from because seems like I keep path structure but I am getting such error on my local environment
I moved /images folder into /static folder then fix html like this:
<img th:src="#{/images/icons-alert-circle.png}" />
and issue has been fixed

Dynamically including website components in JSP

Is there a way to use root path when refering to includes in JSP?
what I mean is instead of using this method:
<jsp:include page="../inc/header.jsp" />
<jsp:include page="../../inc/header.jsp" />
etc.
To just refer to it like this:
<jsp:include page="jsp/inc/header.jsp" />
Also this is how my project tree looks like
Anybody trying to figure it out here is the answer that I came up with. For some reason intelij IDE tells me that the path cannot be resolved(liar!) looking at my project tree you can include header to any file using this piece of code in your project:
<%# include file="/jsp/inc/header.jsp" %>
This is page content
<jsp:include page="/jsp/inc/footer.jsp" />
In both cases the IDE will tell you that it cannot resolve URL but it works like a charm I hope this helps not only me but future visitors

How to use <c:url> in <c:import> on Java?

I'm trying to import a JSP using c:import and c:url, but it tells me it couldn't find the file. I tryed using a link to see if it opens the file, and it works fine. So, I suppose the path is correct, but it isnt't working.
My code is like this:
<c:url value="/cabecalho.jsp" var="cabecalho" />
<c:import value="${cabecalho}"></c:import> //page don't open
link //to test the path
If I use <c:url value="cabecalho.jsp" /> it works fine!
What must be happening?
c:url gives You absolute path from internet like http://www.somehost.com . c:import wants relative path to jsp on server.

finding render path for file in eclipse workspace

I am trying to get a view.jsp file to display another .jsp file as a popup however I keep getting resource not found errors. Both files reside in the same directory in an Eclipse workspace. I was told that I need to use the files render path. I'm not sure how to find/create a render path for a file. The code activating the popup looks like this:
<a href="popupex.jsp" onclick="return popitup('RenderPathGoesHere.jsp')">
<input type="submit" value="Single Issue Upload" name="SingleIssueUpload"><br>
</a>
I'm kinda new to portlet development so any advice would be appreciated.
Thanks!
You can create portlet render url to redirect to jsp
i.e
<portlet:renderURL var="varName">
<portlet:param name="mvcPath" value="/html/foldername/RenderPathGoesHere.jsp" />
</portlet:renderURL>
And you can call like this
Click
HTH

Linking Files And Directory Structuring

I'm working on a web project using java/ jsp/ servlets/ html/ css in eclipse tomcat, where everything is in the WebContent folder.
In my jsp files... When I try to include other jsp files (using a link like "/fileName.jsp" in jsp include directive) I can do that successfully.
But When I try to include image files (using a link like "/fileName.jpg" in the <img src=""> tag) nothing happens.
Nothing happens because instead of looking in the WebContent folder for image file it looks in the tomcat home directory, i.e.
Instead of looking at "http ://localhost:port/projectName/..." it looks at "http: //localhost:port/..."
Why does it look at the wrong location only with <img src=""> tags but not in <%# /> tag.
A workaround for this is that I start giving absolute paths "/projectName/..." However doing this means I'm hardcoding project name everywhere. This is what I do not want.
Don't include binary content in an ascii output. Why not just use the img tag? If you need to do something to produce a jpeg, I would use a Servlet.
Because the jsp-Links in the website are getting processed and the image links not. Either change the image path or develop an filter that changes the images'links.
Yes Templar, that could have been a way to solve my problem.
However, I simply changed the Context Root of my project from "Project Name" to "/" in Eclipse. This solved my problem.

Categories

Resources