본문 바로가기

Servlet

id, 비번, 방문회수 보기// Cookie//쿠키 //WebServlet//저장공간

각각의 java 창에서 pw.println("<a href= dispCookie>Cookie를 표시</a>"); 를 통해 다음 java창으로 넘겨줌

 

 

첫링크 (클릭)

 

두번째 링크 (클릭)

 

세번째 링크(클릭)

 

4번째 화면, 개발자 도구Cookie에서 id, 비번, visited 횟수확인 가능

 

html

<body>
 <a href="hello" >hello Servelet(get)</a>
</body>

 

 

HelloServlet.java

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

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//id입력
		Cookie cookie = new Cookie("id","abc123");
		resp.addCookie(cookie);
		//pw입력
		cookie = new Cookie("pwd","aaaa1111");
		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("<a href= dispCookie>Cookie를 표시</a>");
		
		pw.println("</body>");
		pw.println("</html>");		
		pw.close();
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}
}

 

 

 

 

DisplayCookie.java

@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.print("<h3>Cookie Display</h3>");
		
		Cookie cookies[] = req.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				String cookieName = cookies[i].getName();
				String value = cookies[i].getValue();
				
				pw.println("<p>"+cookieName+":"+"</p>");
			}
		}
		pw.println("<a href= visitedCookie>Cookie를 표시</a>");
		pw.println("</body>");
        
		pw.println("</html>");		
		pw.close();
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}
}

 

 

 

 

VisitedCookie.java

@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>방문 회수</p>");
		Cookie cookies[] = req.getCookies();
		Cookie visitedCookie = null;
		if (cookies != null) {
			//검색
			for (int i = 0; i < cookies.length; i++) {
				if (cookies[i].getName().equals("visited")) {
					visitedCookie = cookies[i];
					break;
				}
			}
			//찾음
			if (visitedCookie != null) {
				int count = Integer.parseInt(visitedCookie.getValue())+1;
				pw.println("<p>"+count+"번째 방문입니다</p>");

				//cookie값을 갱신
				visitedCookie.setValue(count+"");
				visitedCookie.setMaxAge(60);//60초후에는 0번째방문으로 리셋됨
				//기간설정: 365*24*60*60 ->1년설정
				resp.addCookie(visitedCookie);//갱신값 다시 넣어줌
			}
			//못찾음
			else {
				//쿠키 생성
				pw.println("<p>첫번째 방문입니다</p>");
				Cookie newCookie = new Cookie("visited","1");
				resp.addCookie(newCookie);
			}
		}
		pw.println("</body>");
		pw.println("</html>");		
		pw.close();
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}
}