Java에서 Cookie 값 꺼내쓰기 유틸.. 그거/Java2011. 9. 30. 10:02
걍.. 만들어 봄 ㅎㅎ
makeCookie.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<%@ page import="java.net.*" %>
<%
Cookie cookie1 = new Cookie("CJW1", URLEncoder.encode("cAT=A&cBT=B&cCT=C&cDT=D", "euc-kr"));
Cookie cookie2 = new Cookie("CJW2", URLEncoder.encode("cAT=A&cBT=B&cCT=C&cDT=D", "euc-kr"));
Cookie cookie3 = new Cookie("CJW3", URLEncoder.encode("cAT=A&cBT=B&cCT=C&cDT=D", "euc-kr"));
Cookie cookie4 = new Cookie("CJW4", URLEncoder.encode("cAT=A&cBT=B&cCT=C&cDT=D", "euc-kr"));
response.addCookie(cookie1);
response.addCookie(cookie2);
response.addCookie(cookie3);
response.addCookie(cookie4);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<a href="getCookie.jsp">getCookie</a>
</body>
</html>
getCookie.jsp
<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page import="java.util.*" %>
<%@ page import="java.net.*" %>
<%!
class CookieBook {
private Cookie[] cookies = null;
private HashMap<String, String> cookiesMap = new HashMap<String, String>();
public CookieBook(Cookie[] cookies) {
this.cookies = cookies;
makeCookiesMap();
}
private void makeCookiesMap() {
for(Cookie cookie : cookies) {
String value = null;
String key = cookie.getName();
try {
value = URLDecoder.decode(cookie.getValue(), "euc-kr");
} catch(Exception e) {
value = cookie.getValue();
}
cookiesMap.put(key, value);
if( value.indexOf("&") > 0 ) {
parseData(key, value);
}
}
}
public String getCookieValue(String key) {
if( cookiesMap.containsKey(key) ) {
return cookiesMap.get(key);
} else {
return null;
}
}
private void parseData(String key, String value) {
System.out.println(value);
String[] datas = value.split("&");
for(String data : datas) {
int idx = data.indexOf("=");
if( idx != -1 ) {
String tmpKey = key + "." + data.substring(0, idx);
String tmpData = data.substring(idx+1);
cookiesMap.put(tmpKey, tmpData);
}
}
}
public HashMap<String, String> getMap() {
return cookiesMap;
}
}
%>
<%
CookieBook cookBook = new CookieBook(request.getCookies());
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
<table border=1>
<tr>
<th>KEY</th><th>VALUE</th>
</tr>
<%
Iterator<String> keys = cookBook.getMap().keySet().iterator();
while(keys.hasNext()) {
String key = keys.next();
%>
<tr>
<td><%= key %></td><td><%= cookBook.getCookieValue(key) %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
CookieBook 이라는 유틸 클래스 만들어서 사용 ㅎ
KEY | VALUE |
CJW4.cCT | C |
CJW4 | cAT=A&cBT=B&cCT=C&cDT=D |
JSESSIONID | HgjlTGnbyRp75pBBPGCXGN3myQgyL7pYVTFvfGgFvBgTpPYhzzTM!-702582978 |
요딴식으로 보임.
'그거 > Java' 카테고리의 다른 글
NoClassDefFoundError vs ClassNotFoundException (0) | 2011.10.11 |
---|---|
Eclipse 에서 Maven 이용해서 remote repository 에 deploy 하기 (0) | 2011.10.05 |
MSSQL의 JDBC 로의 컬럼 타입 매핑 (0) | 2011.08.08 |
String.sprlit() 의 사용.. (0) | 2011.07.12 |
String.intern() 메소드 (0) | 2011.05.02 |