Request: 단순 전송
Session: 브라우저에 저장
Application: 서버에 저장
Set
package practice;
import java.io.IOException;
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;
import javax.servlet.http.HttpSession;
@WebServlet("/SetScopeServletForP")
public class SetScopeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. request scope에 request/홍길동 저장
request.setAttribute("request", "홍길동");
System.out.println("set request : 홍길동");
//2. Session scope에 session/이순신 저장
// 라이프 사이클이 브라우저와 동일(브라우저가 끊어질 때까지)
HttpSession session = request.getSession();
session.setAttribute("session", "이순신");
System.out.println("set session : 이순신");
//3. Application scope에 application/유관순 저장
ServletContext ctx = getServletContext();
ctx.setAttribute("application", "유관순");
System.out.println("application : 유관순");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Get
package practice;
import java.io.IOException;
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;
import javax.servlet.http.HttpSession;
@WebServlet("/GetScopeServletForP")
public class GetScopeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//폼데이터 -> request.getParameter("key값") --> String 타입
//request.setAttribute -> request.getAttribute --> Object 타입(형변환 필요)
//1. request scope에서 데이터 get
//getAttribute는 object 타입
//set에서 저장하고, 불러오는 것까지 된 것이 아니기 때문에 null이 나옴(저장되고 끊어진 다음, 불러오는 상태)
String x = (String)request.getAttribute("request");
System.out.println("GetScopeServlet request "+x);
//2. Session scope에서 데이터 get
//브라우저가 동일(브라우저 하나당 세션 하나)
HttpSession session = request.getSession();
String x2 = (String)session.getAttribute("session");
System.out.println("GetScopeServlet session "+ x2);
//3. application scope에서 데이터 get
//브라우저가 꺼져도 유지(서버가 살아있는 한 유지)
ServletContext ctx = getServletContext();
String x3 = (String)ctx.getAttribute("application");
System.out.println("GetScopeServlet application "+x3);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
'단순 코드 기록 > JavaSevlet' 카테고리의 다른 글
서블릿_Basic (0) | 2024.03.06 |
---|---|
JavaSevlet_Request / Redirect (0) | 2024.02.11 |
JavaSevlet_Filter (0) | 2024.02.11 |
JavaSevlet_Param (0) | 2024.02.11 |
JavaSevlet_MVC (0) | 2024.02.11 |