<!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 {
}
}
'Java > java 기초' 카테고리의 다른 글
Servlet 기초 응용 (0) | 2020.01.17 |
---|---|
Servlet session 넘기기및 삭제 (0) | 2020.01.17 |
servlet a 태그 받아오기 (0) | 2020.01.17 |
자바 JFRAME + JDBC + OracleDB를 활용한 카페 주문 시스템 구현하기 (3) | 2019.12.31 |
쓰레드를 활용한 오브젝트 전송하기 (0) | 2019.12.10 |