• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Calling a Java function from a href tag

 
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE 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??
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use the dwr for Ajax framework,
 
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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: 10191
3
Mac PPC Eclipse IDE 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...
 
Marshal
Posts: 24594
55
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: 10191
3
Mac PPC Eclipse IDE 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?
 
author
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: 10191
3
Mac PPC Eclipse IDE 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
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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: 10191
3
Mac PPC Eclipse IDE 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: 10191
3
Mac PPC Eclipse IDE 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
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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: 10191
3
Mac PPC Eclipse IDE 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.
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • 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
Sheriff
Posts: 67269
170
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!