This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Calling a java method inside of a JavaScript function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Calling a java method inside of a JavaScript function " Watch "Calling a java method inside of a JavaScript function " New topic
Author

Calling a java method inside of a JavaScript function

Gihan Pandigamage
Ranch Hand

Joined: Aug 23, 2009
Posts: 60
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
You need to make an Ajax call to call methods on the server.

Eric
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
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


Programming is about thinking, NOT coding
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Ajax is NOT a framework.

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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

Joined: Aug 09, 2012
Posts: 179
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

Joined: Jan 10, 2002
Posts: 61106
    
  66

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

Joined: Aug 09, 2012
Posts: 179
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

Joined: Mar 26, 2012
Posts: 186

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.


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

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

Joined: Mar 26, 2012
Posts: 186

Alright. You mean through using XMLHTTPRequest...I am finding out.
Rajdeep Biswas
Ranch Hand

Joined: Mar 26, 2012
Posts: 186

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

Joined: Jan 10, 2002
Posts: 61106
    
  66

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

Joined: Mar 26, 2012
Posts: 186

Alright! I got it. Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Calling a java method inside of a JavaScript function