GeeCON Prague 2014*
The moose likes JSP and the fly likes Inserting scriptlet in java script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Inserting scriptlet in java script" Watch "Inserting scriptlet in java script" New topic
Author

Inserting scriptlet in java script

Yans Tan
Greenhorn

Joined: Jul 11, 2006
Posts: 29
Hi there!

I am facing some problem with inserting the following line in the
javascript.

out.println("document.getElementById(\"img1\").src = \"data:image/jpeg;base64,"+(String)v3.get(1) +"\";");


What I did was to out.print the whole javascript in the jsp file.
Not sure anyone know is this the right way?

Because I tried inserting scriplet in javascripts and its giving me some problem.

Thanks in advance for helping all!
Yans
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42039
    
  64
its giving me some problem.

What does this mean? What is the HTML/JavaScript that gets generated?


Ping & DNS - my free Android networking tools app
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Moved to the JSP forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
This approach is fine, as long as you understand the separation of responsibility. Your JSP will produce javascript code onto the page.
Javascript code will not be accessing java variables.

The easiest way to debug this is to generate the page, and then view source to see what the javascript generated was.
Sometimes I even save that as a static page, edit the javascript until it works, and then go back and change the JSP so it produces the correct javascript.
Yans Tan
Greenhorn

Joined: Jul 11, 2006
Posts: 29
Hi all,
Thanks for all your help!

I found out that the problem actually lies with the base64.

But I have a problem inserting javascript variable in the scriplet

For example:

out.println("document.getElementById(\"img1\").src = \"data:image/jpeg;base64,"+(String)vec3.get(1)+"\";");

(String)vec3.get(1)


How do I change the 1 to a javascript variable?

Thanks in advance for all the help!

Regards
Yans
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
How about this?



MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Yans Tan
Greenhorn

Joined: Jul 11, 2006
Posts: 29
Hi there,

The code that i quoted has no error. But i want to change the number in the vector.get() to a javascript variable.

How do I insert a javascript variable in my vector.get(). It will be recongised as a string if I am not wrong. Anyway to solve this anyone?

Thanks for all the help!

Regards
Yans
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You can't change it to a Javascript variable because Javascript will be interpreted in the client (browser) AFTER all of the JSP code has finished and the output is sent to the client.


To better understand this, run your code and then use your browsers 'view source' option to see what was returned from the server.
THAT is the code that the Javascript interpreter will see.
[ August 03, 2006: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

This article might be helpful in understanding the relationship (or lack thereof) between Javascript and server-side Java.
Yans Tan
Greenhorn

Joined: Jul 11, 2006
Posts: 29
Hi there!

Thanks for the article.
I think this approach is wrong. Anyone can suggest how to counter this?
I shall have a more detailed description of what I am trying to do:

1) Use javascript to bind fuctions to the up key on the keyboard
(case 38 i think)

2) So what is suppose to happen is that the image is suppose to change when user presses up key.
Code behind:
case 38:
image++
document.getElementById('img1').src = data:image/jpeg;base64,vector(image);

So I was thinking of another way that might work, which is to have a javascript array, then hard code the size and initilising of values.
But that would not be practical.

Anyone have any better ways to do it? Forgive any syntax errror i made. I was just citing an example.

Thanks for helping!

Regards
Yans
[ August 03, 2006: Message edited by: Yans Tan ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

document.getElementById('img1').src = data:image/jpeg;base64,vector(image);


I do not understand what this line is supposed to do. The src attribute of the the <img> tag must be a URL to the image file or a servlet that will serve the image data.

You're not trying to in-line the image data in the JSP are you? That's not going to work.
Yans Tan
Greenhorn

Joined: Jul 11, 2006
Posts: 29
Hi there!

Sorry I not sure what is in-line.

Anyway that line is supposed to display an image throught the vector.get(variable) which is from another bean. Its working fine with me. Sorry for any confusion from my syntax again.

I think I managed to solve it through looping:

for(int z=0;z<=2; z++)
{

out.println("sa["+z+"]="+ array[z] +";");
}

This was what I did. Thanks for all your advice and help!
Thanks alot!

Regards
Yans
Yans Tan
Greenhorn

Joined: Jul 11, 2006
Posts: 29
Anyway i would like to make some comments.

I really find this place very useful and the people here are really friendly and gives good advices.
It really saves me much trouble looking blindly on the net.
Especially Bear, you really help me alot Thanks!

Take care all! Thanks again

Regards
Yans
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inserting scriptlet in java script