Using a Java servlet to generate SVG

The code shows how to use a servlet to generate SVG, in this case demonstrating Greatest Common Divisor properties of number pairs.

<html>
<body>
<% 
	}
%>
<%!
    public static long GCD(long a, long b){
        if (b==0) return a;
        return GCD(b,a%b);
    }
%>
<svg width="1030" height="1080" version="1.1" xmlns="http://www.w3.org/2000/svg" onload="Init()">
 
  <script type="text/ecmascript">
  <![CDATA[
    var evtT1;
    
    function Init(evt)
    {
       evtT1 = document.getElementById("evtText1");
    }
    function Highlight(evt)
    {
       evt.target.setAttribute("opacity", "1.0");
       evtT1.firstChild.nodeValue = "Mouse over: " + evt.target.getAttributeNS(null,"id");
    }
    function Unhighlight(evt)
    {
       evt.target.setAttribute("opacity", "0.8");
       evtT1.firstChild.nodeValue = "";
    }
  ]]>
  </script> 
    <rect width="100%" height="100%" fill="lightgrey"/>    
      <g font-size="16px" font-family="Arial">
        <text x="20" y="20" color="black" id="evtText1" > </text>
      </g>

<%
for (long n=1 ; n< 50; n+=1){
    for ( long m = 1; m < 50 ; m+=1 ){
        long gcd=GCD(n,m);
        if ( gcd < 2 ) {
%>
<rect x="<%= n * 20 %>"  y="<%= 30 + m * 20 %>" width="19" height="19" fill="darkgrey" opacity="0.8"
          onmouseover="Highlight(evt);" onmouseout="Unhighlight(evt);"  id="<%= n %>,<%= m %>" 
          gcd="<%= gcd %>" />
<%
        } else {
%>
<rect x="<%= n * 20 %>"  y="<%= 30 + m * 20 %>" width="19" height="19" fill="darkred" opacity="0.8"
          onmouseover="Highlight(evt);" onmouseout="Unhighlight(evt);"  id="<%= n %>,<%= m %>" 
          gcd="<%= gcd %>"/>
<%
    	}
	}
}
		if (browserType.indexOf("MSIE") > 0) {
%>
</svg>
<%
	}else{
%>
</svg>
</body>
</html>
<%
	}
%>
      
It will show up like (demonstrating 'mouse over'):

alt : It seems your browser does not support SVG. Consider using Google Chrome or another SVG enabled browser

© Jan van der Meiden 2015