How Do I use javascript to connect and access values of database ?
I want to register a user ,so when I am about to create user i want to check wheather username is available or not(means want to do search operation over database)?
can we do this with javascript? cause it is use for client side so I am confused?
How Do I use javascript to connect and access values of database ? I
Share
JavaScript is a general-purpose programming language, not a client-side language. People use it server-side all the time.
However, if you mean, can you use JavaScript in the web browser to access a database on your server, the answer is: Not directly, no (well, not unless you’re on an intranet and jump through hoops you really don’t want to jump through); you need a middle layer. You can have JavaScript on the client send messages to the server (via ajax, for instance); server-side code then needs to process those messages, ensure they’re valid and not malicious, and then very guardedly update the database and send back a result to the client browser.
The server-side language can be JavaScript or any other language you want to use on the server. I notice you tagged your question
jsp, so presumably you’d want to write the server-side stuff in Java (rather than JavaScript). If so, and if you want to use ajax so you have a modern, non-page-refresh experience, it looks something like this:User performs and action, raising an event in the browser.
JavaScript code handles the event by sending an ajax message to the server; in the case of login, you probably want to do a
POST.A JSP (or better yet, a servlet)
Receives the
POSTmessageValidates the contents, checking for malicious content and other similar attacks
Queries and/or updates the database using prepared statements and similar to avoid SQL injection attacks
Sends the response, with information about the result of the operation
The JavaScript code receives the response to the
POSTajax call and looks at it to see what happened