Web/JSP
세션(session)
surhommejk
2017. 12. 4. 15:43
정리
1) WAS 상에 만들어지는 객체이므로 보안에 강하고 용량에 제한이 없다
2) 브라우저 하나당 유니크한 객체 1개를 자동으로 jsp 컨테이너가 만든다
session을 이용한 회원 로그인 예제 실습
login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>Insert title here</title>
</head>
<body>
<form action="loginCK.jsp" method="post">
아 이 디 : <input type="text" name="id" size="10"><br />
비밀번호 : <input type="password" name="pw" size="10"><br/>
<input type="submit" value="login">
</form>
</body>
</html>
loginCK.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
if ((id.equals("abcde")) && (pw.equals("12345"))) {
session.setAttribute("id", id);
response.sendRedirect("welcom.jsp");
} else {
response.sendRedirect("login.html");
}
%>
</body>
</html>
welcom.jsp
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
Enumeration enu = session.getAttributeNames();
while (enu.hasMoreElements()) {
String sName = enu.nextElement().toString();
String sValue = (String) session.getAttribute(sName);
if (sName.equals("abcde")) {
out.print(sName + "님 환영합니다. <br/>" );
}
}
%>
<a href="logout.jsp">로그아웃</a>
</body>
</html>
logout.jsp
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
Enumeration enu = session.getAttributeNames();
while(enu.hasMoreElements()){
String sName = enu.nextElement().toString();
String sValue = (String)session.getAttribute(sName);
if(sValue.equals("abcde")){
session.removeAttribute(sName);
}
}
%>
<a href="sessionTest.jsp">session test로 이동합니다</a>
</body>
</html>
sessionTest.jsp
<%@page import="java.util.Enumeration"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<%
Enumeration enu = session.getAttributeNames();
while(enu.hasMoreElements()){
String sName = enu.nextElement().toString();
String sValue = (String) session.getAttribute(sName);
out.print("sName : " + sName + "<br/>");
out.print("sValue : " + sValue + "<br/>");
}
%>
출력할 session이 없습니다.
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------
+ 수업내용 추가
session은 브라우저 단위로 생성되어 관리된다
invalidate
public void invalidate()
Invalidates this session then unbinds any objects bound to it.
Throws: java.lang.IllegalStateException - if this method is called on an already invalidated session
>> session을 비우는 것이 아닌 '소멸'시킨다. (자체 소멸)