aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes how to load an image in a jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "how to load an image in a jsp" Watch "how to load an image in a jsp" New topic
Author

how to load an image in a jsp

kunal vermaa
Ranch Hand

Joined: Jun 27, 2009
Posts: 51
please tell me how to load an image inside a jsp through click of a button..

help..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Do you mean via Ajax?
kunal vermaa
Ranch Hand

Joined: Jun 27, 2009
Posts: 51
i dont know.. i am new to java.. please explain..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Tell us what you want to do exactly. Can the current page be replaced by the image? Do you want to open the image in a new window (or tab)? Do you want to include the image in your current page? Can the current page be refreshed?

1) use a simple &;t'a href="my_image.png">Click here for my image!
2) use a simple &;t'a href="my_image.png" target="_blank">Click here for my image!
3+4) use JavaScript (setting the src attribute of the image element) if you don't want to refresh the page, or a form with submitting if refreshing is allowed

I'm thinking the JSP part is actually irrelevant; it seems to be mostly about HTML and maybe a bit of JavaScript. Moving to the HTML / JavaScript forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
kunal vermaa
Ranch Hand

Joined: Jun 27, 2009
Posts: 51
i want to load the pic on the same jsp page on click of a button or when an mouse event is generated please explain how..?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

In the click event handler, simply create a new <img> element that references the image URL. What part has you stumped?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kunal vermaa
Ranch Hand

Joined: Jun 27, 2009
Posts: 51
thanks a lot bro for your help.. actually i am new to java.. not very familiar with images.. can you explain how to fetch an image placed in a directory...?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Creating an image element on the fly may be quite hard. Not the creating itself, but the placing etc.

Instead, why not create an image element which you initially hide:
With JavaScript, you can then change the image source and unhide it:
The order of these two changes is important; if you unhide it first the bogus image (or lack thereof) is shown.

Of course the image will need to be loaded so it may take sometime. Therefore, perhaps you can use an empty, transparant (GIF) image initially (instead of "dont_really_care_since_were_not_using_it_anyway"). It won't show up as a red cross (meaning image cannot be found), but still show nothing while the new image is loading.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

kunal vermaa wrote:actually i am new to java

This has nothing at all to do with Java.
<img name="image_i_want_to_change"

The name attribute is not legal in image elements.
can you explain how to fetch an image placed in a directory...?

No fetching is involved. Image elements reference images by URL. If you do not know HTML, that's good place to start.
kunal vermaa
Ranch Hand

Joined: Jun 27, 2009
Posts: 51
1. <img name="image_i_want_to_change" id="image_i_want_to_change" src="dont_really_care_since_were_not_using_it_anyway" style="display: none">
2. <% var image = document.getElementById("image_i_want_to_change");
3. image.src = "C:\Documents and Settings\hp\Desktop\wall\image1.jpg;"
4. image.style.display = "";
%>



myeclipse6.5 is showing "invalid escape sequence in the line 3" i.e it is showing error in the path. but path is ok.. what should i do..? and is this code sufficient to load an image on a JSP.?

please help
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61606
    
  67

Why do you have the JavaScript code in a Java scriptlet? If you do not know the difference between JavaScript, Java and Java scriptlets, you need to back up a bit and learn the difference.

Nothing we are talking here needs Java. No Java scriptlets. No JSP. No java. This is all client-side JavaScript. It can appear within a JSP, but does not need any server-side mechanisms.

Perhaps this article might be helpful.
kunal vermaa
Ranch Hand

Joined: Jun 27, 2009
Posts: 51
Thanks a lot for getting me right bro.. i got it..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to load an image in a jsp