applicationVisitCount.jsp

moon.kick·2025년 4월 15일
0

물론이죠! applicationVisitCount.jsp 파일은 웹사이트에 접속할 때마다 방문자 수를 ServletContext (즉, application 객체) 에 저장하여 누적시키는 예제입니다.

아래는 applicationVisitCount.jsp의 전체 코드입니다.


📄 applicationVisitCount.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
    // 방문자 수를 저장하는 변수 이름
    String attributeName = "visitCount";

    // application 객체에서 방문자 수 가져오기
    Integer count = (Integer)application.getAttribute(attributeName);

    // 처음 방문이면 1로 초기화
    if (count == null) {
        count = 1;
    } else {
        count++;
    }

    // 다시 application 객체에 저장
    application.setAttribute(attributeName, count);
%>

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>방문자 수 세기</title>
</head>
<body>
    <h1>🌐 Application 방문자 수 확인</h1>
    <p>현재까지 누적 방문자 수는 <strong><%= count %></strong>명입니다.</p>
</body>
</html>

✅ 동작 원리 요약

  1. application.getAttribute("visitCount") 로 저장된 방문자 수를 가져옴.
  2. 없으면 1로 시작하고, 있으면 1 증가.
  3. 다시 application.setAttribute() 로 저장해서 전체 사용자 공유.

이제 이 파일을 톰캣 서버의 JSP 프로젝트에 추가하고 브라우저에서 새로고침할 때마다 카운트가 올라가는 걸 확인하실 수 있어요!
원하시면 방문자 수를 초기화하는 resetVisitCount.jsp 도 만들어드릴게요 :)

profile
@mgkick

0개의 댓글