package ch15;

 

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.servlet.ServletException;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class CookieCutter extends HttpServlet {

 

       public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

              response.setContentType("text/html");

              PrintWriter out = response.getWriter();

 

              if (request.getParameter("setcookie") != null) {

 

                     Cookie cookie = new Cookie("Learningjava", "Cookies!");

                     //음수로 지정된경우 쿠키가 영구적으로 저장되면 않되며 브라우저가 종료될때 쿠키정보가 지워지게된다.

                     cookie.setMaxAge(3600);

                     response.addCookie(cookie);

 

                     out.println("<html><body><h1>Cookie Set...</h1>");

              } else {

                     out.println("<html><body>");

                     Cookie[] cookies = request.getCookies();

                     if (cookies.length == 0)

                           out.println("<h1>No cookies found...</h1>");

                     else

                           for (int i = 0; i < cookies.length; i++)

                                  out.print("<h1>Name: " + cookies[i].getName() + "<br>" + "Value: " + cookies[i].getValue()

                                                + "</h1>");

                     out.println("<p><a href=\"" + request.getRequestURI() + "?setcookie=true\">"

                                  + "Reset the Learning Java cookie.</a>");

              }

              out.println("</body></html>");

              out.close();

       }

}

 

'Java > Java for the Web' 카테고리의 다른 글

Filtering the Servlet Request & Response  (0) 2013.02.12
A Simple Filter  (0) 2013.02.12
Session정보 보기  (0) 2013.02.12
Hello Servlet  (0) 2013.02.12
Servlet Filter 개요  (2) 2013.02.03
Posted by Steven J.S Min
,