Java Web4 - application객체, scope
application 객체
웹 어플리케이션 서버(WAS)에 대한 정보를 관리하는 객체다. 서버에서 동작하는 어플리케이션 하나 당 application 객체 하나가 생성된다. 즉 프로젝트 1개 당 application객체 1개가 생성된다. 서버가 시작되면 application객체가 생성되고 서버가 중지되면 객체가 제거된다.
// m서버 정보
<%=application.getServerInfo() %>
// 서버 루트 경로의 물리적 경로 정보
<%=application.getRealPath("/") %>
// 서버 컨텍스트(프로젝트) 경로 정보
<%=application.getContextPath() %>
scope
JSP의 4대 영역에는 page, request, session, application이 있다. 각 영역에 대응되는 객체는 pageContext, request, session, application객체들이 있다. page에서 application으로 갈수록 범위가 넓어진다.
- page 영역(pageContext 객체)
현재 페이지에서만 유효(페이지 이동 시 객체 제거됨) - request 영역(request 객체)
요청에 대한 응답까지 유효(새로운 요청 발생 시 객체 제거됨) - session 영역(session 객체)
세션 유지 조건까지 유효(세션 제거 조건 발생 시 객체 제거됨) - application 영역(application 객체)
서버 동작 시에만 유효(서버 중지 시 객체 제거됨)
공통 메서드로는 다음이 있다.
- getAttirbute(String)
영역 객체에 저장된 속성값 리턴 - setAttirbute(String, Object)
영역 객체에 지정된 속성명을 갖는 데이터 저장 - removeAttribute(String)
지정된 속성 제거(데이터 포함)
<%
// 4대 영역 객체에 속성값 저장하기
pageContext.setAttribute("pageValue", "pageContext value");
request.setAttribute("requestValue", "request value");
session.setAttribute("sessionValue", "session value");
application.setAttribute("applicationValue", "application value");
%>
// 4대 영역 객체에 저장된 값을 동일한 페이지에서 확인하기
// page 영역(pageContext 객체) 값
<%=pageContext.getAttribute("pageValue") %>
// request 영역(request 객체) 값
<%=request.getAttribute("requestValue") %>
// session 영역(session 객체) 값
<%=session.getAttribute("sessionValue") %>
// application 영역(application 객체) 값
<%=application.getAttribute("applicationValue") %>
redirect 방식
// response 객체의 sendRedirect() 메서드를 호출하여 이동 : Redirect 방식 포워딩
response.sendRedirect("#");
// pageContext, request 객체 : 데이터 없음(null 값 출력됨)
// session, application 객체 : 데이터 있음
리다이렉트 방식으로 포워딩을 수행할 경우, 기존 요청과 다른 새로운 요청이 발생하여 기존 request 객체가 제거되고, 새로운 request 객체가 생성되므로 기존 request 객체에 저장된 정보도 제거된다. 따라서 null 값이 출력된다.
dispatch 방식
// pageContext 객체의 forward() 메서드를 호출하여 이동 : Dispatch 방식 포워딩
pageContext.forward("#");
// pageContext 객체 : 데이터 없음(null 값 출력됨)
// request, session, application 객체 : 데이터 있음
디스패치 방식으로 포워딩을 수행할 경우, 기존 요청이 그대로 유지된 채 포워딩을 수행하므로 기존 request 객체에 저장된 정보가 그대로 유지되어 기존 request 객체에 접근이 가능하다.
Leave a comment