본문 바로가기

JSP(Java Server Page)/session

session을 이해하는 예제

ex1.jsp


<%@ page contentType="text/html; charset=UTF-8"%>

<%@ page trimDirectiveWhitespaces="true" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%

   String cp = request.getContextPath();

%>

<!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=UTF-8">

<title>Insert title here</title>

</head>

<body>

<c:if test="${empty sessionScope.id }"> <!-- sessionScopre.id가 없으면 -->

<a href="login.jsp">로그인</a><br/>

</c:if>

<c:if test="${not empty sessionScope.id }"> <!-- sessionScopre.id가 있으면 -->

${sessionScope.name } 님 방가방가<br/>

<a href="logout.jsp">로그아웃</a><br/>

</c:if>


<a href="sch.jsp">일정관리</a><br/>


</body>

</html>


=====================================================================================================================================

sch.jsp


<%@ page contentType="text/html; charset=UTF-8"%>

<%@ page trimDirectiveWhitespaces="true" %>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%

   String cp = request.getContextPath();

String id=(String)session.getAttribute("id");

if(id==null){

%>

<jsp:forward page="login.jsp"/>

<%

return;

}

%>

<!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=UTF-8">

<title>Insert title here</title>

</head>

<body>

일정입니다....


</body>

</html>


=====================================================================================================================================
login.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
   String cp = request.getContextPath();
%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>

<form action="login_ok.jsp" method="post">
아이디 : <input type="text" name="id"> <br/>
패스워드 : <input type="password" name="pwd"> <br/>
<input type="submit" value="로그인">
<input type="button" value="회원가입">
<input type="button" value="메인" onclick="javascirpt:location.href='ex1.jsp';">
</form>

</body>
</html>

=====================================================================================================================================
login_ok.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    String cp = request.getContextPath();
String id=request.getParameter("id");
String pwd=request.getParameter("pwd");
/*
// 세션
1. jsp : session 내장객체 사용(별도로 객체 생성하지 않아도 됨)
2. 서블릿 : HttpSession session=req.getSession();
*/
// System.out.println()은 서버의 콘솔창에 정보를 출력함
// 실무에서 System.out.println()으로 서버 컴에 정보를 출력하면 아주 많이 무식함...
System.out.println(session.getId());
System.out.println(session.getMaxInactiveInterval());
if(id.equals("test") && pwd.equals("1111")){
//세션 유지시간 설정(초단위)
session.setMaxInactiveInterval(1*10);
//세션에 아이디와 이름을 저장
session.setAttribute("id", id);
session.setAttribute("name", "자바다");
response.sendRedirect("ex1.jsp");
return;
}
out.print("<script> alert('아이디 또는 패스워드 틀림');");
out.print("history.back();</script>");
%>

=====================================================================================================================================
logout.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
   String cp = request.getContextPath();

//로그아웃은 세션에 저장된 것을 지우면 된다.
session.removeAttribute("id");
session.removeAttribute("name");
// 세션에 저장된 모든 것을 지우고 세션을 초기화
session.invalidate();
response.sendRedirect("ex1.jsp");
%>