14장 연습 문제 소스 코드
14장 연습 문제
1. Cookie 클래스의 메소드에 해당하지 않는 것은 무엇인가?
- 3번 setName()
2. JSP 페이지에서 쿠키를 설정하는 메소드는 무엇인가?
- 4번 addCookie()
3. JSP 페이지에서 설정된 쿠키를 삭제하는 메소드는 무엇인가?
- 3번 setMaxAge()
4. JSP 페이지에서 설정된 하나의 쿠키 정보를 얻는 메소드는 무엇인가?
- 2번 getCookies()
5. JSP 페이지에서 설정된 하나의 쿠키 정보를 얻기 위해 쿠키 이름과 값을 가져오는 메 소드는 무엇인가?
- 1번 getName(), getValue()
6. JSP 페이지에서 쿠키 객체를 생성하는 프로그램의 빈칸을 채우시오.
- 빈칸 addCookie()
7. JSP 페이지에서 쿠키 객체에 저장된 모든 쿠키 이름과 값을 가져와 출력하는 프로그 램의 빈칸을 채우시오.
- 빈칸 a) getCookies() b) getName() c) getValue()
8. JSP 페이지에서 쿠키 객체에 저장된 모든 쿠키를 삭제하는 프로그램의 빈칸을 채우시오.
- 빈칸 setMaxAge(0)
소스 코드
9.
cookie.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie Example</title>
</head>
<body>
<h2>Login</h2>
<form action="cookie_process.jsp" method="post">
<label for="userId">아이디:</label>
<input type="text" id="userId" name="userId" required><br><br>
<label for="password">비밀번호:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="전송">
</form>
</body>
</html>
cookie_process.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="javax.servlet.http.Cookie"%>
<%
String userId = request.getParameter("userId");
String password = request.getParameter("password");
if ("admin".equals(userId) && "1234".equals(password)) {
Cookie userCookie = new Cookie("userID", userId);
userCookie.setMaxAge(60*60);
response.addCookie(userCookie);
response.sendRedirect("welcome.jsp");
} else {
out.println("아이디 또는 비밀번호가 틀립니다.");
}
%>
welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="javax.servlet.http.Cookie"%>
<%
Cookie[] cookies = request.getCookies();
String userID = null;
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("userID".equals(cookie.getName())) {
userID = cookie.getValue();
break;
}
}
}
if (userID == null) {
response.sendRedirect("cookie_out.jsp");
}
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie</title>
</head>
<body>
<h2><%= userID %>, 님 반갑습니다.</h2>
<form action="cookie_out.jsp" method="post">
<input type="submit" value="로그아웃">
</form>
</body>
</html>
cookie_out.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="javax.servlet.http.Cookie"%>
<%
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie cookie : cookies) {
if ("userID".equals(cookie.getName())) {
cookie.setValue("");
cookie.setMaxAge(0);
response.addCookie(cookie);
break;
}
}
}
response.sendRedirect("cookie.jsp");
%>
'프로그래밍 > JSP' 카테고리의 다른 글
[쉽게 배우는 JSP 웹 프로그래밍 개정2판] 16장 연습 문제, 소스 코드 (1) | 2024.06.09 |
---|---|
[쉽게 배우는 JSP 웹 프로그래밍 개정2판] 13장 연습 문제, 소스 코드 (0) | 2024.06.07 |
[쉽게 배우는 JSP 웹 프로그래밍 개정2판] 8장 설명, 연습 문제 (0) | 2024.06.06 |
[쉽게 배우는 JSP 웹 프로그래밍 개정2판] 7장 설명, 연습 문제 (0) | 2024.06.05 |
[쉽게 배우는 JSP 웹프로그래밍 개정2판] 6장 설명, 연습문제 (0) | 2024.06.04 |
댓글