JSP

makeCookie.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="javax.servlet.http.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<!-- /ch10/makeCookie.jsp -->

<%

	
	//쿠키(스트링만 저장 가능)
	Cookie cookie =new Cookie("id2", "김철수");//변수명, 값
	Cookie cookie2 =new Cookie("pwd", "1234");
	Cookie cookie3=new Cookie("age", "20");
	//cookie.setMaxAge(7*24*60*60);//쿠키의 유효시간(초)
	//cookie.setPath("/");//쿠키의 저장경로
	//cookie.setDomain("localhost");//쿠키의 도메인
	
	response.addCookie(cookie);//클라이언트에 쿠키가 저장됨
	response.addCookie(cookie2);
	response.addCookie(cookie3);
	
%>

쿠키가 생성되었습니다. <br>

<a href="useCookie.jsp">쿠키 확인</a>

</body>
</html>

 

쿠키가 생성되었습니다. 
쿠키 확인

 

 

 

 

useCookie.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<%

//String str="hello";
//pageContext.setAttribute("he", str);
Cookie[] cookies=request.getCookies();
//pageContext.setAttribute("co", cookies);
if( cookies !=null){
	for(int i=0; i<cookies.length; i++){
	//	if(cookies[i].getName().equals("pwd")){
			out.println("쿠키이름 :" + cookies[i].getName());
			out.println("쿠키 값 : " + cookies[i].getValue()+"<br>");			
//			break;
		//}
	}
}
%>
<p>

<!--
	cookie.변수명.name  쿠키변수의 이름
	cookie.변수명.value 쿠키변수에 저장된 값
  -->


<%-- 아이디 : ${pageScope.co.id.name} - ${pageScope.co.id.value} <br>
비밀번호 : ${pageScope.co.pwd.name} - ${ pageScope.co.pwd.value } <br>
나이 : ${ pageScope.co.age.name} - ${ pageScope.co.age.value } <br>
${he } --%>
<%-- ${pageScope.he } --%>

<hr>

<a href="deleteCookie.jsp">쿠키 삭제</a><br>
<a href="editCookie.jsp">쿠키 변경</a>



</body>
</html>





 

 

쿠키이름 :id2 쿠키 값 : 김철수
쿠키이름 :pwd 쿠키 값 : 1234
쿠키이름 :age 쿠키 값 : 20
쿠키이름 :JSESSIONID 쿠키 값 : 000AA1CFB05A42C62919D227DD92AFE6

 


쿠키 삭제
쿠키 변경

 

 

 

 

 

 

editCookie.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<%
	response.addCookie(new Cookie("id", "park"));
	
%>

쿠키가 변경되었습니다. <br>
<a href="useCookie.jsp">쿠키 확인</a>

</body>
</html>

 

 

쿠키가 변경되었습니다. 
쿠키 확인

 

 

 

 

 

deleteCookie.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>
<%

	Cookie cookie =new Cookie("id2", "");//쿠키값을 빈값으로
	Cookie cookie1 =new Cookie("age", "");
	Cookie cookie2=new Cookie("pwd", "");
	
	cookie.setMaxAge(0);//즉시 삭제
	cookie1.setMaxAge(0);
	cookie2.setMaxAge(0);
	response.addCookie(cookie);//쿠키가 변경됨(삭제됨)
	response.addCookie(cookie1);
	response.addCookie(cookie2);

%>

아이디 : ${cookie.id.value } <br>
비밀번호 :  ${cookie.pwd.value } <br>
쿠키가 삭제되었습니다.

<a href="useCookie.jsp">쿠키 확인</a>



</body>
</html>

 

아이디 : park 
비밀번호 : 1234 
쿠키가 삭제되었습니다. 쿠키 확인

 

 

 

 

 

counter.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="common.Util" %>
<%@ page import="java.util.Date" %>    
<%@ page import="java.lang.*" %>  
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script src="http://code.jquery.com/jquery-3.2.1.min.js"></script>

</head>
<body>

<%
//request.getCookies() 쿠키 정보 조회
String count =Util.getCookie(request.getCookies(), "count");



//최근 방문 시간 저장
Date date=new Date();
long now_time=date.getTime();//현재 시각
String visitTime=Util.getCookie(request.getCookies(), "visit_time");
long visit_time=0;
//방문시간 쿠키가 존재하면
if(visitTime !=null && !visitTime.equals("")){
	visit_time =Long.parseLong(visitTime);
}



int intCount=0;		
if(count==null || count.equals("")){
	//처음 접속
	 response.addCookie(new Cookie("count", "1"));
	//최초 방문 시간 저장
	response.addCookie(new Cookie("visit_time", Long.toString(now_time)));
	
 }else{
	 //최근 방문한 시간 이후 24시간이 경과되었을 때 카운트 증가 
	 long period=now_time - visit_time;
	 intCount=Integer.parseInt(count);
	 if(period> 60*24*24*1000){//24시간이 지났으면
		 //카운트 증가
		 intCount++;
		 //증가된 카운트값을 쿠키에 저장
		 response.addCookie(new Cookie("count", Integer.toString(intCount)));
		 //방문 시간 업데이트
		 response.addCookie(new Cookie("visit_time", Long.toString(now_time))); 
	 }
 }

//카운트 값을 스트링으로 변환
String counter=Integer.toString(intCount);
//문자열.charAt(인덱스) 몇번째 문자
for(int i=0; i<counter.length(); i++){
 
	String img="<img src='../images/"+counter.charAt(i)+".gif' >";
	out.println(img);
}
%>


<p>


<p>
 <a href="counterDelete.jsp">카우터 삭제</a>
</body>
</html>


 

 

77

 

카우터 삭제

 

 

 

 

 

 

 

 

 

 

about author

PHRASE

Level 60  머나먼나라

천리마 꼬리에 쉬파리 따라가듯 , 자기는 하는 일 없이 남에게 기대어 살거나 싸다님을 이르는 말.

댓글 ( 4)

댓글 남기기

작성