웹 Web/JSP & Servlet

JSP 내장 객체 (3) - Application 내장 객체

하나비 HANABI 2022. 1. 24. 21:38

p.203

컨텍스트 이름 혹은 프로젝트 내 JSP파일의 경로를 알고싶을 때 사용하는 메소드

getServerInfo() : 컨테이너의 이름과 버전 반환

getContextInfo() : 웹 애플리케이션의 URL경로 중 컨텍스트 패스명을 반환

getRealPath() : JSP의 실제 경로 반환

getMimeType(finename) : 지정된 파일의 MIME 탕비을 반환

log(message) : 지정된 message의 로그를 저장

 

p.204

내장객체의 영역

page: 하나의 JSP페이지를 처리할 때 사용되는 영역

request: 하나의 요청을 처리할 때 사용되는 영역

session: 하나의 브라우저와 관련된 영역

application: 하나의 웹 애플리케이션과 관련된 영역

 

<07_firstPage.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>
<%
pageContext.setAttribute("name", "page man");
request.setAttribute("name", "request man");
session.setAttribute("name", "session man");
applicaton.setAttribute("name", "application man");

System.out.println("firstPage.jsp");
System.out.println("하나의 페이지 속성: " + pageContext.getAttribute("name"));
System.out.println("하나의 요청 속성: " + request.getAttribute("name"));
System.out.println("하나의 세션 속성: " + session.getAttribute("name"));
System.out.println("하나의 애플리케이션 속성: " + application.getAttribute("name"));
request.getRequestDispatcher("07_secondPage.jsp").forward(request, response);

%>
</body>
</html>

firstPage.jsp: 
하나의 페이지 속성: page man
하나의 요청 속성: request man
하나의 세션 속성: session man
하나의 애플리케이션 속성: application man

firstPage에서 secondPage로 포워드 방식을 사용해 이동

 

<07_secondPage.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>
하나의 페이지 속성: <%= pageContext.getAttribute("name") %> <br>
하나의 요청 속성: <%= request.getAttribute("name") %> <br>
하나의 세션 속성: <%= session.getAttribute("name") %> <br>
하나의 애플리케이션 속성: <%= application.getAttribute("name") %> <br>
</body>
</html>

하나의 페이지 속성: null
하나의 요청 속성: request man
하나의 세션 속성: session man
하나의 어플리케이션 속성: application man

secondPage에서 thirdPage로 redirect방식을 사용해 이동

 

<07_thirdPage.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>
<%
pageContext.setAttribute("name","page man");
request.setAttribute("name","request man");
session.setAttribute("name","session man");
application.setAttribute("name","application man");

System.out.println("firstPage.jsp: ");
System.out.println("하나의 페이지 속성: " + pageContext.getAttribute("name"));
System.out.println("하나의 요청 속성: " + request.getAttribute("name"));
System.out.println("하나의 세션 속성: " + session.getAttribute("name"));
System.out.println("하나의 애플리케이션 속성: " + application.getAttribute("name"));

request.getRequestDispatcher("47_secondPage.jsp").forward(request, response);
%>
</body>
</html>

하나의 페이지 속성: null
하나의 요청 속성: null
하나의 세션 속성: session man
하나의 어플리케이션 속성: application man