This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Modifing database object using javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Modifing database object using javascript" Watch "Modifing database object using javascript" New topic
Author

Modifing database object using javascript

Joshua Elkino
Ranch Hand

Joined: Jul 24, 2007
Posts: 91
I'm having a dillema.
I'm creating a rating system on my jsp page. I want the user to rate an item and have it recorded in my DB. Right now i can call a JS function but i dont want to do database connection due to security issues. Another requirement is that once rated, the page does not change.

From what i gather my own requirements to be, i should use AJAX. I have never used it and from my understanding its still JS code that can be seen. I'm using struts 1.3 with hibernate 3.2
If anyone has some tips or ideas, let me know.

thanks!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
I'm a bit puzzled by the statement that you don't want a "database connection due to security issues". Browsers can't make database connections. The only ways to get data to a server are a form submit or an AJAX solution.

AJAX (i.e., JavaScript) code can indeed be seen and altered by the user, as can everything else on a web page. What exactly are the security issues you wish to guard against? The data transmission can be over SSL, so it's secure in transit; is it the user you mistrust?


Ping & DNS - my free Android networking tools app
Joshua Elkino
Ranch Hand

Joined: Jul 24, 2007
Posts: 91
as far as i know you CAN have db connections using JS code. I dont want that because of obvious reasons. Is there a way i can call a java method in my project from an JS function?
I think what i want to do is something like a MVC where onclick the rating, there should be a action bean call that does the DB modification and then returns a new value for the rating.
All this needs to happen behind the scene.

If anyone has examples of how i can do this, please link me.
Thanks,
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30595
    
154

Originally posted by Ilya Elkind:
as far as i know you CAN have db connections using JS code.

Server side JavaScript maybe?


Is there a way i can call a java method in my project from an JS function?

No. Javascript on the client can't call Java on the server. You need to make an HTTP request (possibly via AJAX) which invokes a servlet which calls the Java method.


I think what i want to do is something like a MVC where onclick the rating, there should be a action bean call that does the DB modification and then returns a new value for the rating.
All this needs to happen behind the scene.

MVC is going via HTTP, not directly from JavaScript. And AJAX does it behind the scenes.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Just to add a few points to Jeanne's spot-on response:
Originally posted by Ilya Elkind:
as far as i know you CAN have db connections using JS code

Client-side JavaScript cannot establish DB connections. No way. No how.

Is there a way i can call a java method in my project from an JS function?

Only via an Ajax request.

I think what i want to do is something like a MVC

Regardless of whether it's an Ajax request or not, MVC is the way to go.

If you are going to employ Ajax for this, do yourself a favor and familiarize yourself with how Ajax works, but then use a library to perform the nitty-gritty for you. Both prototype and jQuery provide top-notch Ajax support. Lately, I've been favoring the latter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joshua Elkino
Ranch Hand

Joined: Jul 24, 2007
Posts: 91
anyone have links to a good example for using ajax to call a java function? Also would i have to modify any struts configuration files?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Originally posted by Ilya Elkind:
anyone have links to a good example for using ajax to call a java function? Also would i have to modify any struts configuration files?

Ajax cannot directly call a function, it would issue a request just like any other to a servlet, JSP or Struts action, which could in turn call the desired function.

Obviously, if you add a new Struts action to accomplish this, you'd need to update the config file accordingly.

If you are going to be doing this sort of thing frequently, you might want to check out DWR which uses Ajax and servlets as described above to emulate an RPC type of function invocation.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You can also look into http://code.google.com/p/jabsorb/

Eric
Joshua Elkino
Ranch Hand

Joined: Jul 24, 2007
Posts: 91
Thank you all..
I will look into those libraries and find the one that works for me.
 
GeeCON Prague 2014
 
subject: Modifing database object using javascript