JSP/Spring 세션

박산해·2025년 5월 19일

오늘은 세션에 대해 알아보겠습니다.

자 일단 파일부터 만들고 시작하겠습니다.

Ex04CreateSession

1) 세션 값 설정!

session.setAttribute("id", "sanhae");
session.setAttribute("age", 20);
  • id 다음 "sanhae"는 본인 이 원하시는대로 수정하면 됩니다

2) 세션 확인 (연결하기)

  • 이제 연결해줘야죠!
<a href="Ex05GetSession.jsp">세션확인</a>

전체 코드는

<%
	session.setAttribute("id", "sanhae");
	session.setAttribute("age", 20);
%>
	
<a href="Ex05GetSession.jsp">세션확인</a>

Ex05GetSession.jsp

위에 Ex05GetSession.jsp로 연결했으니 다시 해당이름 복사해서 파일 만들어주고

1) <% %> 해당 기호를 사용해서

<%
		String id = (String)session.getAttribute("id");
		int age = (int)session.getAttribute("age");
%>

2) 아이디, 나이 세션, 세션 아이디, 기본 세션 유효시간을 넣기

아이디 : <%=id %> <br>
나이 : <%=age %> <br>
세션 아이디 : <%=session.getId() %><br>
기본 세션 유효시간 : <%=session.getMaxInactiveInterval() %>
<br>

3) 세션을 만들었으면 삭제도 해야지 ! 연결합시다!

<a href="Ex06RemoveSession.jsp">세션삭제</a>

전체 코드

<%
	String id = (String)session.getAttribute("id");
	int age = (int)session.getAttribute("age");
%>

아이디 : <%=id %> <br>
나이 : <%=age %> <br>
세션 아이디 : <%=session.getId() %><br>
기본 세션 유효시간 : <%=session.getMaxInactiveInterval() %>
<br>

Ex06RemoveSession.jsp

1) 아이디 값 입력해주고 연결 까지!

<%
	session.removeAttribute("id");
%>

<a href="Ex05GetSession.jsp">세션확인</a>

Ex07InvalidateSession.jsp

1) 세션 값 전체 삭제

<%
	session.invalidate();
%>

<a href="Ex05GetSession.jsp">세션확인</a>

Ex05GetSession.jsp

1) 다시 돌아와서 전체삭제 연결해줘야해

<a href="Ex06RemoveSession.jsp">세션전체삭제</a>
<a href="Ex07InvalidateSession.jsp">세션전체삭제</a>

자 이러면 끝. 코드 정리해볼까

Ex05 전체 코드(전체 삭제 추가한것)

<%
	String id = (String)session.getAttribute("id");
	int age = (int)session.getAttribute("age");
%>

아이디 : <%=id %> <br>
나이 : <%=age %> <br>
세션 아이디 : <%=session.getId() %><br>
기본 세션 유효시간 : <%=session.getMaxInactiveInterval() %>
<br>

<a href="Ex06RemoveSession.jsp">세션전체삭제</a>
<a href="Ex07InvalidateSession.jsp">세션전체삭제</a>

이러면 끝.

0개의 댓글