By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,918 Members | 1,662 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,918 IT Pros & Developers. It's quick & easy.

assign JavaScript variable to Java variable problem in JSP

P: n/a
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!
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Matt wrote:
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.
Jul 23 '05 #2

P: n/a
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.

"Matt" <jr********@hotmail.com> wrote in message
news:ba**************************@posting.google.c om...
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!

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.769 / Virus Database: 516 - Release Date: 9/25/2004
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.