JSP³ª ȤÀº ServletÀ» ÀÌ¿ëÇÏ¿© ½ÎÀÌÆ®¸¦ ±¸Ãà Çҽÿ¡ Http Server¿Í Client
Browser»çÀÌ¿¡ SessionÀ» À¯ÁöÇϱâÀ§ÇØ Cookie³ª SessionÀ» ÀÌ¿ëÇÒ¼ö ¾ø´Â °æ¿ì¿¡
URL RewritingÀ» ÀÌ¿ëÇÏ´Â ¹æ¹ý.
1. Session Tracking
<html>
<head>
<title>Session Tracking Test</title>
</head>
<body>
<%
session = request.getSession (true);
%>
<h1>Session Tracking Test</h1>
<%
Integer ival = (Integer)session.getValue("counter");
if (ival == null) ival = new Integer (1);
else ival = new Integer (ival.intValue () + 1);
session.putValue ("counter", ival);
%>
This page accessed
<p>counter: <%= ival %>
<p>Session ID: <%=session.getId() %>
<p>Is New..? <%=session.isNew() %>
</body></html>
|
À§ÀÇ ¿¹Á¦¸¦ Browser¿¡¼ ½ÇÇà ÇßÀ» °æ¿ì °è¼Ó Á¢¼ÓÀ» ÇÏ°Ô µÇ¸é Counter°¡ Áõ°¡ ÇÏ°Ô
µË´Ï´ÙÁï Client¿ÍÀÇ SessionÀÌ °è¼Ó À¯Áö µÇ¸é¼ Server¿Í µ¿ÀÛ ÇÏ°Ô µË´Ï´Ù. ÇÏÁö¸¸
Browser¿¡¼ Cookie¸¦ disable½ÃÅ°¸é(client¿¡¼ SessionÀÇ Á¤º¸¸¦ À¯ÁöÇÏÁö
¾ÊÀ¸¸é) Á¦Á¢¼Ó½Ã ServerÂÊ¿¡¼´Â °è¼Ó »õ·Î¿î »ç¿ëÀÚ°¡ Á¢¼ÓÇÏ´Â °ÍÀ¸·Î ÀνÄÀ» ÇÏ°Ô
µË´Ï´Ù.
ÀÌ·± °æ¿ì Client Browser¿¡¼ Cookie¸¦ »ç¿ëÇÏ¸é º° ¹®Á¦°¡ »ý±âÁö ¾ÊÁö¸¸ ¸¸¾à
Cookie¸¦ disableÇسõ°í »ç¿ëÇÑ´Ù¸é Server¿ÍÀÇ SessionÀÌ ²÷±â°Ô µË´Ï´Ù.
°¡Àå Ä¡¸íÀûÀÎ ºÎºÐÀº »ç¿ëÀÚ Browser¿¡¼ Cookie¸¦ disableÇسõÀº °æ¿ì ÀüÀÚ
»ó°Å·¡°¡ ÀÌ·ç¾î ÁöÁö ¾Ê´Â ´Ù´Â °Ì´Ï´Ù.
¿ì¸®´Â ÀÌ·¯ÇÑ »óȲ¿¡¼ URL RewritingÀ» »ç¿ëÇÏ°Ô µË´Ï´Ù.
2.Session °´Ã¼¸¦ ÀÌ¿ëÇÑ Session Traking
source: booklist.jsp
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p>Book List </p>
<table border="1" width="183">
<tr>
<td height="50" width="48">
<img src="book1.jpg" width="45" height="60"></td>
<td height="50" width="119">
<a href=<%=response.encodeURL("Cart.jsp?content=book1")%>>±¸¸Å</a></td>
</tr>
<tr>
<td height="48" width="48">
<img src="book2.jpg" width="48" height="60"></td>
<td height="48" width="119">
<a href=<%=response.encodeURL("Cart.jsp?content=book2")%>>±¸¸Å</a></td>
</tr>
</table>
<p> </p>
</body>
</html>
|
source:Cart.jsp
<%@ page language="java" import = "java.lang.*" %>
<%
session = request.getSession (true);
%>
<% if (request.getParameter("content") == null) { %>
ÁÖ¹® ÇϽŠ¹°°ÇÀÌ ¾ø½À´Ï´Ù.
<%} else if (request.getParameter("content").equals("")) { %>
ÁÖ¹® ÇϽŠ¹°°ÇÀÌ ¾ø½À´Ï´Ù.
<%}else { String content = request.getParameter("content");
System.out.println(content);
if(content.equals("book1")){
Integer ii=(Integer)session.getAttribute(content);
if(ii==null){
session.setAttribute(content,new Integer(1));
}else{
Integer i =(Integer)session.getAttribute(content);
int count = (i.intValue());
count++;
session.setAttribute(content,new Integer(count));
}
}else if(content.equals("book2")){
Integer ii=(Integer)session.getAttribute(content);
if(ii==null){
session.setAttribute(content,new Integer(1));
}else{
Integer i =(Integer)session.getAttribute(content);
int count = (i.intValue());
count++;
session.setAttribute(content,new Integer(count));
}
}
}%>
<%
String all[]=session.getValueNames();
System.out.println("Size "+all.length);
for(int i=0;i<all.length;i++){
out.println("< p>"+all[i]+":"+((Integer)session.getValue(all[i])).intValue());
}
%>
< a href="/url/booklist.jsp">¸ñ·Ïº¸±â< /a>
|
À§ÀÇ µÎ°¡Áö jsp¹®¼¿¡¼ °¢°¢ÀÇ Ã¥À» ±¸ÀÔÇÏ°Ô µÇ¾úÀ»°æ¿ì À̳»¿ëÀ» Cart.jsp°¡ ¹Þ¾Æ¼
session¿¡ Ãß°¡ ÇÏ°Ô µË´Ï´Ù. °£´ÜÇÑ ¿¹Á¦¸¦ À§ÇØ servletÀ̳ª bean°°Àº°ÍÀº »ý·« Çϵµ·Ï
ÇÏ°Ú½À´Ï´Ù.
ÀÌ°æ¿ì browser¿¡¼ Cookie¸¦ disableÇØ ³õÀ¸¸é serverÂÊ¿¡¼´Â ¸Å
request¸¶´Ù »õ·Î¿î sessionÀ¸·Î ó¸® ÇÏ°Ô µË´Ï´Ù.
|