로그인 폼에서 id pw 받기
로그인 폼 생성
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- Begin Wrapper -->
<div id="wrapper">
<!-- Begin Header -->
<div id="header">
/module/top.jsp<br/>
상단 메뉴 <br/><br/>
<a href="<%= request.getContextPath() %>/user/user_insert_form.jsp">01회원가입</a>
<a href="<%= request.getContextPath() %>/user/user_list.jsp">02회원리스트</a>
<a href="#">03상품등록</a>
<a href="#">04상품리스트</a><br/>
<form action="<%=request.getContextPath()%>/login/login_pro.jsp" method="post">
아이디 : <input type = "text" name = "id">
비 번 : <input type = "text" name = "pw">
<input type ="submit" value = "로그인">
</form>
</div>
<!-- End Header -->
폼에서 입력된 id pw login_pro.jsp 에서 받기
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
System.out.println("id: "+ id);
System.out.println("pw: "+ pw);
%>
id 일치/불일치 , pw 조건 비교
login_pro.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
System.out.println("id: "+ id);
System.out.println("pw: "+ pw);
String dbid = "id001";
String dbpw = "pw001";
String dblevel = "관리자"; //관리자 or 판매자 or 구매자 모두 테스트
String dbname = "김길동";
if(id.equals(dbid)){
System.out.println("01 아이디 일치 조건");
if(pw.equals(dbpw)){
System.out.println("03 비밀번호 일치 로그인 성공 조건");
}else {
System.out.println("04 비밀번호 불일치 조건");
}
}else{
System.out.println("02 아이디 불일치 조건");
}
%>
로그인 성공 조건에 response.sendRedirect(request.getContextPath() + "/index.jsp"); 추가.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
System.out.println("id: "+ id);
System.out.println("pw: "+ pw);
String dbid = "id001";
String dbpw = "pw001";
String dblevel = "관리자"; //관리자 or 판매자 or 구매자 모두 테스트
String dbname = "김길동";
if(id.equals(dbid)){
System.out.println("01 아이디 일치 조건");
if(pw.equals(dbpw)){
System.out.println("03 비밀번호 일치 로그인 성공 조건");
response.sendRedirect(request.getContextPath() + "/index.jsp"); // LayoutUG35/index.jsp
}else {
System.out.println("04 비밀번호 불일치 조건");
}
}else{
System.out.println("02 아이디 불일치 조건");
}
%>
test
session setting 후 index로 redirect
session.setAttribute(arg0(변수의 역할) , arg1(값의 역할));
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
System.out.println("id: "+ id);
System.out.println("pw: "+ pw);
String dbid = "id001";
String dbpw = "pw001";
String dblevel = "관리자"; //관리자 or 판매자 or 구매자 모두 테스트
String dbname = "김길동";
if(id.equals(dbid)){
System.out.println("01 아이디 일치 조건");
if(pw.equals(dbpw)){
System.out.println("03 비밀번호 일치 로그인 성공 조건");
response.sendRedirect(request.getContextPath() + "/index.jsp"); // LayoutUG35/index.jsp
session.setAttribute("S_LEVEL", dblevel);
session.setAttribute("S_NAME", dbname);
}else {
System.out.println("04 비밀번호 불일치 조건");
}
}else{
System.out.println("02 아이디 불일치 조건");
}
%>
top에서 session 데이터 확인
top.jsp 에서 session을 확인하기 위한 코드를 추가했다.
<%
String S_LEVEL = (String)session.getAttribute("S_LEVEL");
String S_NAME = (String)session.getAttribute("S_NAME");
System.out.println (S_LEVEL + "<= S_LEVEL top.jsp");
System.out.println (S_NAME + "<= S_NAME top.jsp");
%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String S_LEVEL = (String)session.getAttribute("S_LEVEL");
String S_NAME = (String)session.getAttribute("S_NAME");
System.out.println (S_LEVEL + "<= S_LEVEL top.jsp");
System.out.println (S_NAME + "<= S_NAME top.jsp");
%>
<!-- Begin Wrapper -->
<div id="wrapper">
<!-- Begin Header -->
<div id="header">
/module/top.jsp<br/>
상단 메뉴 <br/><br/>
<a href="<%= request.getContextPath() %>/user/user_insert_form.jsp">01회원가입</a>
<a href="<%= request.getContextPath() %>/user/user_list.jsp">02회원리스트</a>
<a href="#">03상품등록</a>
<a href="#">04상품리스트</a><br/>
<form action="<%=request.getContextPath()%>/login/login_pro.jsp" method="post">
아이디 : <input type = "text" name = "id">
비 번 : <input type = "text" name = "pw">
<input type ="submit" value = "로그인">
</form>
</div>
<!-- End Header -->
test
top에서 로그인 전과 후 화면전환
로그인
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String S_LEVEL = (String)session.getAttribute("S_LEVEL");
String S_NAME = (String)session.getAttribute("S_NAME");
System.out.print (S_LEVEL + "<= S_LEVEL top.jsp");
System.out.print (S_NAME + "<= S_NAME top.jsp");
%>
<!-- Begin Wrapper -->
<div id="wrapper">
<!-- Begin Header -->
<div id="header">
/module/top.jsp<br/>
상단 메뉴 <br/><br/>
<a href="<%= request.getContextPath() %>/user/user_insert_form.jsp">01회원가입</a>
<a href="<%= request.getContextPath() %>/user/user_list.jsp">02회원리스트</a>
<a href="#">03상품등록</a>
<a href="#">04상품리스트</a><br/>
<%
if(S_LEVEL == null){
%>
<form action="<%=request.getContextPath()%>/login/login_pro.jsp" method="post">
아이디 : <input type = "text" name = "id">
비 번 : <input type = "text" name = "pw">
<input type ="submit" value = "로그인">
</form>
<%
} else {
%>
<%= S_NAME %>님 <%= S_LEVEL %> 권한 로그인 중
<a href ="<%= request.getContextPath()%>/login/logout.jsp">로그아웃</a>
<%
}
%>
</div>
<!-- End Header -->
logout
logout.jsp에 로그아웃 처리
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
session.invalidate(); //세션종료
%>
<script type="text/javascript">
alert('로그아웃');
location.href='<%= request.getContextPath()%>/index.jsp';
</script>
권한별 메뉴 변경
권한별 메뉴 설정하기
1관리자 | 2판매자 | 3구매자 | 4로그인전 |
01회원가입 02회원리스트 03상품등록 04상품리스트 |
01회원가입 03상품등록 04상품리스트 |
01회원가입 04상품리스트 |
01회원가입
|
top.jsp의 코드 수정
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String S_LEVEL = (String)session.getAttribute("S_LEVEL");
String S_NAME = (String)session.getAttribute("S_NAME");
System.out.println (S_LEVEL + "<= S_LEVEL top.jsp");
System.out.println (S_NAME + "<= S_NAME top.jsp");
%>
<!-- Begin Wrapper -->
<div id="wrapper">
<!-- Begin Header -->
<div id="header">
/module/top.jsp<br/>
상단 메뉴 <br/><br/>
<%
if(S_LEVEL == null){
%>
<a href="<%= request.getContextPath() %>/user/user_insert_form.jsp">01회원가입</a>
<form action="<%=request.getContextPath()%>/login/login_pro.jsp" method="post">
아이디 : <input type = "text" name = "id">
비 번 : <input type = "text" name = "pw">
<input type ="submit" value = "로그인">
</form>
<%
} else {
if(S_LEVEL.equals("관리자")){%>
<a href="<%= request.getContextPath() %>/user/user_insert_form.jsp">01회원가입</a>
<a href="<%= request.getContextPath() %>/user/user_list.jsp">02회원리스트</a>
<a href="#">03상품등록</a>
<a href="#">04상품리스트</a><br/>
<%
} else if(S_LEVEL.equals("판매자")){
%>
<a href="<%= request.getContextPath() %>/user/user_insert_form.jsp">01회원가입</a>
<a href="#">03상품등록</a>
<a href="#">04상품리스트</a><br/>
<% } else if (S_LEVEL.equals("구매자")) { %>
<a href="<%= request.getContextPath() %>/user/user_insert_form.jsp">01회원가입</a>
<a href="#">04상품리스트</a><br/>
<% } %>
<%= S_NAME %> 님 <%= S_LEVEL %> 권한 로그인 중
<a href ="<%= request.getContextPath()%>/login/logout.jsp">로그아웃</a>
<%
}
%>
</div>
<!-- End Header -->
login_pro.jsp 의 dblevel 변수에 담긴 값이 관리자인 경우
상단 메뉴가 4가지, 관리자 권한으로 로그인되었다고 출력되는 것을 확인
login_pro.jsp 의 dblevel 변수에 담긴 값이 판매자인 경우
상단 메뉴가 3가지, 판매자 권한으로 로그인되었다고 출력되는 것을 확인
login_pro.jsp 의 dblevel 변수에 담긴 값이 구매자인 경우
상단 메뉴가 2가지, 구매자 권한으로 로그인되었다고 출력되는 것을 확인
로그인 상태가 아닌 경우
상단 메뉴가 4가지 출력되는 것을 확인
js_href를 활용하여 경고창으로 로그인 처리 알림
log_pro.jsp에 자바스크립트 코드를 삽입한다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
System.out.println("id: "+ id);
System.out.println("pw: "+ pw);
String dbid = "id001";
String dbpw = "pw001";
String dblevel = "관리자"; //관리자 or 판매자 or 구매자 모두 테스트
String dbname = "김길동";
if(id.equals(dbid)){
System.out.println("01 아이디 일치 조건");
if(pw.equals(dbpw)){
System.out.println("03 비밀번호 일치 로그인 성공 조건");
//response.sendRedirect(request.getContextPath() + "/index.jsp"); // LayoutUG35/index.jsp
session.setAttribute("S_LEVEL", dblevel);
session.setAttribute("S_NAME", dbname);
%>
<script type="text/javascript">
alert('로그인성공');
location.href='<%= request.getContextPath()%>/index.jsp';
</script>
<%
}else {
System.out.println("04 비밀번호 불일치 조건");
%>
<script type="text/javascript">
alert('비밀번호 불일치');
location.href='<%= request.getContextPath()%>/index.jsp';
</script>
<%
}
}else{
System.out.println("02 아이디 불일치 조건");
%>
<script type="text/javascript">
alert('아이디 불일치');
location.href='<%= request.getContextPath()%>/index.jsp';
</script>
<%
}
%>
id가 불일치했을 경우
id가 일치하고 pw가 불일치했을 경우
id와 pw가 일치했을 경우
js_href를 활용하여 경고창 하나로 로그인 처리 알림
항상 경고창이 뜨도록 경고창 코드를 작성
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
System.out.println("id: "+ id);
System.out.println("pw: "+ pw);
String dbid = "id001";
String dbpw = "pw001";
String dblevel = "관리자"; //관리자 or 판매자 or 구매자 모두 테스트
String dbname = "김길동";
if(id.equals(dbid)){
System.out.println("01 아이디 일치 조건");
if(pw.equals(dbpw)){
System.out.println("03 비밀번호 일치 로그인 성공 조건");
//response.sendRedirect(request.getContextPath() + "/index.jsp"); // LayoutUG35/index.jsp
session.setAttribute("S_LEVEL", dblevel);
session.setAttribute("S_NAME", dbname);
}else {
System.out.println("04 비밀번호 불일치 조건");
}
}else{
System.out.println("02 아이디 불일치 조건");
}
%>
<script type="text/javascript">
alert('로그인 처리 알림');
location.href='<%= request.getContextPath()%>/index.jsp';
</script>
어떤 조건이어도 '로그인 처리 알림' 경고창이 뜬다.
변수 alert를 선언하여 로그인 처리에 대한 알림 String을 경우에 따라 alert에 변수에 입력하도록 작성한다.
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
System.out.println("id: "+ id);
System.out.println("pw: "+ pw);
String dbid = "id001";
String dbpw = "pw001";
String dblevel = "관리자"; //관리자 or 판매자 or 구매자 모두 테스트
String dbname = "김길동";
String alert = null;
if(id.equals(dbid)){
System.out.println("01 아이디 일치 조건");
if(pw.equals(dbpw)){
System.out.println("03 비밀번호 일치 로그인 성공 조건");
//response.sendRedirect(request.getContextPath() + "/index.jsp"); // LayoutUG35/index.jsp
session.setAttribute("S_LEVEL", dblevel);
session.setAttribute("S_NAME", dbname);
alert = "로그인 성공";
}else {
System.out.println("04 비밀번호 불일치 조건");
alert = "비밀번호 불일치";
}
}else{
System.out.println("02 아이디 불일치 조건");
alert = "아이디 불일치";
}
%>
<script type="text/javascript">
alert('<%= alert%>');
location.href='<%= request.getContextPath()%>/index.jsp';
</script>
id가 불일치했을 경우의 알림과 콘솔창
id는 일치하고 pw가 불일치했을 경우의 알림과 콘솔창
id와 pw가 모두 일치했을 경우의 알림과 콘솔창
'JSP' 카테고리의 다른 글
[JSP+MySQL] list 불러오기 (0) | 2020.03.26 |
---|---|
[JSP+MySQL] JSP + MySQL / JDBC (0) | 2020.03.19 |
[JSP] JSP 문법 구조 / include를 활용해 layout 나누기 (0) | 2020.03.13 |
[JSP] Java와 jsp 연결 (0) | 2020.03.12 |