• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to refer a java variable in javascript code.

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a String variable named 'location' in a JSP page, value for this variable is being retrieved from database ... now i want to use this variable in javascript code ( written in the same JSP page for validation ) to show to in some textfield .. how can i do this...
Amit
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just write Javascript as part of the HTML page like you normally would. As far as JSP is concerned it doesn't care.
I suggest starting with a static HTML page that has the Javascript you want, using a dummy value for the variable, then just duplicate that from JSP with JSP inserting the value.
Watch out for getting the quote marks right.
Bill

------------------
author of:
 
amit shukla
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bill.
Thanx for the response.. could you please examplify it as i'm not clear about what you said...
actuallly its like...
var slocation = location; (java variable)
textfield.value = slocation;
in this case it is saying variable location not defined ...

Amit
[This message has been edited by amit shukla (edited May 28, 2001).]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to be clear on where each variable 'exists' in a client-server app.
The Java variable you want exists ONLY on the server.
You use all the state information you have to generate a HTML response that gets sent to the client.
When it is on the server this response it treated as text.
This text contains the Javascript, but it is treated as text at the server, not code.
The response gets sent to the client, the client browser loads up the web page and is now able to treat the javascript as code.
Not that the client now has NO KNOWLEDGE of the Java variables.
Sorry if this is a bit long winded, but I remember it took a bit of a leap when I was first trying to understand the whole 'client-server' thing.
So the problem you have tpo overcome is "How do you get the variable on the server to get passed to the client".
If we go back to the fact that the server sends text to the client, and it's this text that the client interprets as code, you just have to include the variable as text in the response sent to the client...
in SERVER_SIDE.jsp you have:

the 'text' that gets sent to the client that the client interprets as a web page would look something like this:

the server replaces the <%=location%> with its actual value before sending it to the client.
Hope this answers you question.
Dave.
 
amit shukla
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dave...
thanx for such a nice explanation... actually the problem i have is like ..
i have to do some validations on one textfield called 'location'
it shud have only alphabets.. when the page is first displayed it shows for ex 'delhi' in the textfield... now the user can change this to any value except numeric chars... now if any numeric or invalid chars r entered i want to change the value of the text field to again 'delhi' by overwriting the numerical or invalid value entered by the user... now the value of this variable 'location' is retrived from the database.. so u cant retrieve that value again from a javascript code.. thats y the need to access java variable...
now since sending is going to take place only after validations java variable cant be retrieved from the server...
in this case what can b done ??
Amit
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Use this, it will help you.
Like Java Code Here.
<% String var = "TestCode" %>
In Java Script
function myfun()
{
document.formName.VariableOrTextBoxName.value="<%=var%>";
return;
}
This will work.
Please Try...........
BYe,
BHU
[This message has been edited by Baraj Bhushan (edited May 29, 2001).]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first get the value for the text box from the database and display it say as
<input type="text" name="display" value="<%= db.getUserName() %>">

now again when the user changes the value ,you have to validate if its alphanumeric so use a javascript function and
if the user entered value is correct then submit the form or do what you need to else set the value of the text box back to
"Delhi" or whatsoever as below:

say you have a submit button and onclick you are calling checkValue() function as

<input type="submit" value="submit" onclick="javascript:checkValue()">

<script language="javascript">
function checkValue()
{
var textval = document.forms[0].display.value;
if (isAlphanumeric(textVal))
{
document.forms[0].submit();
}
else
{
document.forms[0].display.value="delhi";
}
}
function isAlphanumeric (s)
{
var i;
if (isEmpty(s))
if (isAlphanumeric.arguments.length == 1)
return defaultEmptyOK;
else
return isAlphanumeric.arguments[1] == true);

for (i = 0; i < s.length; i++)
{
var c = s.charAt(i);
if (! (isLetter(c) | | isDigit(c) ) )
return false;
}
return true;
}
</script>
I hope this helps.....
 
Oh the stink of it! Smell my tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic