Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Modifing database object using javascript

 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also look into http://code.google.com/p/jabsorb/

Eric
 
Joshua Elkino
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all..
I will look into those libraries and find the one that works for me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic