자 오늘은 쿠키 세션 3번쨰 시간을 가져보자.!
기존에는 웹 브라우저에서 웹서버에 쿠키를 요청하면 쿠키값을 응답하여 쿠키를 생성하는 부분까지 하였다.
이번에는 웹브라우저에서 쿠키를 쿠키저장소에 저장하면 이후 같은 사이트에 접속시 저장된
쿠키가 요청정보에 실려가는 것까지 해볼것이다. 즉 쿠키에 저장된 아이디값을 추출해서 저장하는 것까지
해볼것이다.
자 이클립스로 가자.!
MemberLoginForm.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 id = "";
Cookie [] cookies = request.getCookies();
for(int i = 0; i<cookies.length;i++){
if(cookies[i].getName().equals("id")){
id = cookies[i].getValue();
break;//기능을 다 수행했으면 마침으로 break;를 처리한다.
}
}
%>
<center>
<h2>회원 로그인</h2>
<form action="MemberLoginProc.jsp" method="post">
<table width="300" border="1">
<tr height="100">
<td>아이디:</td>
<td><input name="id" type="text" value ="<%=id%>"></td>
</tr>
<tr height="100">
<td>비밀번호:</td>
<td><input name="password" type="password"></td>
</tr>
<tr height="50">
<td colspan="2" align="center"><input type="checkbox"
name="save" value="1"> 아이디 저장</td>
</tr>
<tr height="50">
<td align="center" colspan="2"><input type="submit"
name="login" value="로그인"> <a href="MemberJoin.jsp"> <input
type="button" name="join" value="회원가입 ">
</a> <a href="../Main/index.jsp"> <input type="button" name="home"
value="홈">
</a></td>
</tr>
</table>
</form>
</center>
</body>
</html>
MemberLoginProc.jsp
<%@page import="java.io.Console"%>
<%@page import="java.io.PrintWriter"%>
<%@page import="db.MemberBean"%>
<%@page import="db.MemberDAO"%>
<%@ 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>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String password = request.getParameter("password");
String save = request.getParameter("save");
if(save!=null){
Cookie cookie = new Cookie("id",id);
cookie.setMaxAge(60*10);
response.addCookie(cookie);
System.out.print("쿠키로그인완료 ");
}
MemberDAO mdao = new MemberDAO();
MemberBean mbean = new MemberBean();
mbean = mdao.oneSelectMemberLogin(id,password);
HttpSession session1 = request.getSession();
if(mbean.getId()==null&&mbean.getId()==null){
PrintWriter script = response.getWriter();
script.println("<script>");
script.println("alert('잘못된 아이디 혹은 비밀번호입니다. ')");
script.println("history.go(-1)");
script.println("</script>");
}else if(mbean.getId().equals(id)&&mbean.getPass1().equals(password)){
response.sendRedirect("../Main/index.jsp");
session1.setAttribute("id", mbean.getId());
}
%>
</body>
</html>
자 위 주석과 코드들을 잘보자..!
그리고 마지막을 맥북에서 쿠키저장소에 어떻게 접근하는지 적은 블로그를 링크로 걸어놓겠다..
크롬 쿠키 확인 방법 알아보자
크롬 브라우저에서 쿠키를 확인하는 방법에 대해 알아보았습니다. 1. 먼저 크롬 브라우저에 개발자 도구를 확인합니다. 크롬 개발자도구 단축키 : - 윈도우 F12 - 맥 alt + cmd + i 2. Application(어플리
seyul.tistory.com
'웹 프로그래밍(풀스택-->java) > 웹프로그래밍(백엔드-->java)' 카테고리의 다른 글
31. Jsp Cookie & Sessions 5&6 (0) | 2021.12.03 |
---|---|
30. Jsp Cookies & Session 4 (0) | 2021.12.03 |
28. Jsp Cookies & Session 2 (0) | 2021.12.02 |
27. Jsp Cookies & Session 1 (0) | 2021.12.02 |
26. Jsp와 데이터베이스 Connection Pool (0) | 2021.12.01 |