New Counter Servlet

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
 
public class nc extends HttpServlet {
 
private PrintWriter toClient;
private static String FILENAME;
private int static MAX_DIGIT;
 
public void init(ServletConfig sc) throws ServletException {
 
super.init(sc);
String temp;
FILENAME = sc.getInitParameter("FILENAME");
temp = sc.getInitParameter("MAX_DIGIT");
 
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");
}
}
 
public synchronized int[] getHitCount(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 ++;
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;
}
 
public void service (HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {
 
int digit[] = new int[MAX_DIGIT];
digit = getHitCount(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://contest.hanyang.ac.kr:8080/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);
}
}

¿¹Á¦ 3. »õ·Î¿î Ä«¿îÅÍ ¼­ºí¸´


ÀÌ »õ·Î¿î Ä«¿îÅÍ ¼­ºí¸´Àº ¾Õ¿¡¼­ º» Ä«¿îÅÍ ¼­ºí¸´°ú ¹«½¼ Â÷ÀÌ°¡ ÀÖÀ»±î¿ä? À§¿¡¼­ servlet.properties¶ó´Â ³»¿ë ±â¾ï ³ª½Ê´Ï±î? ±× ³»¿ëÀ» ¼³¸íµå¸®±â À§Çؼ­ »õ·Î ¸¸µç ¼­ºí¸´ÀÔ´Ï´Ù. ÀÚ ±×·³ ±×³»¿ëÀÌ ¾î¶»°Ô Àû¿ëµÇ¾úÀ»±î¿ä? ¿¹Á¦ 2¿¡¼­ÀÇ Ä«¿îÅÍ´Â È­¸é¿¡ º¸¿©Áö´Â ÀÚ¸®¼ö¸¦ º¯°æÇÏ·Á¸é ¼Ò½º Äڵ带 °íÃļ­ ´Ù½Ã ÄÄÆÄÀÏ Çؾ߸¸ Çß½À´Ï´Ù. ±×¸®°í hit count°¡ ÀúÀåµÇ¾î ÀÖ´Â È­ÀÏÀÇ À̸§À» ¹Ù²Ù±â À§Çؼ­µµ¿ä. ±×·¯´Ï ¿¹Á¦ 3ÀÇ Ä«¿îÅÍ´Â ±×·¸Áö ¾Ê½À´Ï´Ù. °£´ÜÈ÷ servlet.properties¶ó´Â ÆÄÀÏ¿¡ ÇÊ¿äÇÑ µ¥ÀÌÅ͸¦ ÀÔ·ÂÇØÁÖ¸é, ÀÚ¸®¼ö¿Í ÀúÀåµÇ´Â È­ÀÏÀÇ À̸§À» ¹Ù²Ù¾î ÁÙ ¼ö ÀÖ½À´Ï´Ù. ÀÚ¼¼È÷ ¾Ë¾Æ º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù. ¿ì¼± Àü¿¡ ¾ø´ø

public void init(ServletConfig sc) throws ServletException

¶ó´Â »õ·Î¿î method°¡ »ý°å½À´Ï´Ù. initÀ̶ó´Â ¸Þ¼Òµå´Â Àü¿¡ ¼³¸íÀ» µå¸° ÀûÀÌ ÀÖ½À´Ï´Ù. ±â¾ïÀÌ ³ª½Ê´Ï±î? À§¿¡¼­ GenericServlet Ŭ·¡½º¸¦ ¼³¸íÇÒ¶§ GenericServlet¿¡ ¼ÓÇÑ method¿´Áö¿ä. ÀÌ ¸Þ¼Òµå´Â ¼­ºí¸´ÀÇ ÃʱâÈ­¸¦ ´ã´çÇÑ´Ù°í ¸»¾¸ µå·È¾ú½À´Ï´Ù. ±×¸®°í ¹Ýµå½Ã super.init(ServletConfig) ¸Þ¼Òµå¸¦ È£ÃâÇÏ¿©¾ß ÇÑ´Ù°íµµ¿ä. ±×·¡¼­ ¿ì¼±

super.init(sc);

À» È£ÃâÀ» ÇÕ´Ï´Ù. ´Ù½Ã ¼³¸íÀ» µå¸®ÀÚ¸é, ¼­ºí¸´Àº óÀ½ È£ÃâµÉ¶§ Àú init¸Þ¼Òµå°¡ È£ÃâÀÌ µË´Ï´Ù. ServletConfig¶ó´Â object¸¦ °¡Áö°í È£ÃâµÇ´Â ÀÌ ¸Þ¼Òµå´Â GenericServlet Ŭ·¡½º¿¡ ¼±¾ðµÇ¾î Àִµ¥, ¼­ºí¸´ÀÇ logingµî°ú °°Àº ÀÏÀ» Çϱ⠶§¹®¿¡ ¹Ýµå½Ã È£ÃâÀÌ µÇ¾î¾ß ÇÑ´Ù°í Çß¾ú½À´Ï´Ù. ÀÌÁ¦ ±â¾ïÀÌ ³ª½ÃÁö¿ä? ±×·¡¼­ À§Ã³·³ È£ÃâÀ» ÇØ ÁÖ¾ú½À´Ï´Ù.

FILENAME = sc.getInitParameter("FILENAME");

À§ÀÇ ÁÙÀÌ ¹Ù·Î »õ·Î ¸¸µé¾îÁø Ä«¿îÅÍ Å¬·¡½ºÀÇ ÇÙ½ÉÀÔ´Ï´Ù. ¼­ºí¸´ÀÌ È£ÃâµÉ¶§ init ¸Þ¼Òµå°¡ È£ÃâÀÌ µË´Ï´Ù. ±×·¯¸é¼­ parameter·Î ServletConfig¸¦ ¹Þ¾Æ ¿À´Âµ¥¿ä, ±× °´Ã¼¿¡´Â À§¿¡¼­ ¸»Çß´ø servlet.properties¶ó´Â È­ÀÏ¿¡¼­ °¡Áö°í ÀÖ´ø ³»¿ëµéÀ» °¡Áö°í ÀÖ°Ô µË´Ï´Ù. ±×·¡¼­ ÃʱâÈ­¸¦ Çϸ鼭 ¿øÇÏ´Â °ªµéÀ» ºÒ·¯ µéÀÏ ¼ö ÀÖ´Â °ÍÀÌÁö¿ä. ±×¸®°í ±× ºÒ·¯µéÀÌ´Â ¸Þ¼Òµå°¡ getInitParameter(String)¿Í getInitParameterNames(String) À̱¸¿ä, ÀÌ µÎ ¸Þ¼Òµå´Â ServletConfig¶ó´Â Ŭ·¡½ºÀÇ ¸Þ¼ÒµåÀÔ´Ï´Ù. ±×·¡¼­ ¼­ºí¸´¿¡¼­ ÃʱâÈ­ ½Ãų¶§ ºÒ·¯ µé¿©¼­ ±× ÀÌÈÄ·Î °è¼Ó ¾²ÀÌ°Ô µÉ È­ÀÏÀ̶ó¸é, À§Ã³·³ static º¯¼ö·Î ¼±¾ðÀ» ÇÑ ÈÄ¿¡ init ¸Þ¼Òµå¿¡¼­ ºÒ·¯ µé¿©¼­ »ç¿ëÇÏ½Ã¸é ¸Å¿ì Æí¸®ÇÕ´Ï´Ù. ±×·³ init parameter¸¦ ºÒ·¯ ¿À´Â ¹æ¹ýÀº ¾Ë¾Ò´Âµ¥, ¾î¶»°Ô ¼±¾ðÀ» ÇÒ±î¿ä? ¸Å¿ì °£´ÜÇÕ´Ï´Ù. Á¦°¡ À§ÀÇ Å¬·¡½º¸¦ ½ÇÇà½ÃÅ°´Âµ¥ »ç¿ëÇÑ servlet.propertiesÈ­ÀÏÀ» º¸½Ã¸é ±Ý¹æ ¾Æ½Ç°ÍÀÔ´Ï´Ù.

servlet.NEWCOUNTER.code=nc
servlet.NEWCOUNTER.initArgs=FILENAME=counter.dat,MAX_DIGIT=4

ÀÚ, ÀÌÁ¦ ÀÌÇØ ÇÏ½Ç ¼ö ÀÖ½À´Ï±î? ¸Å¿ì °£´ÜÇÏÁö¿ä. ùÁÙÀ» º¸½Ã¸é

servlet.[servlet name].code=[servlet class]

ÀÇ Çü½ÄÀ¸·Î µÇ¾î ÀÖÁö¿ä. ¾Æ½Ã°ÚÁö¿ä?´ÙÀ½ÁÙÀº

servlet.[servlet name].initArgs=[init name]=[init value],[init name]=[init value]

ÀÇ Çü½ÄÀÔ´Ï´Ù. ±×·¯´Ï±î À§ÀÇ ¿¹Á¦ 3 ¼­ºí¸´À» ½ÇÇà½ÃÅ°¸é, FILENAME¿¡ counter.dat°¡ MAX_DIGIT ¿¡´Â 4°¡ µé¾î°¡°Ô µË´Ï´Ù. ±×¸®°í ÀÌ °ªµéÀ» init¸Þ¼Òµå¿¡¼­ ServletConfig object¸¦ »ç¿ëÇؼ­ ¹Þ¾Æ µé¿©¼­ ¼­ºí¸´¿¡¼­´Â »ç¿ëÇÏ¸é µË´Ï´Ù. ±×·³ À§¿¡¼­ ¼­ºí¸´ÀÇ À̸§À» µû·Î Á¤ÇØ ÁÖ¾ú´Âµ¥¿ä, ±× ÀÌÀ¯´Â ¹«¾ùÀϱî¿ä? Á÷Á¢ ÇØ º¸½Ã¸é ¾Ë°ÚÁö¸¸, ¼­ºí¸´ÀÇ À̸§À¸·Î È£ÃâÀ» ÇÏ´ø, Ŭ·¡½º À̸§À¸·Î È£ÃâÀ» ÇÏ´ø ½ÇÇàÀº µË´Ï´Ù. ±×·¯³ª ¸¸ÀÏ Å¬·¡½ºÀÇ À̸§À¸·Î ¼­ºí¸´À» È£ÃâÇÏ¿´À¸¸é, ¼­ºí¸´ Ŭ·¡½ºÀÇ init parameters¸¦ »ç¿ëÇÏÁö ¾Ê´Â´Ù´Â ¶æÀÌ°í, ¼­ºí¸´ÀÇ À̸§À¸·Î È£ÃâÇÏ¿´´Ù¸é, servletÀÇ ÃʱâÈ­ ÆĶó¸ÞÅ͵éÀ» »ç¿ëÇÑ´Ù´Â ¶æÀÔ´Ï´Ù. ¶ÇÇϳª º¸½Ç ÀÏÀÌ ÀÖ½À´Ï´Ù. Àü¿¡ ¼­ºí¸´Àº multi threadingÀ» ±¸ÇöÇÏ¿´´Ù°í ¸»¾¸ µå·È¾ú½À´Ï´Ù. ±×·¡¼­ ¹Ýµå½Ã µ¿±âÈ­¸¦ °í·ÁÇØ ÁÖ¾î¾ß ÇÑ´Ù°í ¸»¾¸ µå·È½À´Ï´Ù. Ä«¿îÅÍ¿¡¼­´Â Ä«¿îÆ®¸¦ ÀÐ¾î µéÀÌ´Â È­ÀÏÀÌ µÎ¹ø ¿­¸®¸é ¾ÈµË´Ï´Ù. ÇÑ È­ÀÏÀ» µÎ°³ÀÇ thread¿¡¼­ µ¿½Ã¿¡ ¿­¸é ¹®Á¦°¡ ¹ß»ýÇϱ⠶§¹®ÀÌÁö¿ä. ±×·¡¼­ getHitCount¶ó´Â ¸Þ¼Òµå´Â synchronized·Î ±¸ÇöÇÑ °ÍÀÔ´Ï´Ù. ÀÌ´Â ¼­ºí¸´À» ÀÛ¼ºÇϽǶ§ ¹Ýµå½Ã À¯³äÇÏ¼Å¾ß ÇÕ´Ï´Ù. ±× ¿Ü¿¡´Â À§ÀÇ ¿¹Á¦ 2¿Í ¸ðµç ¹æ¹ýÀÌ °°À¸´Ï±î, ¿©±â¼­ »õ·Î¿î Ä«¿îÅÍ ¼­ºí¸´Àº ¸¶Ä¡°Ú½À´Ï´Ù.

¾Æ, Âü°í·Î À§ÀÇ ¼­ºí¸´Àº GenericServletÀ» »ó¼Ó¹Þ¾Æ¼­ ServletRequest°´Ã¼¿Í ServletResponse°´Ã¼¸¦ »ç¿ëÇصµ ¹«¹æÇÕ´Ï´Ù. À§ÀÇ Ä«¿îÅÍ ¿¹Á¦´Â HTTP ¿¡¼­ »ç¿ëÇÏ´Â °ÍÀ» ¸¸µé¾úÀ¸¹Ç·Î, ±×³É HttpServlet class¸¦ »ó¼Ó¹Þ¾Ò½À´Ï´Ù.



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