Counter Servlet

 
À̹ø¿¡´Â ¸¸µé¾îº¼ Ŭ·¡½º´Â counterÀÔ´Ï´Ù. ÀÌ Å¬·¡½º¸¦ ½ÇÁ¦·Î »ç¿ëÇÑ ¿¹´Â Á¦ ȨÆäÀÌÁö (hycedu.hanyang.ac.kr:1200)¿¡ ¿À½Ã¸é ù È­¸é¿¡ ÀÖ±¸¿ä, Á¦ ȨÆäÀÌÁö´Â JAVAWEBSERVER1.0.3À» »ç¿ëÇÕ´Ï´Ù. JWS´Â ¼­ºí¸´ SSI¸¦ Á¦°øÇϴµ¥¿ä, <servlet> </servlet> tag¸¦ »ç¿ëÇؼ­ SSI¸¦ ÇÒ ¼ö ÀÖ½À´Ï´Ù. ±×·±µ¥ ´Ù¸¥ À¥¼­¹ö¸¦ »ç¿ëÇϽøé servlet SSI°¡ Á¦°ø µÇÁö ¾Ê±â ¶§¹®¿¡ ½ÇÁ¦·Î Counter¸¦ »ç¿ëÇÏ½Ç ¼ö´Â ¾øÀ» °Í °°³×¿ä. ±×·¡µµ Çѹø ¸¸µé¾î º¸¸é¼­ ¼­ºí¸´À̶õ ÀÌ·¸°Ô ¸¸µå´Â °ÍÀ̱¸³ª, ÇÏ´Â °ÍÀ» ¹è¿ì½Ç ¼ö ÀÖÀ¸¸é ÇÕ´Ï´Ù.

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
 
public class Counter extends HttpServlet {
private PrintWriter toClient;
public void service (HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {
RandomAccessFile fromFile = new RandomAccessFile("counter.dat","rw");
res.setContentType("text/html");
toClient = new PrintWriter(res.getOutputStream(),true);
int now;
String dat;
dat=fromFile.readLine();
now = Integer.parseInt(dat);
now ++;
fromFile.seek(0);
fromFile.writeBytes(now+"\n");
fromFile.close();
int length = dat.length();
String index;
int digit[];
char temp;
digit = new int[6];
 
for (int i=0; i<6-length; i++) {
digit[i]=0;
};
for(int i = 0; i < length; i++) {
temp = dat.charAt(i);
index = temp+"";
digit[i+6-length] = Integer.parseInt(index);
}
toClient.println("<table border=0 cellpadding=0 cellspacing=0><tr>");
for(int i=0; i<6; 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>");
}
}

¿¹Á¦ 2. Ä«¿îÅÍ ¼­ºí¸´
 
¾î¶»½À´Ï±î? ¾ÆÁÖ Âª°í ½±Áö¿ä? Áö±ÝºÎÅÏ ¾ÆÁÖ °£´ÜÈ÷ À§ÀÇ Counter ¸¦ ¼³¸íµå¸®°Ú½À´Ï´Ù. ³Ê¹«³ª ½¬¿î Ŭ·¡½º ÀÌ´Ï±î ¸Å¿ì ½±°Ô ÀÌÇØÇÏ½Ç ¼ö ÀÖÀ¸¸®¶ó »ý°¢ÇÕ´Ï´Ù. ÀÌ Å¬·¡½º´Â service¸Þ¼Òµå¸¦ overridingÇß½À´Ï´Ù. GETÀ̳ª POST¸Þ¼ÒµåÀÇ ³Ñ¾î¿Â ÀÚ·áÀÇ Ã³¸®°¡ ¾Æ´Ï¶ó, ±×³É ÀϹæÀûÀ¸·Î ¿äûÀÌ ¿À¸é Ãâ·Â¸¸ ÇÏ¸é µÇ±â¿¡, service(HttpServletRequest, HttpServletResponse) ¸Þ¼Òµå¸¦ »ç¿ëÇÑ °ÍÀÔ´Ï´Ù. À̶§´Â doPost ¸Þ¼Òµå³ª, doGet¸Þ¼Òµå´Â ¾Æ¹« Àϵµ ÇÏÁö ¾Ê°ÚÁö¿ä. ±× ¸Þ¼Òµå¸¦ È£Ãâ ÇÏÁö ¾ÊÀ¸´Ï±î¿ä. ¹°·Ð µÎ ¸Þ¼Òµå¸¦ È£ÃâÇÑ´Ù°í ÇÏ¿©µµ, abstract¸Þ¼ÒµåÀ̴ϱî, ¾Æ¹« Àϵµ ÇÏÁö ¾Ê½À´Ï´Ù. ±×¸®°í ³ª¼­ counter.dat¶ó´Â È­ÀÏÀ» RandomAccessFile class¸¦ »ç¿ëÇؼ­ openÇÕ´Ï´Ù. ÇöÀç±îÁö ÀúÀåµÇ¾î ÀÖ´Â hit¼ö¸¦ ¾Ë¾Æ º¸±â À§Çؼ­ ÀÔ´Ï´Ù. ±×¸®°í À§ÀÇ Ã¹ ¿¹Á¦¿¡¼­ º¸¾ÒµíÀÌ

res.setContentType("text/html");

·Î Ãâ·ÂÇÏ´Â mime typeÀ» ¼³Á¤ÇÕ´Ï´Ù. Ãâ·ÂÀÌ ¼³Á¤µÈ ÈÄ¿¡´Â ½ÇÁ¦·Î hit¼ö¸¦ ÀÐ¾î ¿É´Ï´Ù. Àоî¿Â °ªÀÌ StringÇüÀ̴ϱî, ±×°É intÇüÀ¸·Î ¹Ù²Ù¾î ÁÝ´Ï´Ù.

now = Integer.parseInt(dat);

±×¸®°í ±×°É ´Ù½Ã counter.dat¶ó´Â È­ÀÏ¿¡ ÀúÀåÀ» ÇÕ´Ï´Ù. RandomAccessFile ¿¡¼­ ùÁÙÀ» Àоî¿ÔÀ¸¹Ç·Î, ´ÙÀ½ÁÙÀ» pointer°¡ ±×¸®Å°°Ô µË´Ï´Ù. ÀÌ »óÅ¿¡¼­ ¹Ù·Î Ãâ·ÂÀ» Çϸé, µÎ¹ø° ÁÙ¿¡ Ãâ·ÂÀÌ µÇ°ÔµË´Ï´Ù. ±×·¡¼­

fout.seek(0);

¶ó´Â ¸Þ¼Òµå¸¦ »ç¿ëÇؼ­ pointer¸¦ È­ÀÏÀÇ °¡Àå óÀ½À¸·Î ºÒ·¯ ¿É´Ï´Ù. ±×ÈÄ¿¡ 1À» Áõ°¡½ÃŲ °ªÀ» È­ÀÏ¿¡ ÀúÀåÇÏ°í, close½Ãŵ´Ï´Ù. ±× ÈÄ¿¡´Â Àоî¿Â °ªÀ» °¢°¢ htmlÀÇ img tag¸¦ »ç¿ëÇؼ­ Ãâ·ÂÀ» ÇÏ¸é µË´Ï´Ù.




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