简体   繁体   中英

How to assign a JavaScript variable to Java

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

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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM