본문 바로가기

개발 회고

[솔데스크] 풀스택 과정 11주-2 총48회차 ‘회고’ | JSP(0214)

[서론]

○● 0214 새로웠던 것
1. JspQuest_19_to_33에서 jstl 쓰나봄. 그냥 쓰려고 하니까 에러나서 

 

 

- 1) 파일들 넣고 - 2) Q30에서 }에러 나는거 에러 나는 구간의 앞쪽에서 닫고 나머지는 주석 처리 해두기

 

2. 작업관리자 단축키: ctrl+shift+esc

 

 

[본론]
○● 0214 내용 정리
 
● JSP(서블릿)

 

○○●●● 상태지속을 위한 방법: cookie

 

-사용법

Cookie [] cookies = request.getCookies();   -> 생성할 때 배열로 함

 

쿠키 참고: [책:처음해보는 Servlet&JSP... ][p.205~214]
깃 허브 소스 참고
특징 1. 세션은 서버에 저장되지만 쿠키는 클라이언트 쪽에 저장됨. javax.servlet.http.Cookie
2. 세션과 다르게 쿠키는 각각의 쿠키별로 유통기한을 다르게 줄 수 있음

쿠키 () 생성 Cookie c = new Cookie("CookieName", "사과쿠키");
생성자 이용해서 바로 이름, 값 전달 가능
여러개의 쿠키를 생성해서 add 해도 됨. 
단, 객체 생성은 쿠키 당 하나씩 하고 키도 다르게 줘야 됨.

세팅 c.setValue("딸기쿠키");
이걸 하면 값이 변경됨. ( ex. 사과쿠키 값에서 딸기쿠키로 바뀜 )
전달 response.addCookie(c); 클라이언트 응답 객체에 쿠키를 실음 ( 상차 )
아직 전달된게 아님. 이 웹 페이지를 유저가 브라우저로 다운받아야 쿠키 구워짐

읽기 request.getCookies(); Cookie [] 배열이 리턴됨 << 배열임. 주의.
getCookie("키이름") 식으로 하나만 따로 골라내서 꺼내는 함수는 없음. 
(필요하면 만드시오)

쿠키 하나의 이름 읽기: ex. c.getName()
쿠키 하나의 값 읽기: ex. c.getValue()

수명주기 c.setMaxAge(int expiry); 초 단위임.
유통기한을 안준 경우 ( setMaxAge 함수를 실행 안한 경우 ) 에는 
브라우저가 열려있는 동안만 쿠키값이 유지됨.
참고 : JSP에서 쿠키의 수명은 클라이언트 측에서 다운로드된 시점부터 시작됩니다.


쿠키 삭제하기
Cookie c = new Cookie("CookieName", "아무거나,nul이나"); 1.객체 또 만들긴 해야됨 << 귀찮지만 이렇게
c.setMaxAge(0); 2.수명 0초로 세팅하고 << 귀찮지만 이렇게
response.addCookie(c); 3.전송도 해야함 << 귀찮지만 이렇게
* 참고 * 브라우저에서 쿠키 값 확인 법:
쿠키 확인(브라우저에서 F12 -> Application - 쿠키항목 - > 클릭) 
( 우상단 =x 누르면 쿠키 삭제도 됨 )

쿠키의 유효성 (p.325) 기본 쿠키(지속시간 설정 안한거)는 브라우저 끄면 무효화됨.
지속시간 설정 한거는 유지됨

쿠키흐름 (p.322) 첫 접속 서버에서 쿠키 구워서 클라에 보냄
두번째 접속 클라가 서버에 쿠키 보냄

쿠키스펙 (p.323)
각 쿠키 크기 4KB ( 4* 1024 Bytes )
한 사이트 쿠키 max 20개 하나의 사이트 당 20개의 쿠키만 허용
클라 저장 쿠키max 300개

참고 JSESSIONID 세션도 쿠키를 이용함. 톰캣 컨테이너에서 세션을 유지하기 위해 발급하는 키. 
but 이 세션 아이디만 클라이언트에 저장될 뿐 나머지 세션 정보는 서버에 있음. 
또한 클라이언트에서 직접 파일로 확인 불가함. 참고 url: https://dmobi.tistory.com/136
ex. cookCookie.jsp tasteCookie.jsp

