Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > assign JavaScript variable to Java variable problem in JSP

Reply
Thread Tools

assign JavaScript variable to Java variable problem in JSP

 
 
Matt
Guest
Posts: n/a
 
      09-25-2004
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!
 
Reply With Quote
 
 
 
 
Hal Rosser
Guest
Posts: n/a
 
      09-26-2004

"Matt" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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


---
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/24/2004


 
Reply With Quote
 
 
 
 
Tor Iver Wilhelmsen
Guest
Posts: n/a
 
      09-26-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Matt) writes:

> <% 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.
 
Reply With Quote
 
Tim Jowers
Guest
Posts: n/a
 
      09-27-2004
(E-Mail Removed) (Matt) wrote in message news:<(E-Mail Removed) 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!


Maybe post this question in a MSFT group. server side javascript is
their thing if I remmebr correctly.
 
Reply With Quote
 
Tim Jowers
Guest
Posts: n/a
 
      09-27-2004
(E-Mail Removed) (Matt) wrote in message news:<(E-Mail Removed) 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!


Maybe post this question in a MSFT group. server side javascript is
their thing if I remmebr correctly.
 
Reply With Quote
 
Bryce
Guest
Posts: n/a
 
      09-27-2004
On 24 Sep 2004 22:52:24 -0700, (E-Mail Removed) (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;


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.

--
now with more cowbell
 
Reply With Quote
 
Bryce
Guest
Posts: n/a
 
      09-27-2004
On 27 Sep 2004 10:26:49 -0700, (E-Mail Removed) (Tim Jowers) wrote:

>(E-Mail Removed) (Matt) wrote in message news:<(E-Mail Removed) 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!

>
>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...

--
now with more cowbell
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-28-2004
On Mon, 27 Sep 2004 15:50:11 -0400, Bryce wrote:

>>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...


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
 
Reply With Quote
 
saravanavsk saravanavsk is offline
Junior Member
Join Date: Jan 2010
Posts: 2
 
      01-25-2010
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=?????*/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Assign JavaScript variable to Jsp Variable Ranjan ASP .Net 1 04-10-2007 03:35 PM
Assign Javascript value to asp session variable philin007@gmail.com ASP General 4 10-09-2006 10:00 AM
[JSP] difference between jsp:forward and jsp:include alexjaquet@gmail.com Java 0 06-02-2006 01:21 PM
assign JavaScript variable to Java variable problem in JSP Matt Javascript 2 09-26-2004 06:12 AM
assign javascript variable value to VBScript server side variable Matt ASP General 2 09-25-2004 10:40 AM



Advertisments