로그인 처리하기

Lee kyu min·2024년 3월 4일

JSP & Servlet

목록 보기
6/15

1. 로그인 화면(loginForm.jsp)

<%@ 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>
	<form action="testLogin.jsp" method="post"> //form내 입력값 testLoign.jsp로 post방식 전송
		<label for="userid"> 아이디 : </label>
		<input type="text" name="id" id="userid"><br>
		
		<label for="userpwd">&nbsp;</label>
		<input type="password" name="pwd" id="userpwd"><br>
		
		<input type="submit" value="로그인">
	</form>
</body>
</html>

2. 아이디 비밀번호 입력값 확인(testLogin.jsp)

<%@ 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="admin";
	String pwd="1234";
	String name="관리자";
	
	if(id.equals(request.getParameter("id")) && 	wd.equals(request.getParameter("pwd"))) {	//입력한 id가 admin, pwd가 1234인지 확인 loginForm.jsp에서 보낸 값 가져옴
		session.setAttribute("loginUser", name);	//name값 loginUser란 이름으로 session에 저장
		response.sendRedirect("main.jsp");		// main.jsp로 이동
	} else {
		response.sendRedirect("1oginForm.jsp");		//입력한 id, 비밀번호가 일치하지 않으면 loginForm.jsp로 이동
	}
%>
</body>
</html>

3. 로그인시 화면(main.jsp)

<%@ 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("loginUser") == null) {		//session에서 보낸 loginUser가 null이면 loginForm.jsp로 이동
		response.sendRedirect("loginForm.jsp");
	} else {
	%>
	<%= session.getAttribute("loginUser") %>님 안녕하세요!<br>
	저희 홈페이지에 방문해 주셔서 감사합니다.<br>
	즐거운 시간 되세요.... <br>
	<form action="login.jsp" method="post">
		<input type="submit" value="로그아웃">
	</form>
	<%
	}
	%>
</body>
</html>

4. 로그아웃 화면(logout.jsp)

<%@ 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값 제거
	%>
	<script>
	alert("로그 아웃 되셨습니다.");		//로그아웃 되셨습니다 라는 메세지박스 내보냄
	location.href="loginForm.jsp"		//loginForm.jsp로 되돌아감
	</script>
</body>
</html>

0개의 댓글