참고: 실 저장되는 곳. (크롬 기준)
C:\Users\sdedu\AppData\Local\Google\Chrome\User Data\Default\Network\Cookies

참고: 브라우저 상에서 쿠키 값 확인하는 법:
F12 - 어플리케이션 메뉴 - 쿠키 - 해당 url
에러 case: Cookie 클래스명 아래에 빨간 밑줄가고 에러 메세지는
Cookie cannot be resolved to a type 나오는 상태

import javax.servlet.http.Cookie; << 이런 임포트가 빠진문제가 아니였음..
import jakarta.servlet.http.Cookie;
<< jsp 기준에서 import 안해도 쓸 수 있는 클래스임.
그런데도 에러가 났는데 이런 케이스였음.
해당 pj 에 Server Runtime [Apache Tomcat v10.0] 이런 폴더가 통째로 빠진 상태였음.
해결 : pj - 우클릭 - 빌드패스 - 컨픽 빌드패스 - class path 선택 
- add 라이브러리 - 서버 런타임 - 톰캣10(깔린거) 선택 후 정상 확인.

 

+ 실행 사례는 cookie파일을 참고할 것

 

JspQuest_19_to_33> src>main>webapp > index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="common.css">
</head>
<body>
<!-- ////////////////////	멤버		///////////////////////// -->
<fieldset>
	<legend id="member">멤버(로그인/회원가입)</legend>
	<ul>
		<li><a href="reg.html">[q19]회원가입</a></li>
		<li><a href="login.html">[q20]로그인</a></li>
	</ul>
</fieldset>	

<!-- ////////////////////	쿠키		///////////////////////// -->
<fieldset>
	<legend id="cookie">쿠키</legend>
	<ul>
		<li><a href="cookCookie.jsp">[q21]쿠키 굽기</a></li>
		<li><a href="countCookie.jsp">[q22]쿠키 카운터</a></li>
	</ul>
</fieldset>	

<!-- ////////////////////	자바 빈	///////////////////////// -->

<fieldset>
	<legend id="bean">자바 빈</legend>
	<a href="testBean.jsp?name=개&number=1">[q23]테스트 빈</a>
	<fieldset>
		<legend>[q24]빈</legend>
		<form action="testBean.jsp">
			이름<input name="name">
			넘버<input name="number">
			<input type="submit" value="빈 저장">
		</form>
	</fieldset>
	<fieldset>
		<legend>[q25]빈</legend>
		<form action="testBean2.jsp">
			이름<input name="name">
			넘버<input name="number">
			<input type="submit" value="빈 저장">
		</form>
	</fieldset>
</fieldset>

<!-- ////////////////////	EL	///////////////////////// -->
<fieldset>
	<legend id="el">EL</legend>
		<a href="el_set_attribute_1.jsp">setAttribute 테스트</a>
		<fieldset>
			<legend>[q26]</legend>
			<form action="elTest.jsp" method="get">
				이름<input name="name">
				나이<input name="age">
				<input type="submit" value="el테스트">
			</form>
		</fieldset>
		
		<fieldset>
			<legend>el을 써서 a페이지에서 setAttribute 로 저장 후 b로 보낸 값 꺼내기</legend>
			<a href="el_temp_1.jsp">확인</a>
		</fieldset>
				
		<fieldset>
			<legend>el add</legend>
			<a href="el_add_1.jsp">확인</a>
		</fieldset>		
		<fieldset>
			<legend>el add by form</legend>
			<a href="el_add_by_form_1.jsp">확인</a>
		</fieldset>		
		
		<fieldset>
			<legend>el 내장객체 순서</legend>
			<a href="el_inner_objs_order_1.jsp">확인</a>
		</fieldset>
		<fieldset>
			<legend>el 내장객체 순서2</legend>
			<a href="el_inner_objs_order_1.jsp?cat=1234">확인</a>
		</fieldset>
</fieldset>

