Use page level function : Methods « JSP « Java Tutorial






<%@ page language="java" contentType="text/html" %>
<%!
  String randomColor() {
    java.util.Random random = new java.util.Random();
    int red = (int) (random.nextFloat() * 255);
    int green = (int) (random.nextFloat() * 255);
    int blue = (int) (random.nextFloat() * 255);
    return "#" + 
      Integer.toString(red, 16) + 
      Integer.toString(green, 16) + 
      Integer.toString(blue, 16);
    }
%>
<html>
  <head>
    <title>Random Color</title>
  </head>
  <body bgcolor="white">

    <h1>Random Color</h1>

    <table bgcolor="<%= randomColor() %>" >
      <tr><td width="100" height="100">&nbsp;</td></tr>
    </table>

  </body>
</html>








23.15.Methods
23.15.1.Creating a Method
23.15.2.Declaring Multiple Methods
23.15.3.Using Recursion
23.15.4.Passing Arrays to Methods
23.15.5.Passing the out Object to a Method
23.15.6.Define function before HTML page
23.15.7.Use page level function