Define function before HTML page : Methods « JSP « Java Tutorial






<%@ page language="java" contentType="text/html" %>
<%@ page import="java.util.Date" %>
<%!
  private String getGreeting() {
    Date now = new Date();
    String greeting = null;
    if (now.getHours() < 12) {
      greeting = "Good morning";
    }
    else if (now.getHours() < 18) {
      greeting = "Good day";
    }
    else {
      greeting = "Good evening";
    }
    return greeting;
  }
%>
<html>
  <head>
    <title>All Scripting Elements</title>
  </head>
  <body bgcolor="white">
    <%= getGreeting() %>
    <% if (request.getParameter("name") == null) { %>
      stranger!
    <% } else { %>
      partner!
    <% } %>
    How are you?
  </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