Please tell me if we can call java inside javascript function ?
You can. In JSF you can use PrimeFaces' component p:remoteCommand, which would contain action method. Call that remoteCommand by its name from JS and the java method will get executed.
JSF Page
In JavaScript
function callJava {rmt();}