Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Modifing database object using javascript

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
author & internet detective
Posts: 41763
885
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67732
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67732
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all..
I will look into those libraries and find the one that works for me.
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic