본문 바로가기

Java/java 기초

Servlet 쿠키값 넘기기

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>

<body>
    <!-- 
  저장공간
  login -> ID(String), 정보(Object: ID, PWD, NAME)

  cookie : 저장공간(client), String
      ex)id, password 저장하는 공간, 방문회수
      Java Script에서 접근이 가능.
      저장기한
      
  session : 저장공간(server), Object, setAttribute("한글X", Object), getAttribute
  		ex) id, MemberDto
     	저장기한(O), 방문회수
     	한글사용 금지!!!!! 
 -->
 
 <a href="hello">Hello Servlet</a>
</body>

</html>

 

package sample10;

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

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;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		Cookie cookie = new Cookie("id", "abc123");
		//쿠키저장
		resp.addCookie(cookie);
		
		cookie = new Cookie("pwd", "aabbcc");
		resp.addCookie(cookie);
		
		
		resp.setContentType("text/html; charset=utf-8");
		PrintWriter pw = resp.getWriter();

		pw.println("<html>");

		pw.println("<head>");
		pw.println("<title>제목</title>");
		pw.println("</head>");

		pw.println("<body>");
		
		pw.println("<p>Hello Servlet</p>");
		
		pw.println("<a href=dispCookie>Cookie를 표시 </a>");
		
		pw.println("</body>");

		pw.println("</html>");

		pw.close();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
	
}

hello servlet - > displaycookie -> visitedcookie

package sample10;

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

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;

@WebServlet("/dispCookie")
public class DisplayCookie extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html; charset=utf-8");
		PrintWriter pw = resp.getWriter();

		pw.println("<html>");

		pw.println("<head>");
		pw.println("<title>제목</title>");
		pw.println("</head>");

		pw.println("<body>");
		
		pw.println("<p>Display Cookie</p>");
		
		Cookie cookies[] = req.getCookies();
		if(cookies != null) {
			
			for(int i = 0; i <cookies.length; i++) {
				String value = cookies[i].getValue();
				pw.println("<p>");
				
				pw.println(cookies[i].getName());
				pw.println(":");
				pw.println(value);
				
				pw.println("</p>");
			}
		}
		
		pw.println("<a href=visitedCookie>방문회수</a>");
		
		pw.println("</body>");

		pw.println("</html>");

		pw.close();	
		}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
	
}

 

package sample10;

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

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;

@WebServlet("/visitedCookie")
public class VisitedCookie extends HttpServlet{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("text/html; charset=utf-8");
		PrintWriter pw = resp.getWriter();

		pw.println("<html>");

		pw.println("<head>");
		pw.println("<title>제목</title>");
		pw.println("</head>");

		pw.println("<body>");
		
		pw.println("<p>Visited Servlet</p>");
		
		Cookie cookies[] = req.getCookies();
		Cookie visitCookie = null;
		
		if(cookies != null) {
			//쿠키 있을떄
			
			for(int i = 0; i< cookies.length; i ++) {
				if(cookies[i].getName().equals("visited")) {
					visitCookie = cookies[i];
					break;
				}
			}
			// 찾음
			if(visitCookie != null) {
				int count = Integer.parseInt(visitCookie.getValue()) + 1;
				pw.println("<p>" + count + "번째 방문입니다" + "</p>");
				
				//쿠키값을 갱신해서 넣는다.
				visitCookie.setValue(count + "");
				//기한설정
				visitCookie.setMaxAge(60);	// 365 * 24 * 60 * 60 -> 1년
				resp.addCookie(visitCookie);
			}
			//못찾음
			else {
			
				// 쿠키가 한개도 없음 -> 생성
				pw.println("<p>첫번째 방문입니다</p>");
				Cookie newCookie = new Cookie("visited", "1");
				resp.addCookie(newCookie);
			}
		}else {
			// 쿠키가 한개도 없음 -> 생성
			pw.println("<p>첫번째 방문입니다</p>");
			Cookie newCookie = new Cookie("visited", "1");
			resp.addCookie(newCookie);
		}
		
		pw.println("<a href=visitedCookie>방문회수</a>");
		
		pw.println("</body>");

		pw.println("</html>");

		pw.close();	
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
	}
	
}

 

 

쿠키 확인