JSP

[JSP] 실습 (Session scope를 자바빈에서 사용하기)

코딩하는 붕어 2021. 2. 25. 02:09
반응형

위치!!

 

Counter.java

package scope;

public class Counter {
	private int cnt = 0;

	public int getCnt() {
		this.cnt++;
		return cnt;
	}

	public void setCnt(int cnt) {
		this.cnt = cnt;
	}
}

 

 

cntReset.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<jsp:setProperty property="cnt" name="testsession" value="0"/>
<script>
	location.href="<%=request.getParameter("page")%>.jsp";
</script>
</body>
</html>

 

 

sessionScope.jsp (실행 페이지)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>자바빈 session scope 테스트</title>
</head>
<jsp:useBean id="testsession" class="scope.Counter" scope="session"/>
<body>
현재 방문자 수 :
<jsp:getProperty property="cnt" name="testsession"/>
<input type="button" value="초기화"
	onclick="location.href='cntReset.jsp?page=sessionScope';">
</body>
</html>

<실행 결과>

 

반응형