jsp:useBean(request)

1762 ワード

[code="java]<%-- ************* RequestBean.jsp ************** --%>
             scope="request"
             class="bean.CounterBean"/>
<%--
<%
bean.CounterBean counterBean = (bean.CounterBean)request.getAttribute("counterBean");
if(counterBean == null) {
counterBean = new bean.CounterBean();
request.setAttribute("counterBean", counterBean);
}
%>
--%>


CounterBean scope="request"Example


<% counterBean.setCount(100); %>

<%--
<% response.sendRedirect("RequestBean2.jsp"); %>
--%>

<%-- ************* RequestBean2.jsp ************** --%>
<jsp:useBean id="counterBean"
             scope="request"
             class="bean.CounterBean" />
<HTML>
<BODY>
<H3>CounterBean scope="request" 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;
	}

	
}