Image cannot be displayed by a simple JSP - java

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" />

Related

Eclipse IDE 2022-06, how to change template when create new JSP file?

I am using Eclipse IDE 2022-06 , Java/JDK 1.6, Windows 10 pro x64, Spring 2.5.6 .
I need encoding UTF-8 by default, not ISO-8859-1. I don't want edit many files, many time manually. How to change template when create new JSP file?
In the New JSP File dialog click Next
to choose a template or
click the JSP Templates link to go to the preferences Web > JSP Files > Editor > Templates where you can change the template for JSP.
By default, in the JSP templates the variable ${encoding} is used which can be configured in the preferences Web > JSP Files.
sample result
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>

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.

Linking a CSS file into JSP

I have been trying to get my '.jsp' file to use a '.css' file that I have created. I had originally stored the '.css' file in my 'WEB-INF' folder but through some searching found that the 'WEB-INF' folder is not public and can therefore not store the file so I moved it outside into the 'webapp' folder but I am still not getting anywhere.
The files 'index.jsp' and 'index.css' files are in the same folder:
'HelloWord/src/main/webapp'
My '/jsp' file links the '.css' as shown
<%# 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">
<html>
<head>
<link rel="stylesheet" href="${pageContext.request.contextPath}/index.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MM Vehicle Registration</title>
</head>
I get the following error when I try to run:
Nov 28, 2015 3:42:00 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/HelloWorld/index.css] in DispatcherServlet with name 'dispatcher'
The web page still displays ony without the css decoration. The file is already declared in a '.jsp' that is being rendered fine so why would I receiving this error?
Spring takes control over all URLs. You need to put <mvc:resources> into your configurtion and preferably put your client resources into WEB-INF/resources/ directory. Also check How to handle static content in Spring MVC?
try replace
'${pageContext.request.contextPath}/index.css'
with
'/index.css'.
Are both index.css and index.jsp in the root folder of webapp?
Ok,the strutre is right.Now I suggest you to check you web.xml.
You might have something like this?
<servlet-mapping>
<servlet>dispatcher</servlet>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Try calling your controllers with a different extension (.do for example) and update the servlet-mapping to suit
<servlet-mapping>
<servlet>dispatcher</servlet>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
then try to access to /helloworld/index.css.if ok,rollback '/index.css'. to '${pageContext.request.contextPath}/index.css'

Doubts about using jsp include

I'm starting to develop systems more precisely in java web applications using jsp'm more a problem that still can not find the solution. I have a file called [header.jsp] that is the links for CSS and other things that are important to my system that file is in the root directory. I have a folder that has other jsp files more when I try to do an include file [header.jsp] does not work, does anyone have experienced the same problem that could help me?
Thank you in advance.
TestFile.jsp
<%# include file="header.jsp" %>
<center>
<p>Thanks for visiting my page.</p>
</center>
<%# include file="footer.jsp" %>
header.jsp
enter code here
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello from Header
</body>
</html>
Same for footer.jsp
The include directive is used to includes a file during the translation phase.
This directive tells the container to merge the content of other external files with the current JSP during the translation phase.

Can't display images in JSP

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.

Categories