Can't display images in JSP - java

I can't display images in JSP (and can't access other things like external Javascript/jQuery through SRC). The following JSP page just contains one <img> tag that can not display the image located by the SRC.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="Temp.htm" method="post">
<img src="/images/temp.jpg" alt="Not available">
</form>
</body>
</html>
The images are in /WEB-INF/jsp/images. I have also tried to change the image location and ensure all the times that the path given to SRC is correct still it didn't. The same is possible with the applications without the framework. It works there perfectly. What am I missing here?

First, /WEB-INF is not accessible from the outside.
Second, if you use /images/temp.jpg, the browser will load images from the root of the web server, and not from the root of your web-app (which is deployed under a context path).
Use the JSTL <c:url> tag to generate absolute paths from the root of the web-app:
<img src="<c:url value='/images/temp.jpg'/>" alt=.../>

You cannot access files that are located in /WEB-INF/ directly from the web. The folder is protected from web access.
Locate your image files somewhere else. /images/temp.jpg is a absolute path, however you likely need a relative one. Point your image path to image/temp.jpg and place the images in the /images/ folder directly located under your web root. E.g. WebContent/images or src/main/webapp/images (depending on your web root).

Try put in the webapp directory, not WEB-INF. Obv. dont nest in the jsp dir.

Related

Image not displayed in JSP page with springboot

I just started learning springboot. Right now I am design the food order website But I want to put an image from the jsp page but int not showing me on browser.
main
|
|__webapp
|
|__WEB-INF
|
|__view
|
|__index.jsp
|__images
|__css
I am trying to add the image from image folder to index.jsp page. in image floder i stored all the images and in css folder i stored all the custom css file.
Index.jsp file
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>JSP-Page</title>
</head>
<body>
<img src="images/top.jpg" alt="food delight">
</body>
</html>
I also tried replacing the src as <img src="../../images/top.jpg"> but it didnt work i also tried moving my jsp and image to webapp folder directly also but no use. Is there anything im doing wrong how exactly does spring handle img requests?

Folder structures eclipse spring MVC - uploading Images

I am creating uploading files application in Spring MVC application. I am using thymeleaf templates. And there's problem because i Have folder structure builded by gradle(STS).
I have such line in controller:
imageDir=new File("C:/Users/lukas/workspace/uploadingFiles/src/main/resources/images");
The problem occurs here in thymeleaf showimage html script
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<title>Script shows specified image</title>
<link rel="stylesheet" type="text/css" href="/css/styles.css"
th:href="#{/css/styles.css}"/>
</head>
<body>
<div class="center">
<h2><a th:href="#{/uploaded}">Click here to previous page where you can choose image to show</a></h2>
</div>
<img th:src="#{${path}}"/>
<div class="center2">
<p th:text="${path}">dddd</p>
</div>
</body>
</html>
It doesn't display \images\0.jpg properly. Only one picture is displayed from other folders not exactly how specified in above line path. It gives output from other other folders for ex.: static images. Even in folder are other images the shown is only one. Even it image isn't in specified in the path.

I want to put my jar file in html code

How can I put my jar file into my html?
I tried to use that:
<html>
<head>
</head>
<body>
<applet code=Tictactoe.class archive="Test.jar">
</applet>
</body>
</html>
My problem is that when Im trying to run my html code java is blocking my jar file.
Try to add your url (local or distant server url) in exception list on java settings panel.

Image cannot be displayed by a simple JSP

I have a JSP which simply shows and image as header. I will change this into a .tag file for custom tag development. I am doing this in eclipse and my project structure is -
The jsp i am trying to run on the server is Header.jsp under jsp folder. The problem is that the image is not displayed, even when I use the fully qualified path of the image. Insted, i see a red cross. How do I fix this ?
When I use this file as a .tag file referenced by another jsp, the contents of tag do not appear in that jsp.
JSP code -
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib prefix="myTags" tagdir="/WEB-INF/tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<img src="/images/java_logo.gif"><br>
</body>
</html>
The URL you specify here is absolute for your site, not the web app.
<img src="/images/java_logo.gif">
So try to use (with WEBAPPROOT replaced by the correct name)
<img src="/WEBAPPROOT/images/java_logo.gif">
or make it relative:
<img src="../images/java_logo.gif">
If you're in JSP or Facelets, it is way better to use HttpServletRequest#getContextPath:
<img src="${request.contextPath}/images/java_logo.gif" />
If you happen to use JSTL:
<img src="<c:url value="images/java_logo.gif" />" />
In this way, you avoid using relative paths and/or guessing what would be your current web application path (this is in case you change the name to display the app or something similar). For example, if you happen to have this structure:
- WebContent
- images
+ java_logo.gif
- jsp
+ Header.jsp
- anotherFolder
+ Another.jsp
If you want to add java_logo.gif in Another.jsp, you just need to do this:
<img src="${request.contextPath}/images/java_logo.gif" />
Unlike the relative path:
<img src="../../images/java_logo.gif" />

How does HTML java applet finds its .jar?

I have this code:
document.write("<APPLET CODE='com/synergex/My.class' ARCHIVE='toc2.jar' ");
When my "toc2.jar" is in the same folder as the HTML - all works fine.
But when I put "toc2.jar" in another folder ("C:\MyJars"), I get ClassNotFoundException.
How can I "tell" the browser where to look for jars?
I tried to add "C:\MyJars" or "C:\MyJars\toc2.jar" to my CLASSPATH env variable, but it did not work.
You might be able to redirect the code base using an HTML base element.
For example, given the following BASE declaration and A declaration:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Our Products</TITLE>
<BASE href="http://www.aviary.com/products/intro.html">
</HEAD>
<BODY>
<P>Have you seen our Bird Cages?
</BODY>
</HTML>
the relative URI "../cages/birds.gif" would resolve to:
http://www.aviary.com/cages/birds.gif
If not, there are only 2 options.
Leave the Jar in the same directory as the HTML.
Presuming that part of the script is the only thing you cannot change, add codebase='../path/' immediately after it, to tell the JRE to wherever the Jar is located.
As an aside:
CODE='com/synergex/My.class'
Should be:
CODE='com.synergex.My'

Categories