aspose file tools*
The moose likes Servlets and the fly likes Href with onClick Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Href with onClick" Watch "Href with onClick" New topic
Author

Href with onClick

Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
I tried using href with onClick but my method consumedLO never reached I used the following code:



it displays everything and when clicked it opens the hyperlink but never go to the method. I create a test method that only displays message.

Any help is appreciated
Minh Nam
Ranch Hand

Joined: Sep 10, 2011
Posts: 57
Nada Bajnaid wrote:I tried using href with onClick but my method consumedLO never reached I used the following code:


is consumedLO Java method or Javascript one? If you want to call Java method in onClick, it is impossible because onClick can call only Javascript.


Advanced java topics
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

I also think that javascript and backend java are mixed here
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
consumedLO is a java method. then how I can call a java method when clicking a hyperlink
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

You can't.

basicaly, you will need a servlet.
That servlet can call your java method.

And the servlet can be "called" by your browser(html) trough href.
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
In my application I display several learning objects as hyperlinks that when clicked will call out resources (some websites)

I need to do some actions when a learning object is selected before browsing the website. To be more clear a learning object will be considered consumed when clicked. So what is the way to combine these two actions?

Thanks
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
Can I call a servlet that will call the java method then go to the website without any more clicks. In this case I will pass the address of the website to the server through the href.

Is this the right way?
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

"(some websites) " are external websites?
Asume that your consumedLO object works with keys (a key matches an url), you can pass that key via your servlet, to your consumedLO instance.
You do your stuf you want to do.
Then, do a response-redirect to the url that matches the key.

this is what you want?
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

Nada Bajnaid wrote:Can I call a servlet that will call the java method then go to the website without any more clicks. In this case I will pass the address of the website to the server through the href.

Is this the right way?


or, indeed, you can pass the address of the website to the server through the href.
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
Yes it external website. Can I call another servlet passing the address of the link through the href. then in the new servlet I call the java method passing to it this address. After that I use go to go to the link.
Is it right way?
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
Thanks Olivier
then in the server can I use go to call a website directly without any further clikc?
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

Nada Bajnaid wrote:Yes it external website. Can I call another servlet passing the address of the link through the href. then in the new servlet I call the java method passing to it this address. After that I use go to go to the link.
Is it right way?


its a solution.

But why 2 servlets?
1 servlet will do.

from your browser, after click, request with the url as parameter comes in the servlet.
you do your stuff with you java methods
and then you do a response sendredirect (from within your servlet of course) to the url that was send as parameter.

so , 1 servlet.

Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
all these items are in the doPost method of the servlet this is why I think I will need another servlet. Or you mean that I can creat a new method in the servlet that will do my consumedLO stuff and sendredirect to the passed link after that it will return control back to the calling point at the servlet? do I misunderstand something?
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

inside your dopost method you do first your consumedLO stuff
right after that , still in the same dopost,
you do a response.sendredirect(the_url_that_was_given);

nothing more...
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
Thanks alot
I will try it this way
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
I decided to embedded my function as a javascript in my servlet. In my doPost method I did the following:

out.println("<head>"
out.println("<script language ='JavaScript' type = 'text/javascript'>function consumedLO(name,lo){");
out.println(name + " has consumed " + lo);
out.println("}");

out.println("</script>");
out.println("</head>");

then the <body> tag contains other stuff that will call my method conumedLO with onClick

is this a correct code? if not how can I embeded a function as java script in a servlet and how to pass parameters to this function

Thanks
Prasad Krishnegowda
Ranch Hand

Joined: Apr 25, 2010
Posts: 587
    
    3

You should use servlets only for data processing and leave the presentation to the Front End(HTML, JSP or whatever it is).
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Href with onClick