JAVA

ServletContext, HttpSession, HttpServletRequest 차이점

cattaku 2020. 2. 10. 17:27

1.Web = HTTP = Stateless

-연결 상태 유지 X

-요청 단위로 Connection

-따라서 이전 페이지에서 수집 했던 데이터를 다음 페이지에서 사용할 수 없다

-다음 페이지에서도 이전 페이지에서의 데이터를 유지해서 쓰기 위해서는 어딘가 저장해두고 유지시켜 놓아야한다

-저장소 : ServletContext, HttpSession, HttpServletRequest

-저장 : setAttribute(name, value)

-추출 : getAttribute(name)

-삭제 : removeAttribute(name)

-각 저장소의 차이점 : 메모리에서 살아있는 기간이 다름

 

ServletContext

HttpSession 

HttpServletRequest

생성 : 서버 시작 시

제거 : 서버 중지 시

web application 이 서비스 중인 동안에는 계속 존재

생성 : Client 최초 접속 시

제거 : Client 접속 종료 시

Client가 접속 중인 동안에만 존재

생성 : Client가 요청 시

삭제 : Server가 응답 시

Request 중인 동안에만 존재

 

로그인/로그아웃

장바구니 등

*요청 재지정

 

 

*요청 재지정

1)RequestDispatcher forward(), include()

-반드시 현재 web app 페이지만 이동 가능

-RequestDispatcher 객체 생성

-ServletContext, HttpServletRequest getRequestDispatcher(“URL”);

 

 

2)HttpServletResponse sendRedirect(URL)

-외부 페이지로 이동 가능

ex) response.sendRedirect(“http://www.naver.com”);

 

https://salguru.tistory.com/23