[JSP] 세션(Session) 로그인/로그아웃 처리

Yun zzin·2022년 1월 12일
1

JSP

목록 보기
5/6

세션(Session) 이란?

  • 사용자로부터 넘어온 정보를 서버측에 저장하는 개념
  • 웹 브라우저당 한 개의 세션이 웹 컨테이너에 저장된다.
  • 웹 서버의 서비스를 받는 사용자를 구분할 수 있는 단위.
  • 세션을 사용하면 여러 사이트를 돌아다녀도 사용자가 웹 서버의 세션에 의해 가상적으로 연결되어 있으므로 정보를 잃지 않는다.
  • 별도의 설정 없이는 세션이 자동으로 생성된다.

세션을 이용한 로그인 처리

  1. Top.jsp : 아이디와 로그인 유무를 확인할 수 있음.
  2. Center.jsp : 가운데 화면
  3. Main.jsp : 메인화면
  4. LoginForm : 로그인폼
  5. SessionLogin.jsp : 로그인 버튼 클릭 시 로그인처리를 하는 페이지

구현목표


Top.jsp

  • 사용자의 아이디와 로그인/로그아웃을 확인할 수 있다.
  1. 로그인 버튼/ 로그아웃 버튼을 생성한다.
  2. id값이 존재하면 로그아웃 버튼을 보여주게한다.
  3. center값이 null값인 경우는 아직 로그인하지 않은 경우이므로 center 값이 null값이면 로그인 버튼을 보여준다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	String id = (String)session.getAttribute("id");
	String center = request.getParameter("center");
%>
<%
	if(id!=null){
		%>
		<%=id %> 님
		<button onclick="location.href='Main.jsp?logout=1'">로그아웃</button>
		<%
	}else if(center==null){
		//center에 값이 존재하는 경우에만 로그인버튼을 띄움.
		//처리를 안해주고 로그인 버튼을 누를시 LoginForm.jsp로 넘어가면 Top.jsp에 있는 로그인 버튼이 그대로 나옴.
		%>
		<button onclick="location.href='Main.jsp?center=LoginForm.jsp'">로그인</button>
		<% 
	}	
	else{
		%>
	<%} 
	%>
</body>
</html>
  • 결과


Center.jsp

  • 로그인 유무에 따라 바뀌는 페이지.
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
Center
</body>
</html>
  • 결과

Main.jsp

  • 메인화면.
    1.Top.jsp, Center.jsp를 include 한다.
    2.로그아웃 처리를 한다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<table border="1">
<%
	response.setCharacterEncoding("UTF-8");
	String center = request.getParameter("center");
	String logout = request.getParameter("logout");
	if(logout!=null){
		//id값에 null 값을 넣음
		session.setAttribute("id", null);
		//세션시간 0. 로그아웃 처리
		session.setMaxInactiveInterval(0);
	}
	if(center==null){
		center = "Center.jsp";
	}
%>
<!-- top -->
	<tr height="50">
		<td width="300" align="right">
		<jsp:include page="Top.jsp"/>
	</td>
</tr>
<!-- center -->
<tr height="300">
		<td width="300" align="center">
		<jsp:include page="<%=center %>"/>
	</td>
</tr>
</table>
</body>
</html>
  • 결과

LoginForm.jsp

  • 로그인 폼
  1. 로그인 폼을 만든다.
  2. 로그인 버튼 클릭시 SessionLogin.jsp 로 넘어가게 처리한다.
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form method="post" action="SessionLogin.jsp">
<table>
<tr height="50">
	<td height="50">로그인</td>
	<td height="50"><input type="text" name="id"></td>
</tr>
<tr height="50">
	<td height="50">패스워드</td>
	<td height="50"><input type="password" name="pass"></td>
</tr>
<tr height="50">
	<td height="50"><input type="submit" value="로그인"></td>
</tr>
</table>
</form>
</body>
</html>
  • 결과

SessionLogin.jsp

  • 세션로그인 처리
  1. id, pass를 String 타입 변수에 할당
  2. id, pass를 session에 할당
  3. 원하는 session 유지시간 설정
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	request.setCharacterEncoding("UTF-8");
	String id = request.getParameter("id");
	String pass = request.getParameter("pass");

	session.setAttribute("id", id);
	session.setAttribute("pass", pass);
	//세션 유지시간 설정
	session.setMaxInactiveInterval(60); //1분간 아이디 유지
	response.sendRedirect("Main.jsp");
%>
</body>
</html>

전체 코드 결과

  • 로그인 버튼을 클릭하면
  • 로그인폼이 나옴
  • 로그인/패스워드를 삽입 후 로그인 버튼을 클릭하면
  • 로그인이 잘 된다.
  • 로그아웃을 클릭하면 다시 로그인 버튼이 활성화된다.
  • 사용자가 설정한 시간(여기선 1분)이 되면 로그아웃 처리가 된다.
  • 참고

https://sesok808.tistory.com/327

profile
공부 기록장

0개의 댓글