简体   繁体   中英

Call a Java function from Javascript

I'm a junior web developer and I am working on the development of a website for which I need to call a Java function in Javascript.

This Java function allows you to connect to a DB to insert data. I could do it in Javascript directly but my boss doesn't want it because by opening the source code of the page we could see the user's clear password when connecting to the database.

I know it's impossible to call a Java function in JS because Java is running on the server side unlike JS, but my boss tells me that no it's possible lol

Sorry if the subject has already been treated but it's been several days that I can't move forward and I don't know what to do...

Thank you

I know it's impossible to call a Java function in JS because Java is running on the server side unlike JS, but my boss tells me that no it's possible lol

Your boss is right, but not in a useful way. :-) JavaScript running in the JVM (via javax.script ) can call Java functions. But as you said, JavaScript in the browser cannot directly call a Java function on the server.

What you do instead is have the JavaScript code in the browser call your server via HTTP, using fetch or other "ajax" tools ( XMLHttpRequest , or wrappers for fetch / XMLHttpRequest like axios ). Your server-side code handles the request with Java code interacting with the database, and returns the result. (Often JSON is useful for the result, and sometimes for the request.)

But :

...my boss doesn't want it because by opening the source code of the page we could see the user's clear password when connecting to the database...

The password shouldn't be in the clear on the server, either. :-) See this answer for alternatives.

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