Java examples for Servlet JSP:JSP
Using a Bean in a JSP Page
<jsp:useBean id="myBean" class="com.mycompany.MyBean" scope="session" /> <jsp:useBean id="myBean2" class="com.mycompany.MyBean" scope="session"> <%-- this body is executed only if the bean is created --%> <%-- intialize bean properties --%> <jsp:setProperty name="myBean2" property="prop1" value="123" /> </jsp:useBean> <jsp:getProperty name="myBean" property="prop1" /> <jsp:getProperty name="myBean" property="prop2" /> <% if (myBean.getProp1() % 2 == 0) { out.println("<font color=\"red\">"+myBean.getProp1()+"</font>"); } else { out.println("<font color=\"green\">"+myBean.getProp1()+"</font>"); } %> package com.mycompany; public class MyBean { // Initialize with random values int prop1 = 123; String prop2 = "java2s.com"; public int getProp1() { return prop1; } public void setProp1(int prop1) { this.prop1 = prop1; } public String getProp2() { return prop2; } public void setProp2(String prop2) { this.prop2 = prop2; } }