-1

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

Tinks
  • 1
  • 1

1 Answers1

1

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875