<!-- ////////////////////	JSTL	///////////////////////// -->
<fieldset>
	<legend id="jstl">JSTL</legend>
	<fieldset>
		<legend>[q..]</legend>
		<a href="jstl_q27.jsp">[q27]</a>
		<a href="jstl_q28.jsp">[q28]</a>
		<a href="jstl_q29.jsp">[q29]</a>
		<a href="jstl_q30.html">[q30]조건문</a>
		<a href="jstl_q31.jsp">[q31]</a>
		<a href="jstl_q32.jsp">[q32]</a>
		<a href="jstl_q33.jsp">[q33]</a>
	</fieldset>
	<fieldset>
		<legend>반복문 추가</legend>
		<a href="jstl_foreach_1.jsp">test_foreach</a>
	</fieldset>
	<fieldset>
		<legend>test</legend>
		<a href="jstl_test.jsp">test</a>
	</fieldset>
</fieldset>
</body>
</html>

 

JspQuest_19_to_33> src>main>webapp > el_add_1.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.util.*,java.sql.*" %>

<%@ 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.setAttribute("x","1");
	request.setAttribute("y","2");
	request.getRequestDispatcher("el_add_2.jsp").forward(request, response);
	//response.sendRedirect("el_temp_2.jsp");
%>

</body>
</html>

 


 

JspQuest_19_to_33> src>main>webapp > el_add_2.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.util.*,java.sql.*" %>

<%@ 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>
${x}
${y}
${x + y}

</body>
</html>

 


 

JspQuest_19_to_33> src>main>webapp > el_add_by_form_1.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.util.*,java.sql.*" %>

<%@ 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="el_add_by_form_2.jsp">
	<input name="x">+
	<input name="y">
	<input type="submit">
</form>

</body>
</html>

 


 

JspQuest_19_to_33> src>main>webapp > el_add_by_form_2.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.util.*,java.sql.*" %>

<%@ 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>

${param.x}
${param.y}

${param.x + param.y}

</body>
</html>

 


 

JspQuest_19_to_33> src>main>webapp > el_inner_objs_order_1.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.util.*,java.sql.*" %>

<%@ 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>

<%
//주석 풀어가면서 테스트. 단, 세션은 브라우저가 켜있으면 유지되므로 주의.
// invalidate 호출해서 날릴 필요 있는 경우 구분해서 처리 할 것.

// 	request.setAttribute("key","밸-리퀘");
 	session.setAttribute("key","밸-세션");
 	
// 	session.invalidate();
	application.setAttribute("key","밸-앱");

%>
<jsp:forward page="el_inner_objs_order_2.jsp"></jsp:forward>
	
</body>
</html>

 

JspQuest_19_to_33> src>main>webapp > el_inner_objs_order_2.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.util.*,java.sql.*" %>

<%@ 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.getAttribute("key");
//	session.getAttribute("key");
// 	application.getAttribute("key");

%>

${key}

	
</body>
</html>

 


 

JspQuest_19_to_33> src>main>webapp > el_set_attribute_1.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.util.*,java.sql.*" %>

<%@ 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.setAttribute("cat_name","kitty");
%>

<jsp:forward page="el_set_attribute_2.jsp"></jsp:forward>


	
</body>
</html>

 


 

JspQuest_19_to_33> src>main>webapp > el_set_attribute_2.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.util.*,java.sql.*" %>

<%@ 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>

고양이 이름은? ${cat_name}


	
</body>
</html>

 


 

JspQuest_19_to_33> src>main>webapp > el_temp_1.jsp

<%@ page import="java.util.*" %>
<%@ page import="java.util.*,java.sql.*" %>

<%@ 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.setAttribute("catName","야옹이");
	request.getRequestDispatcher("el_temp_2.jsp").forward(request, response);
	//response.sendRedirect("el_temp_2.jsp");
%>

</body>
</html>

 


 

JspQuest_19_to_33> src>main>webapp > el_temp_2.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>

너의 이름은? ${catName}

</body>
</html>

 

 

[결론]
 
* JSP 기초 강의 수강

 

*작업파일


 0214
1) JSP 기초(서블릿)