0

Hi I have a query regarding image reload in a jsp page

My code is :

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Script for auto refresh of graphs with 5 seconds interval -->
 <script type="text/javascript">
 var load, generation, store, EP,transaction,Battery;
 window.onload = function() {

 transaction = document.getElementById("Transaction");
 setInterval(function() {

    transaction.src = transaction.src.replace(/\?.*/, function() {
        return '?' + new Date()
    })
}, 1000)

}
</script>                   
<%!
public String transactionFunc(){
System.out.println("---------transactionFunc------------");
String name=PSMApp.getInstance().notif.getTransaction();
String src = "../Stylesheet/images/EnergyDeficit256.png";
if(name!=null && name.equals("BUY")){
src = "../Stylesheet/images/EnergyDeficit256.png";
}
else if(name!=null && name.equals("SELL")){
src = "../Stylesheet/images/EnergySurplus256.png";
}
return src;
}
%>
<div class="col-md-4">
    <img src=<%=transactionFunc() %>id="Transaction">
</div>


</body>
</html>

when I am trying to reload the image using Javascript above, it is not calling the transactionFunc() which is a jsp function (This function accesses a java class and gets the updated value of source of <img> tag. How to resolve this issue so that transactionFunction() gets called every time the image is reloaded?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Abinaya
  • 23
  • 7

1 Answers1

0

you are trying to call JSP method in JavaScript.

reason JSP files are compiled to .class during compilation and JavaScript scripts executed on client side.

either convert your java code to JavaScript or put you java code in java file. i.e. servlet or another JSP call that method from Ajax and set src in success block of ajax.

OR

instead of java code use JavaScript function for your conditions. it might little changes as per your code and requirement.

 function transactionFunc(){
 var name='<%= PSMApp.getInstance().notif.getTransaction()%>';
var  src = "../Stylesheet/images/EnergyDeficit256.png";
if(name !=null && name=="BUY"){
src = "../Stylesheet/images/EnergyDeficit256.png";
}else if(name!=null && name.equals("SELL")){
src = "../Stylesheet/images/EnergySurplus256.png";
}
return src;
}
KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52