jsp:useBean(session)

<%-- *************** SessionBean.jsp **************** --%>


<jsp:useBean id="counterBean"
             scope="session"
             class="bean.CounterBean" />
             

<%--
<%
bean.CounterBean counterBean = (bean.CounterBean)session.getAttribute("counterBean");
if(counterBean == null) {
	counterBean = new bean.CounterBean();
	session.setAttribute("counterBean", counterBean);
}
%>
--%>
<HTML>
<BODY>
<H3>CounterBean scope="session" Example</H3>
<B>Count:</B> <%= counterBean.getCount() %>
</BODY>
</HTML>


<%-- ************** SessionBean2.jsp **************** --%>
<jsp:useBean id="counterBean"
             scope="session"
             class="bean.CounterBean" />
<HTML>
<BODY>
<H3>CounterBean scope="session" Example</H3>
<B>Count:</B> <%= counterBean.getCount() %>
</BODY>
</HTML>


//=================== CounterBean.java =================
package bean;

import java.io.Serializable;

@SuppressWarnings("serial")
public class CounterBean implements Serializable {
	private int count = 0;

	public CounterBean() {
	}

	public int getCount() {
		count ++;
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	
}

你可能感兴趣的:(java,jsp,bean)