Tuesday, October 20, 2009

JSP - declare methods and variables from within a jsp page

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
    <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
        <title>JSP Page</title> 
    </head> 
    <body> 
        <%! 
            // You can do real coding inside of here! 
            // The code in this section is called a scriptlet. 
            // Any functions or variables you create can 
            // be used later in the file with jsp expression 
            // tags or shortcuts 
 
            // You'll notice that the section began with a 
            // <%! ......this allows you to declare methods 
 
            String s = "Declare methods and variables with scriptlets.";
 
            int getNum() {
                if (1>0){
                    return 100;
                }
                return 111;
            }
        %> 
 
        <h1><%= s %></h1> 
        <h2><%= getNum() %></h2> 
 
    </body> 
</html> 
 
 

No comments:

Post a Comment