It's not a secret anymore!
The moose likes JSP and the fly likes Inserting images on JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Inserting images on JSP page" Watch "Inserting images on JSP page" New topic

Inserting images on JSP page

Joseph jo

Joined: Mar 02, 2009
Posts: 11
Hi All

I'm trying to insert few images on the JSP page.

but i would like to link those images from images folder in the application directory...

for e.g. images are in folder C:/JSPproject/images and i would like to insert them in page C:\JSPproject\web\myfirstpage.jsp

So my questions are...
1) Can i create images folder in JSP project? if yes, how?
2) Can i create CSS file in JSP project? if yes, how? or was the alnernative?
3) How can i give the reference to the css file from the JSP page?

Please its very urgent...any ideas welcomed


Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
simply put them in the root directory of your application, in other words

|______ images
|                   |_____ img.png
|______ css
|______ page1.jsp
|______ folder1
|                  |_____ page2.jsp
|______ WEB-INF

now in page1.jsp if you want to get a reference of img.png you do in the href of the image tag "images/img.png" while if you want to use the img.png in the page2.jsp you have to do "../images/img.png".

or you can start your paths with a "/" to start from the root.

its just a matter of directories, the same thing is done for css, javascript or any other file you want to use

Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Joseph jo

Joined: Mar 02, 2009
Posts: 11
Hi Omar

Thanks for your reply.

I tried to follow as you is my html code

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<h1>Hello World!</h1>
<A href="..\images1\chicklet.png" ID="image1">Inserting image on page.</A>

images are in C:\JSPProject \images1\chicklet.png and ....
JSP file is in...C:\JSPProject\web\myfirstpage.jsp

I'm using Netbeans 6.5 for developing this JSP page.

Could you please tell me what im doing wrong here?


Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
I never used Netbeans for web page development, however try to locate a folder in your project that is named WEB-INF and create a sibling folder for it and name it img. (may be the WEB-INF folder is under a folder named web). and put your image inside the folder you created which is "img".

and reference your image in the JSP using this path "img/chicklet.png"

if you don't get it then i would suggest you to better understand how is the structure of a Java web application by visiting this page
Abhishek Asthana
Ranch Hand

Joined: Sep 08, 2004
Posts: 146

If you have created a web project in NetBeans, you must be having a Web Pages folder under your project directory. This will have more folders like:
and your JSP pages.

You can create an images folder right here. And access the images simply images/imagesname.jpg

Hope this helps.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63348

See the JSP FAQ for information on properly formatting resource URLs. Using .. and \ are problems you need to fix.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joseph jo

Joined: Mar 02, 2009
Posts: 11
Hi Abhishek

i did that....but the problem im facing now is ...i have to give full path....
for e.g C:/JSPProject/images/img1.png

its not taking ../images/img1.png....then its not displaying the image.



Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42958
Using absolute physical paths (containing something like "C:") is an even bigger problem than what Bear mentioned. You need to avoid all of those. is the page you should have read.
I agree. Here's the link:
subject: Inserting images on JSP page
It's not a secret anymore!