[JSP/Servlet] 자바빈으로 Count 구하는 예제

counter.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"%>

<!DOCTYPE html>

<html>

<head>

<title>Insert title here</title>

</head>

<body>

<h1>전체 방문자 수 확인</h1>

<jsp:useBean id="counter" class="mybean.scope.CounterBean" scope="application">

<jsp:setProperty name="counter" property="newVisit" value="1"/>

</jsp:useBean>

<!-- 카운터 리셋 -->

<jsp:setProperty property="restart" name="counter"/>

<h2>전체 방문자 수 : <jsp:getProperty property="visitCount" name="counter"/></h2>

<form method="post">

<input type="submit" name="refresh" value="새로고침">&nbsp;&nbsp;&nbsp;

<input type="submit" name="restart" value="카운터를 0으로 리셋"/>

</form>

</body>

</html>


CounterBean.java

package mybean.scope;


public class CounterBean {

int visit = 0;

public void setNewVisit(int num) {

visit = num;

}

public void setRestart(boolean b) {

visit = 0;

}

public int getVisitCount() {

return visit++;

}

}


결과


댓글

Designed by JB FACTORY