assign JavaScript variable to Java variable problem in JSP

M

Matt

If I assign Java variable a to javascript variable x, it is
fine.
<%
int a = 10;
%>
var x = <%= a %>;
alert(x);

But if I do the other way around, then it has 500 error. any ideas??

<%
int b;
%>
<% b %> = x;


thanks!
 
H

Hal Rosser

Matt said:
If I assign Java variable a to javascript variable x, it is
fine.
<%
int a = 10;
%>
var x = <%= a %>;
alert(x);

But if I do the other way around, then it has 500 error. any ideas??

<%
int b;
%>
<% b %> = x;
Matt,
Javascript does not execute until it gets to the browser,
Java executes on the server as you know.
Java does not know the value of the JavaScript variable.
HTH
Hal
 
T

Tor Iver Wilhelmsen

<% b %> = x;

In addition to the response you got, "b" is not a legal Java
statement, which gives the compiler error that the 500 error probably
wraps.
 
T

Tim Jowers

If I assign Java variable a to javascript variable x, it is
fine.
<%
int a = 10;
%>
var x = <%= a %>;
alert(x);

But if I do the other way around, then it has 500 error. any ideas??

<%
int b;
%>
<% b %> = x;


thanks!

Maybe post this question in a MSFT group. server side javascript is
their thing if I remmebr correctly.
 
T

Tim Jowers

If I assign Java variable a to javascript variable x, it is
fine.
<%
int a = 10;
%>
var x = <%= a %>;
alert(x);

But if I do the other way around, then it has 500 error. any ideas??

<%
int b;
%>
<% b %> = x;


thanks!

Maybe post this question in a MSFT group. server side javascript is
their thing if I remmebr correctly.
 
B

Bryce

If I assign Java variable a to javascript variable x, it is
fine.
<%
int a = 10;
%>
var x = <%= a %>;
alert(x);

But if I do the other way around, then it has 500 error. any ideas??

<%
int b;
%>
<% b %> = x;

That's because of this fundamental difference between JSP and
JavaScript:

JavaScript: run on the client's browser.
JSP: Compiled into a Servlet, and runs on the server, before
responding to a request.

Therefore, JSP can write to the JavaScript variable because its part
of the page that's to be displayed. JSP is evaluated BEFORE the page
is displayed on your browser.

Try this experiment:

take your example #1 abouve, and view source. You'll see

var x = 10

in the source. Notice you do not see:

int a = 10;

anywhere.
 
B

Bryce

Maybe post this question in a MSFT group. server side javascript is
their thing if I remmebr correctly.

Wasn't there a Netscape web server/servlet container that had server
side javascript? I seem to remember someone here around the office
complaining about having to support a customer that was using that...

Ahh yes, iPlanet...
 
A

Andrew Thompson

Wasn't there a Netscape web server/servlet container that had server
side javascript? I seem to remember someone here around the office
complaining about having to support a customer that was using that...

Ahh yes, iPlanet...

I suspect this mention of server-side JavaScript is confusing
the OP, if anything.

a) It is very rare, (as alluded to above, though I think there
are other forms of SS JS)

b) He has (and apparently needs) calculation ability on
both the client and server.

c) He already has server-side JSP, so use it.

d) The client-side calculations can be done in JS, which
then does a get/post back to the server to transmit information
for consideration/calculation by JSP's on the server.

HTH

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
Joined
Jan 25, 2010
Messages
2
Reaction score
0
i got the point that we can't assign javascript variables to java variable in jsp. Then in the situation ,,,,,i am having the various textbox going to display the employee details ,,,so after getting the employee id,,and click the view button , i like to display the employee details on the corresponding textbox in the same jsp page.
How can i do this , without having the id value from the text box for the database connection. /*select * from emp where emp id=?????*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top