๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
JSP

[JSP] ์‹ค์Šต (Session scope๋ฅผ ์ž๋ฐ”๋นˆ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ)

by ์ฝ”๋”ฉํ•˜๋Š” ๋ถ•์–ด 2021. 2. 25.
๋ฐ˜์‘ํ˜•

์œ„์น˜!!

 

โ€‹

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>

<์‹คํ–‰ ๊ฒฐ๊ณผ>

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€