티스토리 뷰

 

 

 

1번. JSP 페이지에 쿠키를 설정하는 메소드, 설정된 쿠키 정보를 얻어오는 메소드는 무엇인가?

쿠키를 설정하는  메소드는 addCookie()이고 설정된 쿠키 정보를 얻어오는 메소드는 getCookie() 메소드를 사용하여 쿠키 객체를 받아오고 getName(), getValue()를 사용하여 쿠키 이름, 값을 가져 올 수 있다.

 

 

 

2번. 설정된 쿠키를 삭제하는 기법은 무엇인가?

쿠키는 삭제하는 방법은 따로 없고 유효기간이 만료되면 자동으로 삭제된다.

그렇기 때문에 유효기간 설정을 0으로 해주면 쿠키가 삭제되는데 이때 쿠키를 삭제하는 메소드는 setMaxAge()메소드이다. setMaxAge(0);으로 설정하여 쿠키를 삭제할 수 있다.

 

 

 

 

 

 

3번. 쿠키를 이용하여 다음 조건에 맞게 JSP 애플리케이션을 만들고 실행 결과를 확인하시오.

 

cookie.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>
	<form action="cookie_porcess.jsp" method="post">
		<p> 아이디 : <input type="text" name="id">
		<p> 비밀번호 : <input type="text" name="pw">
		<p> <input type="submit" value="전송">
	</form>
</body>
</html>

 

cookie_porcess.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 user_id = request.getParameter("id");
		String user_pw = request.getParameter("pw");
		
		if(user_id.equals("admin")&&user_pw.equals("1234")){
			Cookie cookie_id = new Cookie("uesrID",user_id);
			Cookie cookie_pw = new Cookie("userPW",user_pw);
			response.addCookie(cookie_id);
			response.sendRedirect("welcome.jsp");
		}
		else{
			out.println("쿠키 생성 실패");
		}
		
	%>
</body>
</html>

 

welcome.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>
	<%
		Cookie[] cookies = request.getCookies();
	
		if(cookies[0]==null){
			response.sendRedirect("cookie_out.jsp");
		}
	
	%>
	<h3><%=cookies[0].getValue() %>님 반갑습니다.</h3>
	<a href="cookie.jsp">로그아웃</a>
	
</body>
</html>

 

cookie_out.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>
	<%
		Cookie[] cookies = request.getCookies();
	
		cookies[0].setMaxAge(0);
		response.addCookie(cookies[0]);
		
		response.sendRedirect("cookie.jsp");
	%>
</body>
</html>

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday