This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JSP and the fly likes Use JavaScript Variable In Side Java Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Use JavaScript Variable In Side Java Code" Watch "Use JavaScript Variable In Side Java Code" New topic
Author

Use JavaScript Variable In Side Java Code

Sameera Abeysekara Abeysekara
Greenhorn

Joined: Apr 10, 2007
Posts: 19
I want to access java script variable in side java code in JSP

How can i do this?

i wrote following code but that one is not working.

function fillData(){
test=document.getElementById('adduser').employeeNumber.value

<%
if(test==71){%>

document.getElementById('adduser').employeeName.value="Sameera";
<%
}
%>

}

(i want to "test" variable access in side the java code)

Thanks
Sameera
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

I think its impossible, because the <% Java code %> inside a JSP page is executed on the server side and produces an HTML page, while the JavaScript code is executed on the client. When the HTML page reaches to the client, the Java code is not present anymore.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Muhammad Saifuddin:
I think its impossible, because the <% Java code %> inside a JSP page is executed on the server side and produces an HTML page, while the JavaScript code is executed on the client. When the HTML page reaches to the client, the Java code is not present anymore.


Muhammad is correct.
Your Javascript is nothing more than output text as far as the JSP environment is concerned.


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: 61447
    
  67

Please read this article to understand the JSP life cycle.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
Don't get me started about those stupid light bulbs.
 
subject: Use JavaScript Variable In Side Java Code