웹 프로그래밍 09(JSP)-세션을 다루어 보자
*세션을 다루어 보자
-쿠키는 세션과 달리 클라이언트에 저장되어 보안상 쓰지않기도 한다.
-세션은 서버에서 객체로 생성되어 자주 사용된다.
*세션 객체 생성
-쿠키와 달리 자동생성 된다.
※따로 세션을 선언 할 필요가 없음
-세션 기본 메소드
-세션 예제(로그인)
웹프로그래밍 08에서 본 예제와 상황은 거의 비슷하다.
main.html => id, pw 입력을 받는다.
logincheck.jsp => id, pw 비교 후 맞다면 id만 세션에 저장후 login.jsp로 전환,
아니면 main.html로 다시돌아감
login.jsp => 세션을 while문으로 elemnt를 찾는다. 만약 원하는 id값이 나오면 출력
logout.jsp => 세션을 지우기 전 값이랑 지운 후 값이 있는지 확인
main.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <head> <meta charset="EUC-KR"> <title>Insert title here</title> </head> <body>
<form action="logincheck.jsp" method="POST">
아이디 : <input type="text" name="id" size="10"><br /> 비밀번호 : <input type="password" name="pw" size="10"><br />
<input type="submit" value="로그인"> <input type="reset" value="초기화"> </form>
</body> </html> |
logincheck.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<%@ 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, pw; %>
<%
id = request.getParameter("id"); pw = request.getParameter("pw");
if(id.equals("abcd") && pw.equals("1234")) { session.setAttribute("id",id); response.sendRedirect("login.jsp"); } else response.sendRedirect("main.html");
%>
</body> </html> |
-login.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<%@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 enumeration = session.getAttributeNames(); while(enumeration.hasMoreElements()) { String sName = enumeration.nextElement().toString(); String sValue = (String)session.getAttribute(sName);
if(sValue.equals("abcd")) out.println(sValue + "님 안녕하세요."+"<br /");
} %> <br /><a href="logout.jsp">로그아웃</a> </body> </html> |
logout.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<%@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 enumeration = session.getAttributeNames(); out.println("===지우기전 세션===" + "<br />"); while(enumeration.hasMoreElements()) { out.println(enumeration.nextElement().toString()); out.println("<br />"); } out.println("===세션 삭제 완료===" + "<br />"); while(enumeration.hasMoreElements()) { String sName = enumeration.nextElement().toString(); String sValue = (String)session.getAttribute(sName);
if(sValue.equals("abcd")) session.removeAttribute(sName); } out.println("===현재 남아있는 세션===" + "<br />"); while(enumeration.hasMoreElements()) { out.println(enumeration.nextElement().toString()); } %>
</body> </html> |
-출력화면
main.html
login.jsp
logout.jsp
'프로그래밍 정리 > JSP, Servlet, DB(oracle)' 카테고리의 다른 글
웹 프로그래밍 11(JSP)-JSP에서 빈(Bean)을 사용해보자 (0) | 2020.01.03 |
---|---|
웹 프로그래밍 10(JSP)-예외처리를 해보자 (0) | 2020.01.03 |
웹 프로그래밍 08(JSP)-쿠키를 다루어 보자 (0) | 2020.01.03 |
웹 프로그래밍 07(JSP)-액션태그, jsp:include (0) | 2020.01.03 |
웹 프로그래밍 06(JSP)-요청과 응답 (0) | 2020.01.02 |