开发者

how to alert browser from javaclass? is it possible?

开发者 https://www.devze.com 2023-02-28 06:05 出处:网络
my scenario is that when ever my target is achieve i want to show alert in browser. while target was checked in javaclass side...

my scenario is that when ever my target is achieve i want to show alert in browser. while target was checked in javaclass side...

i have a method in javascript like.

function targetAchieve(head,target,price)
开发者_JAVA技巧{  
alert(head+"  Target Achieve"+"\n"+"Price:"+price+"\n"+"target: "+target+" \n"  );
}

i want call this method from my java class

ltp = MOConstants.round(ltp, 2);

if(ltp>=target){
    " here i want to call javascript's method"
}

thanks in advance...


Take a look at DWR to make remote calls from a Java web server to a client browser.


you cant do so like this

just make ajax call and check for update at regular interval and do your action


I think you better do polling at some interval for your alerts. Create a separate servlet that handles only polling requests.
Or use web socket.
http://jwebsocket.org/?gclid=CPHkwKaGsKgCFcZ56wodkVsoHA
http://en.wikipedia.org/wiki/WebSockets

Thanx.


I think if you use JSP page to display your content, you can call your java method to check whether you have achieved the target, and then display the your javascript method.

Ex: <% boolean isTargetArchievd = new ClassName().isTargetArchievd(); String javascriptMethod = ""; if(isTargetArchievd){ javascriptMethod = "targetAchieve()"; } %>

//Inside your javascript <%= javascriptMethod %>


As far as I know No its not possible. And even if its possible you should not do it. Don't mix client side and server side coding.

0

精彩评论

暂无评论...
验证码 换一张
取 消