웹페이지 제작을 위한 스프링공부
application scope
풀스택 개발자
2020. 5. 8. 16:39
요청이 발생해서 응답결과가 전달될때 까지의 범위:requestscope
브라우저에 의해 최초의 요청이 발생되고 브라우저가 종료될때까지의 범위:sessionscope
서버가 가동될 때 부터 서버가 종료되는 시점까지의 범위:applicationscope
controller
package kr.co.softcampus.controller;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import kr.co.softcampus.beans.DataBean1;
@Controller
public class TestController {
@GetMapping("/test1")
public String test1(HttpServletRequest request) {//request 객체를 주입받은후 servlet context를 추출후 application 영역에 저장
ServletContext applicaion = request.getServletContext();//ServletContext영역에 저장을 하면 모든 브라우저에 관계없이 전부다 똑같은 메모리 공간을 사용
applicaion.setAttribute("data1", "문자열1");
return "test1";
}
@GetMapping("/result1")
public String result1(HttpServletRequest request) {
ServletContext applicaion = request.getServletContext();
String data1=(String)applicaion.getAttribute("data1");
return "result1";
}
}
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>
<a href='test1'>test1</a><br/>
<a href='result1'>result1</a><br/>
</body>
</html>
<%@ 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>
<h1>저장완료</h1>
</body>
</html>
<%@ 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>
<h1>result</h1>
<h3>data1 : ${applicationScope.data1 }</h3>
</body>
</html>
출력
크롬과 인터넷익스플로러 두개의 브라우저에 실행했지만 둘다 데이터 값이 잘 출력된다.
서버종료시 까짖 유효하기때문에 브라우저에 관계없이 데이터가 유지된다
ServletContext
HttpServletRequest 객체로 부터 추출이 가능합니다.
Controller에서 주입 받을 수 있습니다.
하지만 매개변수로 주입받을 수 없음 ->autowired를 활용하여 타입을 통한 주입 사용
package kr.co.softcampus.controller;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import kr.co.softcampus.beans.DataBean1;
@Controller
public class TestController {
@Autowired
ServletContext application;
@GetMapping("/test1")
//public String test1(HttpServletRequest request) {//request 객체를 주입받은후 servlet context를 추출후 application 영역에 저장
public String test1() {
//ServletContext application = request.getServletContext();//ServletContext영역에 저장을 하면 모든 브라우저에 관계없이 전부다 똑같은 메모리 공간을 사용
application.setAttribute("data1", "문자열1");
return "test1";
}
@GetMapping("/result1")
//public String result1(HttpServletRequest request) {
public String result1() {
//ServletContext applicaion = request.getServletContext();
String data1=(String)application.getAttribute("data1");
return "result1";
}
}
bean데이터 application영역에 주입시키기
package kr.co.softcampus.beans;
public class DataBean1 {
public String data1;
public String data2;
public String getData1() {
return data1;
}
public void setData1(String data1) {
this.data1 = data1;
}
public String getData2() {
return data2;
}
public void setData2(String data2) {
this.data2 = data2;
}
}
package kr.co.softcampus.controller;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import kr.co.softcampus.beans.DataBean1;
@Controller
public class TestController {
@Autowired
ServletContext application;
@GetMapping("/test1")
//public String test1(HttpServletRequest request) {//request 객체를 주입받은후 servlet context를 추출후 application 영역에 저장
public String test1(DataBean1 bean1) {
//ServletContext application = request.getServletContext();//ServletContext영역에 저장을 하면 모든 브라우저에 관계없이 전부다 똑같은 메모리 공간을 사용
application.setAttribute("data1", "문자열1");
bean1.setData1("data1");
bean1.setData2("data2");
application.setAttribute("bean1", bean1);
return "test1";
}
@GetMapping("/result1")
//public String result1(HttpServletRequest request) {
public String result1() {
//ServletContext applicaion = request.getServletContext();
String data1=(String)application.getAttribute("data1");
DataBean1 bean1=(DataBean1)application.getAttribute("bean1");
return "result1";
}
}
<%@ 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>
<h1>result</h1>
<h3>data1 : ${applicationScope.data1 }</h3>
<hr/>
<h3>bean1.data1:${applicationScope.bean1.data1 }</h3>
<h3>bean1.data2:${applicationScope.bean1.data2 }</h3>
</body>
</html>
결과창