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>



'자바 공부 > [자바] 기본 공부' 카테고리의 다른 글
| [AJAX] Json 데이터 불러오기 (0) | 2024.11.26 |
|---|---|
| [JSP 공부 4일차] AJAX (0) | 2024.11.26 |
| [JSP 공부 4일차] 액션 태그, 쿠키 (0) | 2024.11.25 |
| [JSP 3일차] 디렉티브 태그와 인클루드 태그 (0) | 2024.11.21 |
| [JSP 2일차] 스크립트 태그 (1) | 2024.11.20 |