gwooden_코린이

세션을 이용한 로그인 구현해보기 본문

JSP&Servlet

세션을 이용한 로그인 구현해보기

gwooden22 2023. 1. 25. 09:33
728x90
<%@ 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>
<%
	if(session.getAttribute("id") !=null) {
		response.sendRedirect("loginMain.jsp");
	}
%>

	<form method="post" action="cehLogin.jsp">
		아이디 : <input type="text" name="id"><br>
		비밀번호 : <input type="password" name="pw"><br>
		<input type="submit" value="로그인">
	</form>
	<br>
	<a href="loginSucs.jsp">메일</a>
</body>
</html>
<%@ 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 = "user";
		String pw = "1234";
		
		String user_id = request.getParameter("id");
		String user_pw = request.getParameter("pw");
		
		if(id.equals(user_id) && pw.equals(user_pw)) {
			//세션 성공
			session.setAttribute("id", user_id);
			
			//로그인성공 페이지
			response.sendRedirect("loginSucs.jsp");
		} else {
			%>
			
			<script type="text/javascript">
				alert("로그인 실패");
				history.go(-1);
			</script>
			
			<%
		}

	%>
	
</body>
</html>
<%@ 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>
	<h1>메일 보관함</h1>

<%
	if(session.getAttribute("id") == null) {
		response.sendRedirect("loginForm.jsp");
	}
%>
	<%=session.getAttribute("id") %>님 환영합니다.
	
	<form method="post" action="loginOut.jsp">
		<input type="submit" value="로그아웃">
	</form>
</body>
</html>
<%@ 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>
	<%
		//세션 전부 삭제
		session.invalidate();
	
		//특정 세션 삭제
		/* session.removeAttribute("id"); */
		
		response.sendRedirect("loginForm.jsp");
	%>
</body>
</html>

- forward

<%@ 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>
	<h1>껍데기</h1>
	<h2>기존 방식</h2>
	<form method="get" action="forward.jsp">
		아이디 : <input type="text" name="id"><br>
		비밀번호 : <input type="password" name="pw"><br>
		<input type="radio" name="select" value="user"> 사용자
		<input type="radio" name="select" value="admin"> 관리자 <br>
		<input type="submit" value="전송">
	</form>
	
	<h2>액션 태그 방식</h2>
	<form method="get" action="actForward.jsp">
		아이디 : <input type="text" name="id"><br>
		비밀번호 : <input type="password" name="pw"><br>
		<input type="radio" name="select" value="user"> 사용자
		<input type="radio" name="select" value="admin"> 관리자 <br>
		<input type="submit" value="전송">
	</form>
</body>
</html>
<%@page import="java.net.URLEncoder"%>
<%@ 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 select = request.getParameter("select");

	if(select.equals("user")) {
%>	
	<jsp:forward page="forwardTest.jsp">
		<jsp:param name="s" value='<%=URLEncoder.encode("사용자", "utf-8") %>'/>
	</jsp:forward>
<%
	} else {
%>
	<jsp:forward page="forwardTest.jsp">
		<jsp:param name="s" value='<%=URLEncoder.encode("관리자", "utf-8") %>'/>
	</jsp:forward>
<%
	}
%>


</body>
</html>
<%@ 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 select = request.getParameter("select");
		
		if(select.equals("user")) {
			request.setAttribute("s", "사용자");
		} else {
			request.setAttribute("s", "관리자");
		}
	
		RequestDispatcher dispatcher = request.getRequestDispatcher("forwardTest.jsp");
		dispatcher.forward(request, response);
	%>

	
</body>
</html>
<%@page import="java.net.URLDecoder"%>
<%@ 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>
	<h1>기존방식(포워드)</h1>
	<h2>아이디 : <%=request.getParameter("id") %></h2>
	<h2>권한 : <%=request.getAttribute("s") %></h2>
	
	<h1>액션태그(포워드)</h1>
	<h2>아이디 : <%=request.getParameter("id") %></h2>
	<h2>권한 : <%=URLDecoder.decode(request.getParameter("s"), "utf-8") %></h2>
</body>
</html>

- include

<%@ 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>
	<h1>여기는 header</h1>
</body>
</html>
<%@ 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>
	<h1>여기는 footer</h1>
</body>
</html>
<%@ 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>
	<%@ include file="header.jsp" %>
	
	<h1>메인01페이지</h1>
	<a href="main02.jsp">두 번째 페이지</a>
	
	<%@ include file="footer.jsp" %>
</body>
</html>
<%@ 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>
	<%@ include file="header.jsp" %>
	
	<h1>메인02페이지</h1>
	<a href="main01.jsp">첫 번째 페이지</a>
	
	<%@ include file="footer.jsp" %>
</body>
</html>
<%@ 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>
	데이터 머시기
	<jsp:include page="header.jsp"/>
	데이터 머시기
	<h1>메인01페이지</h1>
	<a href="actionMain02.jsp">두 번째 페이지</a>
	<jsp:include page="footer.jsp"/>
</body>
</html>
<%@ 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>
	<jsp:include page="header.jsp"/>
	<h1>메인02페이지</h1>
	<a href="actionMain01.jsp">두 번째 페이지</a>
	<jsp:include page="footer.jsp"/>
</body>
</html>
728x90
Comments