count 증가, 초기화

cookieTest02.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cookie Count 연습</title>
</head>
<body>
<a href="<%=request.getContextPath() %>/cookieCountServlet.do">
  Cookie Count 증가하기
</a><br><hr><br>
<a href="<%=request.getContextPath() %>/cookieCountDelServlet.do">
  Cookie Count 초기화하기
</a><br><hr><br>


</body>
</html>

cookieCountServlet

package kr.or.ddit.basic.cookie;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class cookieCountServlet
 */
@WebServlet("/cookieCountServlet.do")
public class cookieCountServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8");
		
		PrintWriter out = response.getWriter();
		
		//count라는 쿠키변수를 사용해서 처리할 예정임
		
		//count라는 쿠키가 있는지 검사한다.
		Cookie[] cookies = request.getCookies();
		int count = 0; //카운트 값 저장할 변수 선언 및 초기화
		
		if(cookies!=null) { //쿠키가 null이 아닐때만 count세기
		
			for(Cookie cookie : cookies) {
				 String name = cookie.getName();
				 if("count".equals(name)) {
					 // 현재의 count값 구하기
					 count = Integer.parseInt(cookie.getValue());
				     break;
				
			}
			
		}
		
	}
		count++; //count값 증가하기

		//증가된 count값을 저장한다.
		Cookie countCookie = new Cookie("count", String.valueOf(count));
		
		response.addCookie(countCookie);
		
		out.println("<html><head><meta charset='utf-8'>");
		out.println("<title>Cookie count 연습</title></head>");
		out.println("<body>");
		out.println("<h2>어서오세요 . 당신은 "+count+"번째 방문입니다.</h2>");
		out.println("<br><hr><br>");
				
		out.println("<a href='"
				+request.getContextPath()
				+"/cookieCountServlet.do'>카운트 증가하기</a>");
		
		out.println("<a href='"
				+request.getContextPath()
				+"/cookie/cookieTest02.jsp'>시작문서로 가기</a>");
		
		
		out.println("</body></html>");	
		
	}	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

cookieCountDelServlet

package kr.or.ddit.basic.cookie;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class cookieCountDelServlet
 */
@WebServlet("/cookieCountDelServlet.do")
public class cookieCountDelServlet extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=utf-8"); 
		PrintWriter out = response.getWriter();
		
		Cookie[] cookieArr = request.getCookies();
	
		if(cookieArr!=null) { //쿠키가 없을때만
			
			for(Cookie cookie : cookieArr) {
				String count = cookie.getName(); //쿠키변수 값 구하기
				
				if("count".equals(count)) { //삭제할 쿠키 찾기
					cookie.setMaxAge(0); //유지시간을 0으로 설정한다.
					response.addCookie(cookie);  //변경된 쿠키를 저장한다.
				    break;
				}
				
			}
			
		}
		out.println("<html><head><meta charset='utf-8'>");
		out.println("<title>Count Cookie 삭제하기</title></head>");
		out.println("<body>");
		out.println("<h2>Count가 초기화 되었습니다.</h2>");
		
		out.println("<a href='"
				+request.getContextPath()
				+"/cookie/cookieTest02.jsp'>시작문서로 가기</a>");
		
		out.println("</body></html>");
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

좋은 웹페이지 즐겨찾기