비전공개미 개발노트
[JSP] 쿠키를 이용해 로그인했던 아이디 기억하기 본문
반응형
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와 연결이 필요하다
아이디 기억을 체크한 후 로그인을 시도하고 다시 로그인폼으로 돌아갔을때
기존에 입력했던 아이디가 남아있는것을 볼 수 있다.
반응형
LIST
'프로그래밍 > JSP' 카테고리의 다른 글
[JSP] 쿠키를 설정하고 확인하기 (0) | 2022.10.12 |
---|---|
[JSP] session값 지정하고 지정된 값 불러오기 (0) | 2022.10.12 |
[JSP] jsp:useBean, jsp:setProperty, jsp:getProperty 사용하기 (0) | 2022.10.12 |
[JSP] jsp:include에 jsp:param값을 넘기기 (0) | 2022.10.12 |
[JSP] get.Parameter값을 받아와서 페이지 바꾸기 (0) | 2022.10.12 |
Comments