aspose file tools*
The moose likes JSP and the fly likes How to use the Java variable inside the Javascript function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to use the Java variable inside the Javascript function" Watch "How to use the Java variable inside the Javascript function" New topic
Author

How to use the Java variable inside the Javascript function

Rizwan Patel
Ranch Hand

Joined: Jan 03, 2008
Posts: 59

Hi All,
I wanted to know how can we use the variable declared and defined in Java inside the javascript function?

Suggestion required....


Example


scjp 1.5, OCPJWSD 5
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

You can't. Not directly.

JSP executes on the server; JavaScript on the client. There can be no interaction.

However, remember that the purpose of a JSP is to dynamically create the markup to send to the client, so you can use JSP to hard-code the variable's value into the JavaScript:



[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

You may find this article informative.
Rizwan Patel
Ranch Hand

Joined: Jan 03, 2008
Posts: 59

Thank you Sir, The article really clarifies most of the doubts.
One question is Whether in JSP by writing one script function can we write the complete JAVA code inside the jsp file withing <% %> tags
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

You can put Java code in a JSP, but modern best practices say that you should not.

I have another article that you might find useful.
Paulina Tanner
Greenhorn

Joined: May 10, 2008
Posts: 3
I am having a similar problem.

I would like to pass a java variable as a parameter in to a onKlick event handler javascript function. How do I do that?

<html>
....
.
..
.
<script>

function enable(value){
alert(value);
}
</script>


<%
int i=89;
%>

<input type=checkbox name="chkBox" onKlick="enable(<%=i%>);"


How do I pass the value of i to the enable function?

Please help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

What about your example doesn't work? The value of I should be written into the page before it is sent to the browser.
Rizwan Patel
Ranch Hand

Joined: Jan 03, 2008
Posts: 59

I am having a similar problem.

I would like to pass a java variable as a parameter in to a onKlick event handler javascript function. How do I do that?

<html>
....
.
..
.
<script>

function enable(value){
alert(value);
}
</script>


<%
int i=89;
%>

<input type=checkbox name="chkBox" onKlick="enable(<%=i%>);"


How do I pass the value of i to the enable function?

Please help.



Hi I have resolved my issue to get the value in a javascript but I dint know whether it is the best way to that or nat.
I am giving the approach.
1.Taken the java variable and assign it the value
2.Now i have assigned this value to the Hidden parameter of the form
3 And from the javascript function i used the value of this parameter.

In my case it works fine...
Please suggest whether any other approach can be possible
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Paulina Tanner,
In the future, please start a new thread with your question.
Asking a new question in someone else's thread is called hijacking and is a rude thing to do to the original poster.
If you think your question is similar or related to another, add a link to that thread in your own.

Thanks
-Ben


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use the Java variable inside the Javascript function