JSP/Javascript event catcher  
Author Message
morc





PostPosted: 2006-2-10 3:36:00 Top

java-programmer, JSP/Javascript event catcher hi,
i was wondering how to call a function upon clicking of a button... i
have abutton that uses some javascript to unhide a div... but at the
same time i would like it to call a method not part of javascript.

<form>
<input type="button" value="Generate Code"
onClick="display('code');"'<%UserBean.trackModels("n3",user,1);%>'"">
</form>


that is the code i tried... the javascript function works properly but
im unsure how to have the jsp method called.

i tried a system out println right after the calling trackModels and it
seems to execute when the page loads rather then when the button is
clicked. and the trackModels method doesn't get called at all.

any and all help is appreciated
thanks alot
-morc

 
Oliver Wong





PostPosted: 2006-2-10 3:45:00 Top

java-programmer >> JSP/Javascript event catcher
"morc" <email***@***.com> wrote in message
news:email***@***.com...
> hi,
> i was wondering how to call a function upon clicking of a button... i
> have abutton that uses some javascript to unhide a div... but at the
> same time i would like it to call a method not part of javascript.
>
> <form>
> <input type="button" value="Generate Code"
> onClick="display('code');"'<%UserBean.trackModels("n3",user,1);%>'"">
> </form>
>
>
> that is the code i tried... the javascript function works properly but
> im unsure how to have the jsp method called.
>
> i tried a system out println right after the calling trackModels and it
> seems to execute when the page loads rather then when the button is
> clicked. and the trackModels method doesn't get called at all.
>
> any and all help is appreciated
> thanks alot

When the page is downloaded, the JSP has finished executing. No
connection is maintained between the server and the browser. After the JSP
has finished executing, and the page is downloaded, the browser will parse
the page, notice the JavaScript, and start running it. Again, the JSP has
finished executing, so it's too late to try to run further JSP code.

The only workaround is to have your javascript ask the browser to send a
new request to the server, where a new invocation of your JSP code will
occur.

- Oliver


 
morc





PostPosted: 2006-2-10 3:57:00 Top

java-programmer >> JSP/Javascript event catcher thanks that explains alot.

how do you suggest i should have the javascript reload the page but
also call the jsp method??

 
 
morc





PostPosted: 2006-2-10 4:00:00 Top

java-programmer >> JSP/Javascript event catcher is ther anyway i can call it in maybe form action???

 
 
Oliver Wong





PostPosted: 2006-2-10 4:08:00 Top

java-programmer >> JSP/Javascript event catcher
"morc" <email***@***.com> wrote in message
news:email***@***.com...
> thanks that explains alot.
>
> how do you suggest i should have the javascript reload the page but
> also call the jsp method??

I don't know much JavaScript. You might want to try asking this question
on a JavaScript related newsgroup. What you would want to do, at a very high
level, is to make a new request using JavaScript. Depending on the situation
and what is possible in JavaScript, you may wish to put information in the
GET or POST headers to explain to the JSP code exactly what it is you want
it to do. For example, perhaps pass in the name of the method you want to
have invoked.

- Oliver


 
 
morc





PostPosted: 2006-2-10 5:33:00 Top

java-programmer >> JSP/Javascript event catcher anyways thanks for kinda showing me hte light.

i found my self a solution by makin the button a submit and sending it
to another jsp page where it runs the method an redirects the user
back. thanks laot

 
 
Hal Rosser





PostPosted: 2006-2-10 9:00:00 Top

java-programmer >> JSP/Javascript event catcher
"morc" <email***@***.com> wrote in message
news:email***@***.com...
> hi,
> i was wondering how to call a function upon clicking of a button... i
> have abutton that uses some javascript to unhide a div... but at the
> same time i would like it to call a method not part of javascript.
>
> <form>
> <input type="button" value="Generate Code"
> onClick="display('code');"'<%UserBean.trackModels("n3",user,1);%>'"">
> </form>
>
>
> that is the code i tried... the javascript function works properly but
> im unsure how to have the jsp method called.
>
> i tried a system out println right after the calling trackModels and it
> seems to execute when the page loads rather then when the button is
> clicked. and the trackModels method doesn't get called at all.
>
Oliver's is right. The javascript executes AFTER it gets to the user's
browser. The JSP code executed on the server.
JSP can see what the form element's values are at the time it gets the http
request (when the web page is called). You can only send another http
request to the jsp page.
Maybe you could have the javascript change a form element's value before you
submit the form so the JSP will respond to it in that way.