image display on jsp page - java

i am using<img src="../img/image.gif" /> to display the image on jsp but it show a smalll icon.
img folder are parallel to jsps folder
Directory structure:
WebContent
jsps
.jsp files
foo
img
images.gif
WEB-INF

Create a variable in your jsp that points to the context of your application
<c:set var="ctx" value="${pageContext.request.contextPath}" />
and use it to reference your images
<img src="${ctx}/images/theme/middle_banner.jpg" />

Related

Specifying Image Path File From Another Folder in WEB-INF?

I'm including a separate JSP page (header.jsp) into my current home.jsp page as goes:
<jsp:include page='/WEB-INF/Header/header.jsp'/>
In that header.jsp page, I have an image as follows:
<img id="myImage" src="<c:url value='../Images/myImage.jpg'/>" />
But my image is not displaying on my home.jsp even though all the other elements (i.e. paragraphs, anchor links, etc.) found inside header.jsp is displaying normally on the home.jsp page as it should.
My folder structure goes:
MyProject
WebContent
WEB-INF
Header
header.jsp
Images
myImage.jpg
Views
Home
home.jsp
How do I specify the path file to my image (<img id="myImage" src="<c:url value='?/myImage.jpg'/>" />) from within my header.jsp file?
I seem to have it working:
img src="${pageContext.request.contextPath}/Images/myImage.jpg"
Please let me know if this is an ideal way to go about this in JSP.

Linking CSS File To JSP

I have a css file in my WEB-INF folder and in a jsp page i have given the location of the css file as :
<link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/WEB-INF/Tabs.css"
But the problem is it doesn't link with the css file. If i take out the css file from the WEB-INF, it works perfectly.
This is my first time working with css.
What seems to be the problem here ?
Thank you for your time.
The problem here is just as you describe it, the location of the css-file.
All resources located within the WEB-INF-folder is not reachable directly from the web browser, but has to be accessed through a servlet.
And this is the reason of why the css isn't loaded when you are visiting your jsp.
Try instead to put them in a more common structure such as /webapp/resources/css/Tabs.css and change the href to ${pageContext.request.contextPath}/webapp/resources/css/Tabs.css
**Edit **
The folder I refer to as webapp might have any name, but the location of it would be the parent folder of the WEB-INF folder.

Display image in JSP with SPRING MVC

I am trying to display an image on a jsp.
My image file is located at
MyApp/WebContent/images/logo.jpg
And my JSP pages are located at
MyApp/WebContent/WEB-INF/view/home.jsp
I have already tried to use the image by
<'img src="<%=request.getContextPath()%>/images/logo.jpg" />
and
<'img src="<'c:url value='<%=request.getContextPath()%>/images/logo.jpg'></c:url></img>
Is this issue something because of my location hierarchy where I have placed my image?
Really appreciate your help. Thank you.
UPDATE:
I've found the solution to my problem in:
http://www.tutorialspoint.com/spring/spring_static_pages_example.htm
I just have to use resource mapping in my servlet.xml.
I really appreciate all of your kind answers. :)
Any static resource is also look for a URL Mapping in spring mvc, so static resources should be defined in the springmvc-servlet.xml.
Add the following entry to your MVC configuration. I assume that your static files in resources folder.
<mvc:resources mapping="/resources/**" location="/resources/" />
then static files can be accessible from the page.
<img src="/resources/images/logo.jpg" />
To avoid to have to indicate explicitly the context path you can use jstl core and do it like that
<img src="<c:url value="/images/logo.jpg"/>"/>
You can also check this thread about spring ressource and path
Spring 3 MVC resources and tag <mvc:resources />
try
<img src="/MyApp/WebContent/images/logo.jpg" />
Even though it is a Spring MVC app, it should still deploy as a normal webapp. Check your deployment to make sure, and also use the browser to test loading.
To make it work I had to do
in spring config:
<mvc:resources mapping="/resources/**" location="/resources/" />
In JSP:
<spring:url value="/resources/images" var="images" />
<img src="${images}/back.png"/>
TRY THIS !
ALWAYS WORKS FINE !
Create your img folder at src/main/resources
Copy the picture inside this folder called "img"
Write inside
Use this picture inside
check the screenshots and enjoy !
I put images folder under WEB-INF directory, after did fully configuration in the spring-dispatcher-servlet.xml file, I used this img src:< img src="projectname/../images/logo.jpg" /> in my jsp page, images display finally.
in springmvc-servlet.xml you should add <mvc:resources location="/WEB-INF/images/" mapping="/images/**" /> and in jsp <img src="images/logo.jpg" /> and you should create a folder under web-inf which is named images and in the web.xml your servlet mapping shoul be like that <url-pattern>/</url-pattern>.

Where I save the applet in tomcat?

I call an applet through a JSP...where I have to save it? and what the .class and the .java file? in which root in tomcat so when I call it from the JSP to be appeared?
here is the applet call from the jsp
%><%# page language="java"%>
<html>
<body>
<jsp:plugin code="g7appletDialog.class" codebase="" type="applet" width="300" height="200">
<jsp:fallback>Unable to load applet</jsp:fallback>
</jsp:plugin>
<applet code=""g7appletDialog.class"" width="300" heigjt="200"></applet>
</body>
</html><%
The path to your servlet must be relative to your jsp. If you have a jsp in web-inf/admin/pages/index.jsp, then you can place the .class file in directory web-inf/admin/pages/ and just have code="g7appletDialog.class".
In case that you have a central repository for applets, lets say web-inf/admin/applets then you should change the code to code="../applets/g7appletDialog.class".
The ../ gets you one dir back. To go two dirs back use ../../ and so on...

how to refer images in javascript file which are located in different folder?

I have below folders in my web application.
inside
webapp/public/images/some.png
webapp/public/js/somejs.js
webapp/WEB-INF/some.jsp
Now inside somejs.js i need to refer some.png and am refering as below but it is not picking the image. do i need to change the url?
My jsp file is located in WEB-INF folder
$('#myform #someId').html('<img src="../images/some.png" /> <span style="color:green">Successfully Saved</span>');

Categories