728x90
반응형
웹 프로그래밍을 하다 보면 자연스럽게 나오게 되는 부분이 쿠키(Cookie)이다.
Java에서도 쿠키를 사용할 수 있는데, 다음과 같은 방식으로 사용하면 된다.
1. Cookie 객체 생성
2. Cookie 생명 주기 설정
3. Response에 Cookie 추카
4. Request에서 Cookie 일어오기
예제
1. 쿠키를 생성하고 설정하는 서블릿
2. 간단한 사용자 정의 쿠키 예제
Java에서도 쿠키를 사용할 수 있는데, 다음과 같은 방식으로 사용하면 된다.
1. Cookie 객체 생성
Cookie cookie = new Cookie("username", name);
2. Cookie 생명 주기 설정
cookie.setMaxAge(30*60); //30분...
3. Response에 Cookie 추카
response.addCookie(cookie);
4. Request에서 Cookie 일어오기
Cookie[] cookies = request.getCookies();
for(int i = 0 ; i<cookies.length ; i++) {
Cookie cookie = cookies[i];
if(cookie.getName().equals("username")) {
String userName = cookie.getValue();
out.println("Hello " + userName);
break;
}
}
for(int i = 0 ; i<cookies.length ; i++) {
Cookie cookie = cookies[i];
if(cookie.getName().equals("username")) {
String userName = cookie.getValue();
out.println("Hello " + userName);
break;
}
}
예제
1. 쿠키를 생성하고 설정하는 서블릿
package com.example;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 CookieTest extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
String name = request.getParameter("username");
Cookie cookie = new Cookie("username", name);
cookie.setMaxAge(30*60);
response.addCookie(cookie);
RequestDispatcher view = request.getRequestDispatcher("cookieresult.jsp");
view.forward(request, response);
}
}
import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 CookieTest extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
String name = request.getParameter("username");
Cookie cookie = new Cookie("username", name);
cookie.setMaxAge(30*60);
response.addCookie(cookie);
RequestDispatcher view = request.getRequestDispatcher("cookieresult.jsp");
view.forward(request, response);
}
}
2. 간단한 사용자 정의 쿠키 예제
package com.example.web;
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 CheckCookie extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie[] cookies = request.getCookies();
for(int i=0 ; i<cookies.length ; i++) {
Cookie cookie = cookies[i];
if(cookie.getName().equals("username")) {
String userName = cookie.getValue();
out.println("Hello " + userName);
break;
}
}
}
}
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 CheckCookie extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie[] cookies = request.getCookies();
for(int i=0 ; i<cookies.length ; i++) {
Cookie cookie = cookies[i];
if(cookie.getName().equals("username")) {
String userName = cookie.getValue();
out.println("Hello " + userName);
break;
}
}
}
}
728x90
반응형
'Java > Servlet & JSP' 카테고리의 다른 글
리스너 예제 (2) | 2009.02.03 |
---|---|
HttpSessionBindingListener와 HttpSessionAttributeListener (0) | 2009.02.02 |
Session 관리 (0) | 2009.01.30 |
스레드 안전한 ServletContext 만들기 (1) | 2009.01.29 |
ServletContext 이용하기 (2) | 2009.01.23 |