• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method calling using Javascript function

 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Aggarwal Arpit
Ranch Hand
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic