JSP
JSP_Session을 이용해서 로그인, 로그아웃만들기
김판다
2021. 4. 22. 19:16
<%@ 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");
if (id == null) {
%>
<form action="exam3_login_check.jsp" method="post">
<input type="text" name="id" placeholder="아이디"><br> <input
type="password" name="pw" placeholder="비밀번호"><br> <input
type="submit" value="로그인"><br>
</form>
<%
}else{ %>
<%=id%>님 로그인 되셨습니다.
<a href="exam3_loginout.jsp">로그아웃</a>
<%
}
%>
</body>
</html>
exam3_login.jsp페이지
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("utf-8");
String id = request.getParameter("id");
String pw = request.getParameter("pw");
if (id.equals("test") && pw.equals("1234")) {
session.setAttribute("id", "test");
session.setAttribute("check", "remember");
%>
<script>
alert('로그인 성공');
location.href = 'exam3_login.jsp';
</script>
<%
} else {
%>
<script>
alert('로그인 실패');
location.href = "exam2_login.jsp";
</script>
<%
}
%>
exam3_login_check.jsp페이지
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
session.invalidate();
%>
<script>
alert('로그아웃');
location.href="exam3_login.jsp";
</script>
exam3_loginout.jsp페이지
[결과 페이지]