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!
 
R

Richard Cornford

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;

With server-side scripting one of the most fundamental things that needs
to be grasped is what runs where and when. The approximate process
goes:-

1. Browser sends a request to a server.
2. Server discovers that the request is for a JSP
and *executes* the method of that JSP that builds
a response (usually HTML page source code that may
include client-side javascript).
3. The response is sent back to the browser.
4. The browser receives the response and displays the
result, executing any javascript that may be included.

As a result the JSP should be considered to have finished before the
javascript has even started. The JSP, executing first, may write values
into any javascript source code on the page but the only way a
client-side javascript variable value is going to get anywhere near a
JSP is if it is sent back as part of a query string on a GET request or
as part of a POST request.

Richard.
 
H

Hal Rosser

Since Java runs on the server, and javascript waits until it gets to the
browser, Java does not know what the value of the javascript variable is. It
don't work now, and it won't work - unless you include a var's value in the
querystring you send to the jsp.
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top