각각의 java 창에서 pw.println("<a href= dispCookie>Cookie를 표시</a>"); 를 통해 다음 java창으로 넘겨줌
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 {
}
}
'Servlet' 카테고리의 다른 글
송장정보// 출력 (0) | 2020.07.21 |
---|---|
session//세션 삭제//id//방문회수//name 삭제 (0) | 2020.07.21 |
회원정보// .jsp파일로 넘기기 (0) | 2020.07.21 |
select로 웹사이트 주소 송신하기 (0) | 2020.07.20 |
에러상태 코드 select로 값을 넘기기 (웹으로 확인하기) (0) | 2020.07.20 |