Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- ㅁㅇㅂ??ㅇㅈㄷ ㅎㅇㅌ...
- riceteacatpanda
- 정보보호병 후기
- SessionAttribute
- restapi
- Forensic 절차
- reversing.kr
- 메모리 포랜식
- react
- webhacking 처음
- 네이버 인턴
- JSTL
- 3단계 지역 DB
- Layered Architecture
- 인턴 지원
- mysql
- frontend
- 소개딩
- 동읍면 DB
- spring
- 행정지역 DB
- 소프트웨어 개발보안 경진대회
- 방명록 만들기
- Django
- Database
- DBMS
- 인턴 후기
- jsp
- PyAmdecoder
- EER
Archives
- Today
- Total
웹찢남
Spring - Scope 본문
웹 프로그래밍 중 약속된 객체들이 존재하고 이 객체의 범위가 존재함
Scope는 Application, Session, Request, Page로 나뉨
- Application : 하나의 어플리케이션 생성되고 소멸될 때까지 변수가 유지되는 경우 사용
- Session : 웹 브라우저 별로 변수가 관리되는 경우 사용
- Request : http요청을 WAS가 받아서 웹 브라우저에게 응답할 때까지 변수가 유지되는 경우 사용
- Page : 페이지 내에서 지역변수처럼 사용
Page Scope - 특정 서블릿이나 JSP가 실행되는 동안에만 정보를 유지하고 싶을 때 사용
- PageContext 추상 클래스를 사용한다.
- JSP 페이지에서 pageContext라는 내장 객체로 사용 가능 하다.
- forward가 될 경우 해당 Page scope에 지정된 변수는 사용할 수 없다.
- -> 이 페이지가 실제 실행되는 동안만 사용할 수 있다.
- 사용방법은 Application scope나 Session scope, request scope와 같다.
- 마치 지역변수처럼 사용된다는 것이 다른 Scope들과 다르다.
- jsp에서 pageScope에 값을 저장한 후 해당 값을 EL표기법 등에서 사용할 때 사용된다.
- 지역 변수처럼 해당 jsp나 서블릿이 실행되는 동안에만 정보를 유지하고자 할 때 사용된다.
- pageContext.setAttribute, pageContext.getAttribute 같이 사용 가능
Request Scope - 웹 브라우저로부터 WAS가 요청을 받은 후 포워드 되는 동안 유지하고 싶은 정보가 있을때 사용
- http 요청을 WAS가 받아서 웹 브라우저에게 응답할 때까지 변수값을 유지하고자 할 경우 사용한다.
- HttpServletRequest 객체를 사용한다.
- JSP에서는 request 내장 변수를 사용한다.
- 서블릿에서는 HttpServletRequest 객체를 사용한다.
- 값을 저장할 때는 request 객체의 setAttribute()메소드를 사용한다.
- 값을 읽어 들일 때는 request 객체의 getAttribute()메소드를 사용한다.
- forward 시 값을 유지하고자 사용한다.
- 앞에서 forward에 대하여 배울 때 forward 하기 전에 request 객체의 setAttribute() 메소드로 값을 설정한 후, 서블릿이나 jsp에게 결과를 전달하여 값을 출력하도록 하였는데 이렇게 포워드 되는 동안 값이 유지되는 것이 Request scope를 이용했다고 합니다.
- RequestScope의 사용 목적은 포워드 시에 값을 유지하고자 할 때 사용
- request.setAttribute, request.getAttribute 같이 사용 가능
Session Scope - 접속한 웹 브라우저별로 정보를 관리하고 싶을때 사용
- 웹 브라우저별로 변수를 관리하고자 할 경우 사용한다.
- 웹 브라우저간의 탭 간에는 세션정보가 공유되기 때문에, 각각의 탭에서는 같은 세션정보를 사용할 수 있다.
- HttpSession 인터페이스를 구현한 객체를 사용한다.
- JSP에서는 session 내장 변수를 사용한다.
- 서블릿에서는 HttpServletRequest의 getSession()메소드를 이용하여 session 객체를 얻는다.
- 장바구니처럼 사용자별로 유지가 되어야 할 정보가 있을 때 사용한다.
- session.setAttribute, session.getAttribute 같이 사용 가능
Application Scope - 하나의 웹 앱에서 공유하고싶은 변수가 있을 때 사용
- 웹 어플리케이션이 시작되고 종료될 때까지 변수를 사용할 수 있다.
- ServletContext 인터페이스를 구현한 객체를 사용한다.
- jsp에서는 application 내장 객체를 이용한다.
- 서블릿의 경우는 getServletContext()메소드를 이용하여 application객체를 이용한다.
- 웹 어플리케이션 하나당 하나의 application객체가 사용된다.
- 모든 클라이언트가 공통으로 사용해야 할 값들이 있을 때 사용한다.
- application.setAttribute, application.getAttribute 같이 사용 가능
Application Scope 사용법
ApplicationScope02.java
package examples;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ApplicationScope02
*/
@WebServlet("/ApplicationScope02")
public class ApplicationScope02 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ApplicationScope02() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
ServletContext application = getServletContext();
try {
int value = (int)application.getAttribute("value");
value++;
application.setAttribute("value",value);
out.println("<h1>value:"+value+"</h1>");
}
catch (NullPointerException e) {
out.print("value의 값이 설정되지 않음");
}
}
}
ApplicationScope01.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Application Scope</title>
</head>
<body>
<%
try{
int value = (int)application.getAttribute("value");
value = value + 2;
application.setAttribute("value", value);
%>
<h1><%=value %></h1>
<%
}catch(NullPointerException ex){
%>
<h1>설정된 값이 없습니다.</h1>
<%
}
%>
</body>
</html>
'BACK_END > Spring 공부' 카테고리의 다른 글
Spring - JSTL (0) | 2021.02.03 |
---|---|
Spring - EL (Expression Language) (0) | 2021.02.02 |
Spring - JSP (0) | 2021.02.01 |
Spring - Servlet 생명 주기 (0) | 2021.02.01 |
Spring - DBMS,MiddleWare,WAS (0) | 2021.01.26 |
Comments