This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Inserting images on JSP page" Watch "Inserting images on JSP page" New topic
Author

Inserting images on JSP page

Joseph jo
Greenhorn

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


Thanks

-Joseph
Omar Al Kababji
Ranch Hand

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

JSPProject
|______ 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
Greenhorn

Joined: Mar 02, 2009
Posts: 11
Hi Omar

Thanks for your reply.

I tried to follow as you said...here is my html code

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

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?


Thanks

-Joseph
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
Joseph

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:
META-INF
WEB-INF
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
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

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
Greenhorn

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.


Thanks

-Joseph

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41089
    
  43
Using absolute physical paths (containing something like "C:") is an even bigger problem than what Bear mentioned. You need to avoid all of those.

http://faq.javaranch.com/java/ResourceUrlProblems is the page you should have read.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting images on JSP page
 
Similar Threads
Folder Mapping in web application.
creating website basic question
image storing and displaying (struts2 + hibernate)
Adjusting location of images in a CSS file using url() ?
JSP ALL Load