my dog learned polymorphism*
The moose likes HTML, CSS and JavaScript and the fly likes Method calling using Javascript function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Method calling using Javascript function" Watch "Method calling using Javascript function" New topic
Author

Method calling using Javascript function

Aggarwal Arpit
Ranch Hand

Joined: Mar 04, 2012
Posts: 86
Hi,
I have a href link on my jsp page. My requirement is that on clicking that href link,a javascript should be called which further call some method in some class.
That method will make a database query and insert value=1 in an id column of User table.

How can I implement it?

Thanks in Advance for your help
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

Aggarwal Arpit wrote:I have a href link on my jsp page. My requirement is that on clicking that href link,a javascript should be called

Why the JavaScript? The URL of the link can reference a server-side action to perform the required steps. I don;'t see where JavaScript needs to be involved at all unless you have a requirement that the page needs to not be refreshed.

In that case, establish a click handler for the link. Be sure to return false as the result to prevent the default action from taking place.

which further call some method in some class.

I assume you mean a Java class on the server? You can't. You'll need to fire off an Ajax request to communicate with the server.

That method will make a database query and insert value=1 in an id column of User table.

The server code responding to the Ajax request can do whatever it wants.

Both event handling and Ajax are much easier to accomplish using jQuery rather than trying to do it with raw JavaScript.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Aggarwal Arpit
Ranch Hand

Joined: Mar 04, 2012
Posts: 86
Hi Bear.
Thanks for your response.Actually what I done is I am calling one javscript function from my href link.In that function I am trying to call a method defined in my local class,
say,

Class 1{

public void insert()
{
}


}

But I am not able to do so.Should I need to use AJAX for it?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61449
    
  67

Aggarwal Arpit wrote:In that function I am trying to call a method defined in my local class,

What is a "local" class?

Again, you are not giving us any details that we need to answer the question.

But I am not able to do so.

Read ItDoesntWorkIsUseless.

Should I need to use AJAX for it?

Again, if you are talking about a Java class on the server -- and we have no way of knowing because you haven't given us any details -- then yes.
Aggarwal Arpit
Ranch Hand

Joined: Mar 04, 2012
Posts: 86
Hi Bear,
Sorry for late reply actually I am not clear of the fact that class is my server class.
Now I done with that using Ajax call.I am sharing my code might be it's useful for others

function success() {
$.ajax({
url : "/package/class?method=methodName",
cache : false,
type : 'POST',
success : function() {
alert("Added Successfully");
},
error:function(){
alert("Error");
}
});
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method calling using Javascript function