JSP

 

setSession.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<%
String id="kim";
String passwd="1234";
int age=20;
double height=170.5;
//session 내장객체, 모든 자료형을 저장할 수 있음
session.setAttribute("id", id);
session.setAttribute("passwd", passwd);
session.setAttribute("age", age);
session.setAttribute("height", height);

%>



<c:set scope="session" value="hello" var="test" />


세션이 생성되었습니다.<br>

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




</body>
</html>

 

세션이 생성되었습니다.
세션 확인

 

 

 

 

 

viewSession.jsp

<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>     
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>


<%

//(자료형) session.getAttribute("세션변수명")
if(session.getAttribute("id")!=null){
	String id1=(String)session.getAttribute("id");
	String passwd=(String)session.getAttribute("passwd");
	int age=(int)session.getAttribute("age");
	double height=(double)session.getAttribute("height");

%>

아이디 : <%=id1 %><br/>
비번 : <%=passwd %><br/>
나이 : <%=age %><br/>
키 : <%=height %><br/>

<%
}
%>
<hr>
<h2> EL 사용</h2>
${ id}
<br>
${sessionScope.passwd }<br>
${sessionScope.age }<br>
${sessionScope.height }<br>
${test }<br/>
<hr>

<h2>Enumeration 데이터 출력</h2>
<%
//세션변수명 집합 리턴
Enumeration<String> attr=session.getAttributeNames();
while(attr.hasMoreElements()){//다음 요소가 있으면
	String attrName=attr.nextElement();//다음 요소를 저장
	Object attrValue =session.getAttribute(attrName);
	out.println("세션변수명 :" + attrName);
	out.println("세션값 : " + attrValue +"<br>");
}

%>


<p>
<a href="setSession.jsp">세션 생성</a><br>

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

</body>
</html>

 

아이디 : kim
비번 : 1234
나이 : 20
키 : 170.5

EL 사용

kim 
1234
20
170.5
hello

Enumeration 데이터 출력

세션변수명 :test 세션값 : hello
세션변수명 :passwd 세션값 : 1234
세션변수명 :id 세션값 : kim
세션변수명 :age 세션값 : 20
세션변수명 :height 세션값 : 170.5

세션 생성
세션 삭제

 

 

 

 

 

 

deleteSession.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<%
//세션 개별 삭제 JSESSION 아이디는 유지되고 일부 값만 삭제
session.removeAttribute("id");
session.removeAttribute("passwd");

//세션 변수가 삭제되고 JSESSIONID 도 변경됨
session.invalidate();


%>

세션이 초기화되었습니다. <br>
<a href="viewSession.jsp">세션 확인</a>

</body>
</html>

 

 

 

세션이 초기화되었습니다. 
세션 확인

 

 

 

 

 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

처음에는 처녀처럼 조용하게 하고 일단 결심이 서면 그물에서 도망치는 토끼처럼 신속히 단행한다. 적의 간첩을 대처하는 데에는 먼저 들어왔을 때에는 부드럽고 친절하게 맞아들여서 이것을 내 편으로 만들 생각을 한다. 후에 간첩이 적의를 나타내면 방어하는 기회를 주지 말고 빨리 처단해야 한다. -손자

댓글 ( 4)

댓글 남기기

작성