Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a Java function from a href tag

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Lei Guoguo
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use the dwr for Ajax framework,
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64980
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21142
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just thought of this way using the servlet mapping tag in the web.xml.
 
Lei Guoguo
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
write the ajax implements for yourself?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, it is a jspf page and can I use the form element it in?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64980
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using a jsp fragment page in which I have this href element.
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64980
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 874
Android Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64980
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic