JSP
내장 객체의 영역
H_eh
2022. 7. 11. 15:49
내장 객체의 영역 : page, request, session, application
- 내장 객체의 영역
- 객체의 유효기간
- 객체가 얼마동안이나 살아있는가
- page
- 하나의 jsp페이지를 처리할 때 사용되는 영역
- 한번의 클라이언트 요청에 따라 하나의 jsp 페이지를 범위로 갖음
- pagecontext 라는 내장 객체를 할당 받음
더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
pageContext.setAttribute("name", "세종대왕");
request.setAttribute("name", "태조");
session.setAttribute("name", "정조");
application.setAttribute("name", "연산군");
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("second.jsp").forward(request, response);
%>
<title>first.jsp</title>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<title>second.jsp</title>
<body>
<%=pageContext.getAttribute("name") %><br>
<%=request.getAttribute("name") %><br>
<%=session.getAttribute("name") %><br>
<%=application.getAttribute("name") %><br>
<p> second 페이지로 이동했습니다.</p>
<a href="third.jsp">다른 페이지</a>
</body>
<!-- null / 태조 / 정조 / 연산군 -->
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<title>thrid</title>
<body>
<%=pageContext.getAttribute("name") %><br>
<%=request.getAttribute("name") %><br>
<%=session.getAttribute("name") %><br>
<%=application.getAttribute("name") %><br>
<p> third 페이지로 이동했습니다.</p>
</body>
<!-- null / null / 정조 / 연산군 -->
- request
- 하나의 요청을 처리할 때 사용되는 영역
- 브라우저의 주소창에 url 입력 / 페이지 링크 클릭할 때 브라우저가 웹 서버에 전송하는 요청
- 브라우저가 요청 할 때마다 새로운 request 내장 객체가 생성되고 매번 새로운 request 영역이 생성됨
- 브라우저가 결과를 받으면 그 요청과 관련된 request 내장 객체를 사라진다
- page 와 request 영역의 차이점
- page : 오직 하나의 jsp 페이지만 포함
- request : 하나의 요청을 처리하는데 있어 사용되는 모든 jsp 페이지를 포함
- session
- 웹 브라우저를 닫기 전까지 페이지를 이동해도 사용자 정보를 잃지 않고, 서버에 보관할 수 있도록 한 객체
- 모든 웹 서버에서 제공됨
- ex) 로그인 인증 처리 - 로그인 권한 상태 유지
더보기
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<title>session01</title>
<body>
<%
String name = "세종대왕";
// 세션을 이용해서 데이터를 유지
session.setAttribute("name1", name);
//세션 유지 시간
//session.setMaxInactiveInterval(60*60); //60분. 초단위
session.setMaxInactiveInterval(10);
%>
<a href="session.jsp?name=<%=name %>">페이지 이동</a>
<!-- <a href="session.jsp?name=<%=URLEncoder.encode(name, "UTF-8")%>">session</a> -->
</body>
<title>session</title>
<body>
<%
request.setCharacterEncoding("UTF-8");
String name1 = request.getParameter("name");
String name2 = (String)session.getAttribute("name1");
%>
<p><%=name1 %>님 반갑습니다</p>
<p><%=name2 %>님 로그인 됐습니다.</p>
</body>
- application
- 하나의 웹과 관련된 전체 영역 포함
- 하나의 웹 앱에 속한 모든 페이지, 그 페이지에 대한 요청, 세션은 모두 하나의 application 영역에 속함
- jsp 내장 객체에서 정보 주고 받는 메서드
- setAttribute(), getAttribute(), getAttributeName(), removeAttribute()
728x90
728x90