Cookie Counter



import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
 
public class cc extends HttpServlet {
 
        private PrintWriter toClient;
        private String FILENAME;
        private int MAX_DIGIT;
        private String COOKIE_NAME;
 
        public void init(ServletConfig sc) throws ServletException {
 
                super.init(sc);
                String temp;
                FILENAME = sc.getInitParameter("FILENAME");
                temp = sc.getInitParameter("MAX_DIGIT");
                COOKIE_NAME = sc.getInitParameter("COOKIE_NAME");
 
                try {
                        MAX_DIGIT = Integer.parseInt(temp);
                }catch(Exception e) {

                        error_handling (e, "ERROR parsing temp to MAX_DIGIT");
                }
 
                try {
                        File f = new File(FILENAME);
                        if(!f.exists()) {
                                RandomAccessFile raf = new RandomAccessFile(FILENAME,"rw");
                                raf.writeBytes("0");
                                raf.close();
                        }
                }catch (IOException e) {
                        error_handling(e, "Error checking file");
                }
        }
        
        protected synchronized int[] getHitCountOnly(String filename) {
                String dat=null,index;
                int now,datLength;
                int digit[] = new int[MAX_DIGIT];
                char temp;
 
                try {
                        RandomAccessFile fromFile = new RandomAccessFile("counter.dat","rw");
                        dat=fromFile.readLine();
                } catch(IOException e) {
                        error_handling (e, "Error get hit count");
                }
 
                datLength = dat.length();
                for (int i=0; i < MAX_DIGIT - datLength; i++)
                        digit[i]=0;
                for(int i = 0; i < datLength; i++) {
                        temp = dat.charAt(i);
                        index = temp+"";
                        digit[i + MAX_DIGIT - datLength] = Integer.parseInt(index);
                }
                return digit;
        }

        
        protected synchronized int[] UpdateHitCount(String filename) {
 
                String dat=null,index;
                int now,datLength;
                int digit[] = new int[MAX_DIGIT];
                char temp;
 
                try {
                        RandomAccessFile fromFile = new RandomAccessFile("counter.dat","rw");
                        dat=fromFile.readLine();
 
                        now = Integer.parseInt(dat);
                        now ++;
                        dat = Integer.toString(now);
                        fromFile.seek(0);
                        fromFile.writeBytes(now+"\n");
                        fromFile.close();
                } catch(IOException e) {
                        error_handling (e, "Error updating hit count");
                }
 
                datLength = dat.length();
                for (int i=0; i < MAX_DIGIT - datLength; i++) digit[i]=0;
                        for(int i = 0; i < datLength; i++) {
                                temp = dat.charAt(i);
                                index = temp+"";
                                digit[i + MAX_DIGIT - datLength] = Integer.parseInt(index);
                        }
                return digit;
        }
        protected Cookie getCookie(HttpServletRequest req, String c_name) {
                Cookie[] cookies = req.getCookies();
                if(cookies!=null) {
                        for(int i=0; i < cookies.length; i++) {
                                if(cookies[i].getName().equals(c_name)) {

                                        return cookies[i];
                                }
                        }
                return null;
        }

        public void setCookie(HttpServletResponse res, String c_name) {
                Date date = new Date();
                String visit = date.toString();
                Cookie logged = new Cookie(c_name, visit);
                res.addCookie(logged);
        }

        public void service (HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {
                
                int digit[] = new int[MAX_DIGIT];
                Cookie curr_Cookie = getCookie(req, COOKIE_NAME);
                if(curr_Cookie == null) {
                        setCookie(res,COOKIE_NAME);
                        digit = UpdateHitCount(FILENAME);
                } else {
                        digit = getHitCountOnly(FILENAME);
                }

                res.setContentType("text/html");
                toClient = new PrintWriter(res.getOutputStream(),true);

                toClient.println("<table border=0 cellpadding=0 cellspacing=0><tr>");
                for(int i = 0; i < MAX_DIGIT; i++)
                        toClient.println("<td><img src=http://hycedu.hanyang.ac.kr:1200/counter/"+digit[i]+".JPG border=0 vspace=0 hspace=0></td>");
                toClient.println("</tr></table>");
        }
 
        public void error_handling(Exception e, String msg) {
                System.err.println(msg);
                System.err.println(e);
        }

}

 

 

  À§ÀÇ ¼Ò½º´Â ¾Õ¿¡¼­ ÀÛ¼ºÇß´ø Ä«¿îÅÍ ¼Ò½º¸¦ ¾à°£ ¼ÕÀ» º» °ÍÀÔ´Ï´Ù. ¾Õ °­Á¿¡¼­ Cookie ¿¡ ´ëÇØ °øºÎÇÏ¿´À¸´Ï, ¹è¿î °ÍÀ» ½á ¸Ô¾î ºÁ¾ß ÇÏ°ÚÁö¿ä?

  ÀÌ Ä«¿îÅÍ´Â ¾ÕÀÇ Ä«¿îÅÍ¿Í ¹«¾ùÀÌ ´Ù¸¦±î¿ä? Å« Â÷ÀÌ´Â ¾ø½À´Ï´Ù. ´Ù¸¸ Çѹø µé¾î¿Ô´ø »ç¶÷ÀÌ ´Ù½Ã ÆäÀÌÁö¸¦ ¿ä±¸ ÇÏ´õ¶óµµ, ±× °ªÀÌ º¯ÇÏÁö ¾Ê´Â´Ù´Â Â÷ÀÌ »ÓÀÔ´Ï´Ù. ¹«½¼ ¼Ò¸®³Ä¸é, ¾î¶² »ç¶÷ÀÌ Á¢¼ÓÇØ ¿À¸é, ±× »ç¶÷ÀÇ À¥ ºê¶ó¿ìÀú¿¡ Cookie¸¦ ¸¸µì´Ï´Ù. ±×¸®°í óÀ½ µé¾î¿ÔÀ¸¹Ç·Î, Ä«¿îÆ®¸¦ ¿Ã¸®°í, È­¸é¿¡ »Ñ·ÁÁÝ´Ï´Ù. ´Ù½Ã ±× »ç¶÷ÀÌ µé¾î¿ÔÀ» ¶§´Â, ÄíÅ°ÀÇ À¯¹«¸¦ È®ÀÎÇÏ¿© ±×»ç¶÷ÀÌ ±âÁ¸¿¡ ¹æ¹®À» Çß´ø »ç¶÷ÀÎÁö¸¦ È®ÀÎÇÕ´Ï´Ù. ±×·¡¼­ ±âÁ¸¿¡ µé¾î¿Â ÀûÀÌ ÀÖÀ¸¸é, ÇöÀçÀÇ Ä«¿îÆ® ¼ö¸¦ ±×³É »Ñ·ÁÁֱ⸸ ÇÏ´Â °ÍÀÔ´Ï´Ù. ±âÁ¸ÀÇ Ä«¿îÅÍ¿¡¼­´Â »ç¿ëÀÚ°¡ µé¾î¿Í¼­ Reload¸¦ ´©¸£¸é ´©¸¦ ¶§¸¶´Ù Ä«¿îÆ® ¼ö°¡ ´Ã¾î ³µÁö¸¸, À̹ø Ä«¿îÅÍ´Â ´Ã¾î ³ªÁö ¾Ê´Â °ÍÀÌÁö¿ä. ´Ù¸¸ CookieÀÇ Á¾·á ½ÃÁ¡À» Default·Î Ç߱⠶§¹®¿¡, À¥ ºê¶ó¿ìÀúÀÇ Á¾·á¿Í µ¿½Ã¿¡ ÄíÅ°´Â »ç¶óÁý´Ï´Ù. ±×·¡¼­ ´Ù½Ã À¥ ºê¶ó¿ìÀú¸¦ ±âµ¿½ÃŲÈÄ Á¢¼ÓÇϸé Ä«¿îÆ®°¡ Áõ°¡µË´Ï´Ù. ±×·³ ÀÌÁ¦ ¼Ò½º¸¦ ºÐ¼®ÇØ º¸°Ú½À´Ï´Ù.

init ¸Þ¼Òµå¸¦ º¸¸é

COOKIE_NAME = sc.getInitParameter("COOKIE_NAME");

¶ó´Â °ÍÀÌ »õ·Î »ý°åÁö¿ä? »Ó¸¸ ¾Æ´Ï¶ó ¸â¹ö º¯¼ö¿¡µµ, String COOKIE_NAMEÀÌ »õ·ÎÀÌ ¸¸µé¾ú½À´Ï´Ù. ÀÌ°Ç Cookie¸¦ »ý¼ºÇÒ ¶§ »ç¿ëÇÒ CookieÀÇ À̸§ÀÌ µË´Ï´Ù. Cookie¿¡´Â À̸§°ú °ªÀÌ µé¾î°£´Ù´Â °ÍÀº ¾Õ¿¡¼­ °øºÎ Çß¾úÁö¿ä?--;

protected synchronized int[] getHitCountOnly(String filename)

»õ·Î »ý±ä ¸Þ¼ÒµåÀÔ´Ï´Ù. À§¿¡¼­ ¼³¸íÇßµíÀÌ ÀÌ¹Ì Cookie°¡ ¸¸µé¾îÁ® ÀÖ´Â »ç¶÷Àº Count¼ö¸¦ ¿Ã¸± ÇÊ¿ä°¡ ¾øÀ¸´Ï, ±×³É ÇöÀçÀÇ Count°ªÀ» ÀÐ¾î µéÀ̴µ¥ »ç¿ëÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù. ÀÌÀü nc.class¿¡¼­ÀÇ getHitCount() ¸Þ¼Òµå¿Í´Â Å« Â÷ÀÌ°¡ ¾ø½À´Ï´Ù. ´Ù¸¸ °ªÀ» Áõ°¡½ÃÄѼ­ ´Ù½Ã file ¿¡ writingÇÏ´Â ºÎºÐ¸¸ ºüÁ® ÀÖ½À´Ï´Ù.

protected synchronized int[] UpdateHitCount(String filename)

¿ª½Ã »õ·Î »ý±ä ¸Þ¼ÒµåÀÔ´Ï´Ù. ÇÏÁö¸¸ Â÷ÀÌ°¡ ¾ø³×¿ä. nc.class¿Í. °ªÀ» Àо Áõ°¡½ÃÅ°°í ´Ù½Ã ÀúÀåÇÏ´Â ³»¿ëÀ¸·Î µÇ¾î ÀÖ½À´Ï´Ù. ºÎ¿¬ ¼³¸íÀÌ ÇÊ¿ä ¾ø°ÚÁö¿ä?

protected Cookie getCookie(HttpServletRequest req, String c_name)

°¡Àå Áß¿äÇÑ ¸Þ¼Òµå°¡ µÇ°Ú³×¿ä. Cookie¸¦ ¾ò¾î³»´Â methodÀÔ´Ï´Ù. parameter·Î´Â HttpServletRequest¿Í StringÀ» ¹Þ´Â±º¿ä. ÀüÀÚ´Â ¸»¾¸À» ¾Èµå·Áµµ Àß ¾Æ½Ç °ÍÀÌ°í, c_nameÀ̶ó´Â StringÀº ¹Þ¾ÆµéÀÏ CookieÀÇ À̸§ÀÔ´Ï´Ù. ±×·¡¼­ °°Àº À̸§ÀÇ Cookie¸¦ ã¾Æ¼­ ±× Cookie¸¦ returnÇϵµ·Ï ÇÕ´Ï´Ù.

Cookie[] cookies = req.getCookies();

°¢°¢ÀÇ ºê¶ó¿ìÀú´Â ÇϳªÀÇ Cookie¸¸À» °¡Áö°í ÀÖ´Â °ÍÀÌ ¾Æ´Ï±â ¶§¹®¿¡, ¹Þ¾Æ¿À´Â Cookie´Â ¹Ýµå½Ã ¹è¿­·Î ¹Þ¾Æµé¿©¾ß ÇÕ´Ï´Ù. ±×·¡¼­ ¹Þ¾ÆµéÀÎ CookieÁß¿¡¼­ ¿øÇÏ´Â °ÍÀ» ã½À´Ï´Ù. ÄíÅ°ÀÇ À̸§ÀÌ °°Àº °ÍÀ» ÀǹÌÇÏ´Â °ÍÀÌÁö¿ä.

Cookie[] cookies = req.getCookies();
if(cookies!=null) {
          for(int i=0; i < cookies.length; i++) {
                     if(cookies[i].getName().equals(c_name)) {
                                 return cookies[i];
                     }
          }
}

À§ ³»¿ëÀº Cookie°¡ ÀÖ´Ù¸é, ±× CookieÁß¿¡¼­ À̸§ÀÌ °°Àº Cookie¸¦ ã¾Æ¼­ return½Ãŵ´Ï´Ù. ¾î·ÆÁö ¾ÊÀ¸´Ï ´õ ÀÌ»óÀÇ ºÎ¿¬Àº ÇÊ¿ä ¾ø°ÚÁö¿ä?

public void setCookie(HttpServletResponse res, String c_name)

ÀÌ ¸Þ¼Òµå´Â ¹Þ¾ÆµéÀÎ c_nameÀ¸·Î Cookie¸¦ ¼³Á¤ÇÏ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù. À̹ø¿¡´Â »ý¼ºÀ» ÇÏ´Â °ÍÀÌ´Ï ´ç¿¬È÷ HttpServletResponse °´Ã¼¸¦ ¹Þ¾Æ¼­ ÇØ¾ß ÇÏ°ÚÁö¿ä? ±×·¡¼­

Date date = new Date();
String visit = date.toString();
Cookie logged = new Cookie(c_name, visit);
res.addCookie(logged);

ÇöÀçÀÇ ½Ã°£À» ÀÌ¿ëÇؼ­ Cookie¸¦ res.addCookie(Cookie c)¶ó´Â ¸Þ¼Òµå¸¦ ÀÌ¿ëÇؼ­ »ý¼ºÇØ ÁÝ´Ï´Ù. ¾î·ÆÁö ¾Ê°Ô »ç¿ëÇÏ½Ç ¼ö ÀÖ°ÚÁö¿ä? ¿©±â¼­, CookieÀÇ °ªÀ» ÇöÀç ½Ã°£À¸·Î Çߴµ¥, ÀÌ°É ÀÌ¿ëÇؼ­ »ç¿ëÀÚ¿¡°Ô ´ç½ÅÀº ¾ðÁ¦ ¿À¼Ì¾ú±º¿ä, ¶ó°í ¸»ÇÒ ¼öµµ ÀÖ°ÚÁö¿ä? ¶Ç´Â ¼ýÀÚ¸¦ ÀÔ·ÂÇØÁ־ ¸î ¹ø° ¹æ¹®ÀÔ´Ï´Ù ¶ó°í ¸» ÇÒ ¼öµµ Àְڳ׿ä....*^^*

service ¸Þ¼Òµå´Â Cookie°¡ ÀÖ´ÂÁö ¾ø´ÂÁö¸¦ È®ÀÎÇؼ­ ÀûÀýÇÑ ¸Þ¼Òµå¸¦ È£Ãâ ÇÑ ´ÙÀ½¿¡ ¹ÞÀº °ªÀ» ÀÌ¿ëÇؼ­ È­¸é¿¡ »Ñ·ÁÁÖ´Â ÀÏÀ» ÇÕ´Ï´Ù. ±×·³ Àß ¾Æ½Ã°ÚÁö¿ä?

 



¨Ï 1999 Lee Min Goo, All rights are reserved Last modified 1999.2
If you have any question, send mail to bluehope