• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a java method inside of a JavaScript function

 
Gihan Pandigamage
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Friends,
I have a javascript function called test



so I need to call my another java method called 'validate' inside of that javascript. how can I do this?


Please help...




 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to make an Ajax call to call methods on the server.

Eric
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

javascript is a client side technology and you cannot access server side methods directly through it. So, you need to use some framework , one of which is ajax, to do your stuff

Vishal
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ajax is NOT a framework.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this article to understand how JSP and server-side Java works and why you can;t just make a simple Java call and must use Ajax.
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Pascarello wrote:Ajax is NOT a framework.

Eric


Ajax is a framework. Please check this and this . There are many more, so I stop here

Vishal
 
Paul Clapham
Sheriff
Posts: 20983
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vishal Shaw wrote:Ajax is a framework. Please check this...


I checked that. And it says

an Ajax framework is a framework which leverages Ajax


In other words, it completely fails to say that Ajax is a framework. I assume the other links are just as unreliable.
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Vishal Shaw wrote:Ajax is a framework. Please check this...


I checked that. And it says

an Ajax framework is a framework which leverages Ajax


In other words, it completely fails to say that Ajax is a framework. I assume the other links are just as unreliable.


So please correct me, shall I call it Technology?

Over here, I heard most of them calling it a framework, and I also got the feeling of using a framework while using ajax.

I felt that ajax is a frmework that can be used with a variety of technologies like asp, java etc.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ajax is part of JavaScript. It is not a framework. Often, a framework is used to make Ajax easier to use, but that does not make Ajax itself a framework.
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Ajax is part of JavaScript. It is not a framework. Often, a framework is used to make Ajax easier to use, but that does not make Ajax itself a framework.


Thanks Bear !!!

Yes, Ajax is call of asynchronous javascript, but my only intention in calling it a framework is because the underlying technology was javascript and it was like working with a framework (logically of course). But now I guess I was technically wrong, as it does not meet the technical specification of a framework .

Thanks for correcting me.
 
Rajdeep Biswas
Ranch Hand
Posts: 216
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all. I have a JSP page. I want to call and get value from a Java method from inside a Javascript function inside the jsp. I have browsed and found no easy to understand reply, and that I do not know AJAX, so I thought to post it here. Please help. Thanks.

Info: I am a demo web project in MyEclipse local PC, and its deployed in Tomcat.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As already pointed out, you cannot call a Java method from JavaScript, even in a JSP. Please read this article to understand how JSP operates and why Java is not available in the JavaScript on a JSP page.

In order to have JavaScript obtain values from the server, you must use Ajax to create a request to the server that can invoke a servlet that will return the desired values as the response.

If you are working with web applications, you must learn Ajax. It's a required part of the technology set required to write modern web apps.

P.S. Using jQuery makes using Ajax much simpler.
 
Rajdeep Biswas
Ranch Hand
Posts: 216
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright. You mean through using XMLHTTPRequest...I am finding out.
 
Rajdeep Biswas
Ranch Hand
Posts: 216
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to find out. My little app is deployed in tomcat server, and I just want to get a value evaluated from a simple Java method (not Servlet) after calling it from a javascript function inside a JSP page.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajdeep Biswas wrote:I just want to get a value evaluated from a simple Java method (not Servlet) after calling it from a javascript function inside a JSP page.

Well you can't. Period. End of story. And until you let go of the notion that you can, you will meet with failure.

You'll need to use Ajax to invoke a servlet that will call the method and serialize its value as the response.
 
Rajdeep Biswas
Ranch Hand
Posts: 216
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright! I got it. Thank you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic