 |
|
|  |
|
 |
14³â Àü |
// sessionÀ» ÀÌ¿ëÇÑ Ä«¿îÅÍ
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionCounter extends HttpServlet
{
static final String Counter_key ="Counter.count";
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
// Ŭ¶óÀ̾ðÆ®·Î ºÎÅÍ ¼¼¼ÇÀ» °¡Áö°í ¿À´Âµ¥.
// true À϶§¸¸ ¼¼¼ÇÀÇ °´Ã¼¸¦ ¹ÝȯÇÕ´Ï´Ù.
HttpSession session = req.getSession(true);
res.setContentType("text/html");
PrintWriter pout = new PrintWriter(
new OutputStreamWriter(
res.getOutputStream(),"KSC5601"
)
);
int count =1;
Integer i =(Integer) session.getAttribute(Counter_key);
//¸¸¾à ±× Àü¿¡ Ä«¿îÅÍ°¡ ÀÖ´Ù¸é ±× °ªÀ» ´õÇØÁÝ´Ï´Ù.
if( i != null)
{
count=i.intValue()+1;
}//if ¹® ´Ý±â
// ÄíÅ°°ü·Ã ½ÃÀÛ!!
// »ç¿ëÀÚ¿¡°Ô ÀúÀåµÈ ÄíÅ°¸¦ ¸ùâ °¡Á®¿Â´Ù. ±×¸®°í ÀúÀåµÈ ½Ã°£µµ °¡Á®¿Â´ç..
Cookie cookies[] = req.getCookies();
Cookie cookie = null;
long cookieDate = 0L;
if(cookies != null)
{
cookie = cookies[0];
cookieDate = Long.parseLong(cookie.getValue());
}
else
{
Cookie coo = new Cookie("TodayCheck", new String(""+System.currentTimeMillis()));
coo.setMaxAge(60*60*24);
res.addCookie(coo);
}
long nowTime = System.currentTimeMillis() - cookieDate;
if(nowTime > 60*60*24*1000)
{
// ¿©±â Á¶±Ý ÀÌ»óÇÔ.. MilliSecond ¸¦ Áشٰí Çؼ
// 1000À» °öÇØÁÖ±â´Â Çߴµ¥ Á¤È®ÇÑÁö´Â ¸ð¸£°ÚÀ½.
// API º¸°í ¼öÁ¤¿ä¸Á..
System.out.println("ÇÏ·ç°¡ Áö³µ½À´Ï´Ù. Ä«¿îÆ®¸¦ Áõ°¡ÇÕ´Ï´Ù.");
//»õ¼Ç¾È¿¡ ¹ÝȯµÈ Ä«¿îÅÍÀÇ °ªÀ» Ãß°¡ÇÕ´Ï´Ù.
session.setAttribute(Counter_key, new Integer(count));
}
else
{
System.out.println("ÇÏ·ç°¡ ¾ÈÁö³µ½À´Ï´Ù. ´ç½ÅÀ» Áõ°¡´ë»ó¿¡¼ Á¦¿ÜÇÕ´Ï´Ù~~(°³±×ÄܼƮ¹öÁ¯)");
}
// Cookie Ŭ·¡½º °ü·Ã ³¡... ½ÇÇàÇϽðí ÄíÅ°°¡ »ý¼ºµÇ¾ú´ÂÁö Cookie µð·ºÅ丮¿¡¼ È®Àοä¸Á..
pout.println("<html>");
pout.println("<head>");
pout.println("<title> ¼¼¼ÇÀ¸·Î ¸¸µç Ä«¿îÅÍ</title>");
pout.println("</head>");
pout.println("<body>");
pout.println("´ç½ÅÀÇ ¼¼¼Ç¾ÆÀ̵ð´Â <b>"+session.getId()+"</b>");
pout.println("ÀÌ¸ç ´ç½ÅÀÇ ¹æ¹®È½¼ö´Â <b>"+count+" ° ÀÔ´Ï´Ù.</b>");
pout.println("<form method=GET action=""+req.getRequestURI()+"">");
pout.println("<input type=submit value="´Ù½Ãº¸±â">");
pout.println("</form>");
pout.println("</body></html>");
pout.flush();
}// doGet() ´Ý±â
}// SessionCounter´Ý±â |
|
̵̧ : 400 |
̵̧
¸ñ·Ï
|
|
|  |
|