반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/09   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

비전공개미 개발노트

[JSP] 쿠키를 이용해 로그인했던 아이디 기억하기 본문

프로그래밍/JSP

[JSP] 쿠키를 이용해 로그인했던 아이디 기억하기

비전공개미 2022. 10. 31. 20:25
반응형
SMALL

loginFrm.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>
<script src="https://code.jquery.com/jquery-3.6.1.js" integrity="sha256-3zlB5s2uwoUzrXK3BT7AX3FyvojsraNFxCc2vC/7pNI=" crossorigin="anonymous"></script>
</head>
<body>
<%
Cookie[] c = request.getCookies();
String userId = "";
if(c != null){
	for(int i=0; i<c.length; i++){
		if(c[i].getName().equals("userId")){
			userId = c[i].getValue();
		}
	}
}

%>
<h3>관리자 로그인</h3>
<form action="loginProc.jsp" method="post">
	아디 : <input type="text" name="userId" value="<%=userId%>"/>
	<input type="checkbox" name="chk" id="chk" />아이디 기억<br>
	<input type="hidden" name="chkValue" id="chkValue" value="n" />
	비번 : <input type="password" name="userPwd" />
	<input type="submit" value="전송" />
</form>
<script>
$("#chk").change(function(){
    if($("#chk").is(":checked")){
        $("#chkValue").val("y");
    }else{
    	$("#chkValue").val("n");
    }
});

</script>
</body>
</html>

 

 

 

loginProc.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 userId = request.getParameter("userId");
String userPwd = request.getParameter("userPwd");

String webId = application.getInitParameter("adminId");
String webPwd = application.getInitParameter("adminPwd");

String chk = request.getParameter("chkValue");
//out.print(chk);
if(chk.equals("y")){
	Cookie c = new Cookie("userId", userId);
	c.setMaxAge(60*1);
	c.setValue(userId);
	response.addCookie(c);
}else{
	Cookie[] cookies = request.getCookies();
	for(int i=0; i<cookies.length; i++){
		if(cookies[i].getName().equals("userId")){
			Cookie cookie = new Cookie("userId", null);
			cookie.setMaxAge(0);
			response.addCookie(cookie);
		}
	}
}

if(userId.equals(webId) && userPwd.equals(webPwd)){%>
	<h3>로그인에 성공했음</h3>
	<a href="loginFrm.jsp">로그인 폼으로</a>
<%}else{%>
	<h3>아이디와 비밀번호가 틀렸습니다</h3>
	<a href="loginFrm.jsp">로그인 폼으로</a>
<%}%>
</body>
</html>

 

 

아이디기억 쿠키에만 중점을 둔것이기 때문에

webId와 webPwd는 xml에 기본값으로 등록해놓은것(참고)

별도 db와 연결이 필요하다

 

 

 

loginFrm.jsp

 

 

 

loginProc.jsp

 

 

 

loginFrm.jsp

 

 

아이디 기억을 체크한 후 로그인을 시도하고 다시 로그인폼으로 돌아갔을때

기존에 입력했던 아이디가 남아있는것을 볼 수 있다.

반응형
LIST
Comments