Forums Register Login

Href with onClick

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
I also think that javascript and backend java are mixed here
+Pie Number of slices to send: Send
consumedLO is a java method. then how I can call a java method when clicking a hyperlink
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
"(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?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
Thanks Olivier
then in the server can I use go to call a website directly without any further clikc?
+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
Thanks alot
I will try it this way
+Pie Number of slices to send: Send
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
1
+Pie Number of slices to send: Send
You should use servlets only for data processing and leave the presentation to the Front End(HTML, JSP or whatever it is).
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 4655 times.
Similar Threads
Passing data to and from a jsp page
Calling a backingBean method from javascript
Frames in struts
Facing problem to pass a quoted String as a parameter in javascript function
Calling JSF bound method via javascript
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 04:45:13.