0

...... <% int s = (int) (Math.random() * 1000000); %> .................

<body bgcolor="<%=s%>">     .......

it shows no error and executing but on viceversa it shows error.I want JS varivble in java is their any otherway to do?

.....

var a=10;

<% int s = a //is their any other way %>

i know getParamater() method i want another alterntive way

1
  • Do you understand that Javascript runs on the client and Java on the server and that the two can never communicate directly? What are you trying to do? Commented Nov 13, 2010 at 5:23

1 Answer 1

2

No, you cannot do it like that. The Java is in a JSP that is executed on the server to generate a web page (in this case containing Javacript) which is returned to the user's web browser. When the web browser receives it typically renders it, and the Javascript is executed immediately or in response to some user action. By the time that the Javascript executes, it is on the wrong machine, and the execution context for the original JSP has gone away.

If Javascript needs to pass information to the server, it must do it by means of a new HTTP request. It could use an XmlHttpRequest object explicitly, or it could put information into the elements of a <form> in the current web page, or something similar.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.