views:

116

answers:

4

I'm trying to assign a js variable to java's request or session and want to access it from the server side. I dont want the js variable to be passed on in the URL. Help me, I'm seeking for a solution.

Thanks in advance

A: 

JavaScript only runs on the browser, it has no connection to the database except for ajax type calls.

But, you can add data to the post request for example from javascript, and that would get to the servlet, which is probably your best solution.

But, this really depends on which javascript library you are using, though, for more details.

James Black
Actually you can run JS on the server too. Check out the Rhino library: http://www.mozilla.org/rhino/.
Willie Wheeler
True, but if it is running in the browser they still can't just work together without going through a URL, as far as I know.
James Black
A: 

I'm not sure if I understood your question. But if You want asynchronously manipulate "objects in browser" from server it is not possible directly.

There are workarounds, search for reverse ajax.

Maciek Sawicki
+1  A: 

It sounds like you need to review the difference between GET and POST variable submission. GET will put it in the address (exmple.com/index.html?var=6) and POST will not. After that you'll have to look at your js library to determine how to direct what the variable hooks up to.

Jason Tholstrup
A: 

The typical most simple way is to use hidden input fields and have your JavaScript fill those input fields with the values you need. When your form gets submitted the hidden input fields will be accessibel in your Java

Peter
But how will I access it in Java, will it be automatically stored in the session?
Arun
The hidden input field is only hidden in your html, once the request goes to your weberver you can treat and retrieve it like any other parameter in a form, aka you retriever it from your request
Peter