aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Calling a Java function from a href tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Calling a Java function from a href tag" Watch "Calling a Java function from a href tag" New topic
Author

Calling a Java function from a href tag

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Guys,

I do not know if this is the right forum. I would like to know how I could call a java method when clicking on a href element??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Lei Guoguo
Greenhorn

Joined: Mar 31, 2009
Posts: 26
you can use the dwr for Ajax framework,


I'm a chinese, my english is so bad, I need help. If you want to help me, you can send your question to my e-mail: lgg860911@yahoo.com.cn, I will help you, and improve my english. Thanks everyone.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

DWR is pretty invasive and mandates a whole new way of working with web applications. While I think it is a cool project, I don't really recommend it just to be able to perform this sort of thing.

Using a lighter-weight library such as jQuery or Prototype make a lot more sense unless you really, really want to to adopt the "DWR way" of working, and agreeing to the resulting vendor lock-in.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Thanks guys for the option. I'm in a situation wherein I cannot use any frameworks. Got to do it with just jsp and servlets...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Well, there's your solution then. Have the href element link to a servlet which calls the Java method in question.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

I just thought of this way using the servlet mapping tag in the web.xml.
Lei Guoguo
Greenhorn

Joined: Mar 31, 2009
Posts: 26
write the ajax implements for yourself?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Java is on the server and html/JavaScript is on the client.

Your choice is post back the page with the href by clicking on it.
OR
Make an Ajax call with the onclick link to the server.

There is no fancy magic here.

Eric
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

By the way, it is a jspf page and can I use the form element it in?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

Jothi Shankar Kumar wrote:By the way, it is a jspf page and can I use the form element it in?

I have no idea what you are asking.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

I'm using a jsp fragment page in which I have this href element.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Would it be possible to do using a simple form submit. I mean enclosing the a href element in the form tag of html?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

Forms and anchor tags don't have anything to do with each other. Putting an anchor in a form has no special semantic meaning.

I'm still unsure what your question is at this point. The easiest way to invoke Java on the server as a result of a client side action, without the need to submit the whole page, is via an Ajax call to a servlet. What's the issue at this point?
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

There isn' an AJAX framework involved in the project that I#m working. I have to do it with simple javascript or using the servlet mapping.
Balu Sadhasivam
Ranch Hand

Joined: Jan 01, 2009
Posts: 874

Jothi Shankar Kumar wrote:Would it be possible to do using a simple form submit. I mean enclosing the a href element in the form tag of html?


Jothi,

You could call a javascript funciton like this and submit the form



Edit : href='javascript:submitform()' is not shown correctly
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61043
    
  66

Doing Ajax "by hand" is a pain and fraught with pitfalls because of browser nuances. jQuery isn't a "framework" but a JavaScript library that can help you immensely. Saying you can't use it is like saying you can't use any of the Java libraries.

In any case, if you need to use Ajax without jQuery or other library, this article is a good place to start.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a Java function from a href tag