简体   繁体   中英

How to display Text and images both in same JSP page in easiest way?

Here i am sending you my code for simple navigation page...! I am not putting it into code segment as its executed as HTML code...!

Can anyone tell me the way to get my image in this page along with Text and where i need to put it inside my netbeans project ??

I have created images folder inside /WEB-INF folder...!

/WEB-INF/ Images/Forward.jpg Is it correct ??

Code from file nevigation.jsp

<%@page import="java.net.URL"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> </title>
</head>
<body>
<div style="position: fixed ;bottom:550px;left:600px">
<p>Nevigation Bar </p>
<img src=" ??? " />
</div>
</body>
</html>

I have tried all possiblities....

  1. /images/Forward.jpg

  2. /WEB-INF/images/Forward.jpg

  3. /localhost:8081/Car_Dynamic_Demo/images/Forward.jpg

  4. D:\\study\\ADVANCE_JAVA\\PrOgRaMs\\WEB_APPS\\Car_Dynamic_Demo\\web\\WEB-INF\\images\\Forward.jpg

BUT NOTHING IS WORKING FOR ME...!

IS IT PROBLEM OF MOZILLA FIREFOX ??? OR DYNAMICALLY GENERATED PAGES are faulty somewhere???

Hoping for best and quick solution....!

Thanks....!

The WEB-INF folder is inaccessible from the outside of the webapp. So, anything that must be downloaded by the browser must NOT be put in this folder.

The root of the webapp is the folder containing WEB-INF. Put anything in this folder or any subfolder, and it will be downloadable from the browser.

So, if your JSP and your image are in the same folder, and you invoke the JSP from the browser, the JSP can use a relative path for the image:

<img src="Forward.jpg"/>

If the JSP is at the root, and the image is inside an images folder (which is at the root), the URL is

<img src="images/Forward.jpg"/>

If you want to be able to refer to the absolute path of the image , regardless of the URL used to invoke your JSP, you'll have to use an abolute path. An absolute path must start with a /, and contain the context path of the web app:

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

or, if you use the JSTL (which you should do):

<img src="<c:url value='/images/Forward.jpg'/>" />

No.. it is not right to keep images inside WEB-INF. You should place them outside WEB-INF folder and if images are in same folder in which jsp pages are, then simply mention only relative path in otherwise mention its full path. Example- I have placed images and other jsp pages in the folder containing WEB-INF folder(means outside WEB-INF folder) and i have used the image in jsp page as

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM