본문 바로가기
JSP

221201 application

application 기본 객체
특정 웹 어플리케이션에 포함된 모든 jsp페이지는 하나의 application기본 객체를 공유한다.
이 객체는 웹 어플리케이션 전반에 걸쳐서 사용되는 정보를 담고 있음
 
setAttribute
getAttribute
removeAttribute

생명주기가 조금씩 다름
request객체는 요청영역마다(페이지별로) 생성되고,
session객체는 브라우저별로 생성되고(브라우저를 끄면 사라짐),
application은 프로그램 전체에서 딱 한번 최초 가동시 생성된다(tomcat-서버를 끌 때까지 유지된다).

 

app_basic

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	/*
	application은 session과 사용방법은 거의 동일하며
	생명주기가 톰캣을 stop할때 까지 1개 유지가 된다.
	*/
	
	int total=0;
	
	if(application.getAttribute("total")!=null){
		total=(int)application.getAttribute("total");
        //무슨 뜻? total값의 누적. 새로고침할 때마다 값이 증가한다. 브라우저를 꺼도 증가값이 유지된다.
	}
	
	total++;
	
	application.setAttribute("total", total);//저장
%>    
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="app_basic_ok.jsp">total값 확인</a>
	app에 유지되는 total 값 : <%=total%> 
</body>
</html>

새로고침을 할 때마다 total값이 증가하며, 브라우저를 꺼도 total값은 유지된다.

application이기 때문에 서버가 꺼지기 전까지 값이 사라지지 않는다.

 

app_basic_ok

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
	int total = (int)application.getAttribute("total");

	//삭제는 removeAttribute
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	app에 유지되는 total값:<%=total%>
</body>
</html>

 

applicaion에 사용자정보를 저장하면 안된다. 덮어씌워짐. 다양한 사용자가 접속할때마다 덮어씌워지는 것
가령 a사용자가 접속하면 a의 정보가 저장되지만 b사용자가 접속하면 b의 정보가 덮어씌워진다.

'JSP' 카테고리의 다른 글

221201 jsp error 예외 페이지  (0) 2022.12.01
221201 jsp 경로  (1) 2022.12.01
221201 session실습 redirection추가  (0) 2022.12.01
221130 session실습  (0) 2022.11.30
221130 session  (0) 2022.11.30