It's not a secret anymore!
The moose likes Servlets and the fly likes How to refer a java variable in javascript code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to refer a java variable in javascript code." Watch "How to refer a java variable in javascript code." New topic

How to refer a java variable in javascript code.

amit shukla
Ranch Hand

Joined: Oct 25, 2000
Posts: 45
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...
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
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.

author of:
amit shukla
Ranch Hand

Joined: Oct 25, 2000
Posts: 45
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 ...

[This message has been edited by amit shukla (edited May 28, 2001).]
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

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.
amit shukla
Ranch Hand

Joined: Oct 25, 2000
Posts: 45
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 ??
Baraj Bhushan

Joined: May 24, 2001
Posts: 8
Hi, Use this, it will help you.
Like Java Code Here.
<% String var = "TestCode" %>
In Java Script
function myfun()
This will work.
Please Try...........
[This message has been edited by Baraj Bhushan (edited May 29, 2001).]
Poornima Shetty

Joined: Dec 12, 2000
Posts: 26
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))
function isAlphanumeric (s)
var i;
if (isEmpty(s))
if (isAlphanumeric.arguments.length == 1)
return defaultEmptyOK;
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;
I hope this helps.....
I agree. Here's the link:
subject: How to refer a java variable in javascript code.
It's not a secret anymore!