자바 공부/[자바] 기본 공부
[JSP 공부 5일차] 세션
햅2024
2024. 11. 26. 19:59
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JSP</title>
</head>
<body>
<h1>JSP Examples</h1>
<hr>
<a href="11_session.jsp">Session</a><br>
</body>
</html>
11_session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Session Login</title>
</head>
<body>
<%
String userid = "";
//이미 로그인 된 경우
if(session.getAttribute("userid") != null){
System.out.println("[getSession] Login OK");
userid = (String)session.getAttribute("userid");
// 환영 인사
%>
<h1><%=userid%>님 반값습니다.</h1>
<h1>회비는 29만원 입니다.</h1>
<%
}
//로그인을 해야 될 경우
else{
%>
<form name="loginForm" method="post" action="11_session_ok.jsp">
<fieldset>
<legend>Login Info.</legend>
id : <input type="text" name="userid"><br>
pw : <input type="password" name="passwd"><br>
<input type="button" value="login" onclick="sendIt()">
</fieldset>
</form>
<%
}
%>
</body>
<script>
var frm = document.loginForm;
function sendIt()
{
if(frm.userid.value == ''){
alert("이름을 입력하세요");
return false;
}
if(frm.passwd.value == ''){
alert("비밀번호를 입력하세요");
return flase;
}
frm.submit();
}
</script>
</html>
11_session_ok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Session Login</title>
</head>
<body>
<%
String userid = request.getParameter("userid");
String passwd = request.getParameter("passwd");
//user id : Apple, passwd : 111 인 것만 인정
if(userid.equals("Apple") && passwd.equals("111"))
{
//session에 저장
session.setAttribute("userid", userid);
response.sendRedirect("11_session.jsp");
}
else{
%>
<script>
alert("너 누구야");
location.href = "11_session.jsp";
</script>
<%
}
%>
</body>
</html>


