반응형
[1] 테스트 환경
hardware | |
cpu : intel i7-7700, gpu : intel hd 630, ram : ddr4 16GB, mainboard : samsung DB400S7A-Z51, secondary storage : 256GB SSD, 1TB HDD |
OS |
windows pro 64bits |
software |
jdk version 1.8.0_301 eclipse version 2021-09 (4.21.0) apache-tomcat version 9.0.54 |
[2] 강의 정리
()
header.jspf
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%--
.jspf (Java Server Page Fragment) 파일은
jsp의 일부분(조각)으로 다른 jsp에 포함되도록 만들어졌다.
반복 사용되는 것을 jspf로 뽑아서 jsp파일에서 include 한다.
--%>
<a href="#">공지사항</a>
<a href="#">게시판1</a>
<a href="#">게시판2</a>
footer.jspf
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<h2>서울 강남구 강남대로 아이티뱅크 11층</h2>
<h2>고객센터 : 02-3673-3200</h2>
main.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>
<%--
header.jspf / footer.jspf 파일을 만든 후
페이지 지시자로 include 하기
--%>
<%@include file="header.jspf" %>
<br><br><br><br><br><br><br><br>
<h1>JSP 수업은 재미있습니다!!</h1>
<br><br><br><br><br><br><br><br>
<%@include file="footer.jspf" %>
</body>
</html>
()
cookieSet.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 cookie = new Cookie("cookieName", "cookieValue");
// 쿠키생성 : new Cookie("쿠키이름", "쿠키값");
cookie.setMaxAge(60 * 60); // 1시간동안 쿠키유지
// 쿠키는 보통 유효시간을 정해놓지 않으면 웹브라우저를 닫음과 동시에
// 쿠키가 삭제된다. 하지만 유효시간을 정해놓으면 설정해놓은 시간동안
// 쿠키가 존재하게 되고, 웹브라우저를 닫아도 쿠키는 삭제되지 않고 남아있는다.
response.addCookie(cookie);
// response 객체의 addCookie 메소드로
// 쿠키객체를 웹브라우저에 심는다.
%>
<h2>쿠키가 탑재되었습니다.</h2>
<a href="cookieGet.jsp">쿠키 얻기</a>
</body>
</html>
cookieGet.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();
// 모든 쿠키를 배열로 얻는다.
for(int i = 0; i < cookies.length; i++){
// getName() : 쿠키 이름을 얻는 메소드
// getValue() : 쿠키 값을 얻는 메소드
String str = cookies[i].getName();
if(str.equals("cookieName")){
out.print("쿠키이름 : " + cookies[i].getName() + "<br>");
out.print("쿠키 값 : " + cookies[i].getValue() + "<br>");
}
}
%>
<a href="cookieDelete.jsp">쿠키 삭제</a>
</body>
</html>
cookieDelete.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();
for(int i = 0; i < cookies.length; i++){
String str = cookies[i].getName();
if(str.equals("cookieName")){
cookies[i].setMaxAge(0);
// 유효시간을 0으로 설정
response.addCookie(cookies[i]);
// addCookie메소드로 변경된 쿠키객체 웹브라우저에 심기
}
}
%>
<h2>쿠키가 삭제되었습니다.</h2>
<a href="cookieTest.jsp">쿠키 확인</a>
</body>
</html>
cookieTest.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 != null){
for(int i = 0; i < cookies.length; i++){
out.print("쿠키이름 : " + cookies[i].getName() + "<br>");
out.print("쿠키 값 : " + cookies[i].getValue() + "<br>");
}
}
%>
</body>
</html>
()
cookieLogin.html
<%@ 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 != null){
for(int i = 0; i < cookies.length; i++){
out.print("쿠키이름 : " + cookies[i].getName() + "<br>");
out.print("쿠키 값 : " + cookies[i].getValue() + "<br>");
}
}
%>
</body>
</html>
cookieLoginCheck.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>
<%--
1. 넘어온 아이디와 비밀번호 받기
2. 아이디 > hong 이고 비밀번호가 1234 라면
받은 아이디값으로 쿠키값 설정하기 new Cookie("id", id);
3. 리다이렉트로 cookieMain.jsp로 이동하기
4. 일치하지 않는다면 리다이렉트로 cookieLogin.html로 이동하기
--%>
<%
request.setCharacterEncoding("UTF-8");
String id = request.getParameter("id");
String pw = request.getParameter("pw");
if(id.equals("hong") && pw.equals("1234")){
Cookie cookie = new Cookie("id", id);
cookie.setMaxAge(60*60);
response.addCookie(cookie);
response.sendRedirect("cookieMain.jsp");
}else{
response.sendRedirect("cookieLogin.html");
}
%>
</body>
</html>
cookieMain.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>
<%--
1. 모든 쿠키를 배열로 얻기
2. 반복문을 통해 쿠키값이 hong과 일치한다면
웹브라우저에 홍길동님 안녕하세요 출력하기
3. a태그 사용해서 로그아웃 만들기
--%>
<%
Cookie[] cookies = request.getCookies();
for(int i = 0; i < cookies.length; i++){
String id = cookies[i].getValue();
if(id.equals("hong")){
out.print("<h1>홍길동님 안녕하세요</h1>");
}
}
%>
<a href="cookieLogout.jsp">로그아웃</a>
</body>
</html>
cookieLogout.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>
<%--
쿠키를 배열로 얻은다음 쿠키값이 hong인 쿠키 삭제하기
삭제 후 리다이렉트로 cookieTest.jsp 로 이동하기
--%>
<%
Cookie[] cookies = request.getCookies();
for(int i = 0; i < cookies.length; i++){
String str = cookies[i].getValue();
if(str.equals("hong")){
cookies[i].setMaxAge(0);
response.addCookie(cookies[i]);
response.sendRedirect("cookieTest.jsp");
}
}
%>
</body>
</html>
cookieTest.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 != null){
for(int i = 0; i < cookies.length; i++){
out.print("쿠키 이름 : " + cookies[i].getName() + "<br>");
out.print("쿠키 값 : " + cookies[i].getValue() + "<br>");
}
}
%>
</body>
</html>
()
sessionGet.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>
<%
Object obj1 = session.getAttribute("sessionName");
// getAttribute 의 반환타입은 Object 이다.
String sessionNameValue = (String)obj1;
// Object 타입이므로 캐스팅 해줘야 한다.
out.print("sessionName의 값 : " + sessionNameValue + "<br>");
Object obj2 = session.getAttribute("sessionNumber");
int sessionNumberValue = (Integer)obj2;
out.print("sessionNumber의 값 : " + sessionNumberValue + "<br>");
out.print("------------------------------------------<br>");
%>
</body>
</html>
() sessionSet.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>
<%
session.setAttribute("sessionName", "sessionValue");
session.setAttribute("sessionNumber", 12345);
// 세션객체는 생성하지 않아도 쓸 수 있는 내장객체이다.
// setAttribute("세션이름", 세션 값);
// 이름은 String 타입이고 value는 Object 이다.
%>
<p>세션이 생성되었습니다.</p>
<a href="sessionGet.jsp">세션 얻기</a>
</body>
</html>
반응형
'강의 > JSP 강의 정리' 카테고리의 다른 글
JSP 강의 정리 11일차 (0) | 2022.07.20 |
---|---|
JSP 강의 정리 10일차 (0) | 2022.07.19 |
JSP 강의 정리 8일차 (0) | 2022.07.15 |
JSP 강의 정리 7일차 (0) | 2022.07.14 |
JSP 강의 정리 6일차 (0) | 2022.07.13 |