¹æ¸í·Ï ÀÛ¼ºÇϱâ #1

 

  À̹ø °­Á´ ¹æ¸í·Ï ÀÛ¼º¿¡ °üÇÑ ³»¿ëÀÔ´Ï´Ù. ½ÇÁ¦·Î ÀÛ¼ºÀ» Çϱâ Àü¿¡ ¹æ¸í·ÏÀ» Çѹø OverviewÇÏ´Â °ÍÀÌ ÁÁÀ» °Í °°½À´Ï´Ù. ÀÌ°÷À» Ŭ¸¯ÇÏ½Ã¸é ½ÇÁ¦·Î »ç¿ëµÇ°í ÀÖ´Â ¹æ¸í·ÏÀ» º¸½Ç ¼ö ÀÖ½À´Ï´Ù. °¡¼­ º¸½Å ±è¿¡ ÇÑ ¸»¾¸ ²À ÇØÁÖ°í ¿À¼¼¿ä...*^^* ¹æ¸í·ÏÀº º¸³½»ç¶÷ÀÇ À̸§°ú, E-mail Address, º¸³½»ç¶÷ÀÇ Homepage URLÀ» ÀûÀ» ¼ö ÀÖ°í, ½ÇÁ¦ ³»¿ëÀ» ÀûÀ» ¼ö ÀÖ°Ô µÇ¾î ÀÖ½À´Ï´Ù. ½ÇÁ¦·Î ±×·¸°Ô µÇ¾î ÀÖÁö¿ä?

  ±× ¹æ¸í·ÏÀº BookWriter¶ó´Â Ŭ·¡½º¿Í BookLister¶ó´Â µÎ °³ÀÇ Å¬·¡½º·Î ÀÌ·ç¾î Áý´Ï´Ù. ±×¸®°í ÇϳªÀÇ HTML¹®¼­°¡ ÇÊ¿äÇÕ´Ï´Ù. BookWriter¶ó´Â Ŭ·¡½º´Â ¹æ¹®ÀÚ°¡ ¾´ ±ÛÀ» ¹Þ¾Æ µé¿©¼­ ¼­¹öÃø¿¡ ÀúÀåÇÏ´Â ÀÏÀ» Çϴ Ŭ·¡½ºÀÌ°í, BookListerŬ·¡½º´Â ¼­¹ö¿¡ ÀúÀåµÇ¾î ÀÖ´Â ³»¿ëÀ» Ŭ¶óÀ̾ðÆ®·Î »Ñ·ÁÁÖ´Â ¿ªÇÒÀ» ÇÕ´Ï´Ù. ±×¸®°í HTML¹®¼­´Â »ç¿ëÀÚ°¡ ÀÛ¼ºÇÒ ³»¿ëÀ» ¹Þ¾ÆµéÀÏ FORM¹®À¸·Î µÇ¾î ÀÖ½À´Ï´Ù.

±×·³ ¿ì¼± BookWriter¶ó´Â Ŭ·¡½º¸¦ ºÐ¼®ÇØ º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.

 

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
  
  public class BookWriter extends HttpServlet {
  
  public void doPost (HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException {
  
  private PrintWriter writer;
  String name,email,url,today,content;
  String tt=null,line;
  Date Today = new Date();
  String filename,dir;
  File bbs;
  int next,num;
  dir = req.getParameterValues("dir")[0];
  filename = req.getParameterValues("filename")[0];
  name = req.getParameterValues("name")[0];
  email = req.getParameterValues("email")[0];
  url = req.getParameterValues("url")[0];
  content = req.getParameterValues("content")[0];
  today = Today.toString();
  
  bbs = new File(dir);
  if(bbs.exists()) {
    if(bbs.isDirectory()) {
      RandomAccessFile fout = new RandomAccessFile(dir+"/"+filename+".dat","rw");
      fout.seek(fout.length());
      fout.writeBytes("<center><hr width=300 border=1></center><br><BR>");
      fout.writeBytes("name : " +name+"<br>\n");
      fout.writeBytes("E-mail : <a href=mailto:"+email+">"+email+"</a><br>\n");
      fout.writeBytes("HomePage : <a href="+url+">"+url+"</a><br>\n");
      fout.writeBytes("Date : "+today+"<br>\n");
      StringTokenizer st = new StringTokenizer(content,"\n");
      while(st.hasMoreTokens()) {
        fout.writeBytes("<br>"+st.nextToken());
      }
    fout.writeBytes("<BR><Br>\n");
    fout.close();
    } else {
      System.out.println("Error : bbs is not a directory");
      System.exit(1);
    }
  } else {
    bbs.mkdir();
    RandomAccessFile fout = new RandomAccessFile(dir+"/"+filename+".dat","rw");
    fout.writeBytes("Name : " +name+"<br>\n");
    fout.writeBytes("E-mail : <a href="+email+">"+email+"</a><br>\n");
    fout.writeBytes("HomePage : <a href=http://"+url+">http://"+url+"</a><br>\n");
    fout.writeBytes("Date : "+today+"<br>\n");
    StringTokenizer st = new StringTokenizer(content[0],"\n");
    while(st.hasMoreTokens()) {
    fout.writeBytes("<br>"+st.nextToken());
  }
  fout.writeBytes("<BR><br>\n");
  fout.close();
  }
  res.setContentType("text/html");
  writer = new PrintWriter(res.getOutputStream(),true);
  writer.println("<html><title>BBS for bluehope</title>");
  writer.println("<body bgcolor=#ffffff><font size=2 face=helvetica><br><img src=http://contest.hanyang.ac.kr:8080/images/guestbook.jpg border=0><hr><br><hr><center>");
  writer.println("<BR><BR><BR>¹æ¸í·Ï ÀÛ¼ºÀÌ ¼º°øÀûÀ¸·Î ³¡³µ½À´Ï´Ù.");
  writer.println("<br><BR><BR><a href=http://contest.hanyang.ac.kr:8080/servlet/BookLister.class?dir=" + dir + "&filename=" + filename + "> ¹æ¸í·Ï º¸±â</a>");
  }
  writer.println("</center></body></html>");
  }
  public void error_handling(Exception e, String msg) {
    System.err.println(e+msg);
  }
}

 

 ¾ÆÁÖ ½±°í ´Ü¼øÇÏÁö¿ä? °£´ÜÈ÷ »ìÆì º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.

BookWriter Class´Â ¿¹»ó ÇϽŴë·Î HttpServlet Ŭ·¡½º¸¦ »ó¼Ó ¹ÞÀº ¼­ºí¸´ÀÔ´Ï´Ù. ±×¸®°í PostÇüÀ¸·Î º¸³»Áö´Â ÀڷḦ ó¸®Çϱâ À§Çؼ­ doPost ¸Þ¼Òµå¸¦ ¿À¹ö¶óÀ̵ùÇÕ´Ï´Ù.ÀÌÀüÀÇ °­Á¿¡¼­ Post¿Í Get¹æ½ÄÀÇ Â÷ÀÌ´Â ¼³¸íµå·ÈÀ¸´Ï, ¿Ö Post¸¦ ¾²´Â °ÍÀÌ ´õ ÁÁÀºÁö´Â ¾Æ½Ã¸®¶ó ¹ÏÁö¸¸, »çÁ·ÀÌ µÉÁö¶óµµ Ȥ½Ã ¸ð¸£½Ã´Â ºÐµéÀ» À§ÇØ, PostÇü½ÄÀº ¸¹Àº ¾çÀÇ ÀڷḦ ¹Þ¾Æ µéÀÏ ¼ö Àֱ⠶§¹®¿¡, ±×¸®°í URLµÚ¿¡ ÁöÀúºÐÇÏ°Ô µþ·Á °¡Áö ¾Ê°Ô ¶§¹®¿¡ Post¸¦ »ç¿ëÇϱâ·Î ÇÕ´Ï´Ù. ¹°·Ð GetÀ» »ç¿ëÇϼŵµ µÇ±â´Â µË´Ï´Ù. --;

Á¿찣 ÀÌÀüÀÇ ¼­ºí¸´µéó·³ PrintWriter°´Ã¼¸¦ ¼±¾ðÇÕ´Ï´Ù. ±×¸®°í name, email, url, content °´Ã¼¸¦ StringÇüÀ¸·Î ¼±¾ð ÇÕ´Ï´Ù. À§ÀÇ °´Ã¼µéÀº °¢°¢ ¹æ¸í·ÏÀ» ÀÛ¼ºÇÏ´Â »ç¶÷ÀÇ À̸§°ú, ÀüÀÚ¸ÞÀÏ ÁÖ¼Ò, ȨÆäÀÌÁö URLÀ» ÀǹÌÇÕ´Ï´Ù. ±×¸®°í content´Â ½ÇÁ¦·Î ÀÛ¼ºÇÏ´Â ±ÛÀ» ÀúÀåÇÏ´Â °´Ã¼°¡ µË´Ï´Ù.

»ç¿ëÀÚ°¡ ¹æ¸í·ÏÀ» ÀÛ¼ºÇϸé, ³Ñ¾î¿À´Â °ªÀº name, email, url, content, dir, filenameÀÔ´Ï´Ù. ÀÌÁß dir°ú filename¸¸ »© ³õÀ¸¸é À§ÀÇ º¯¼öµé°ú °°Àº °ªÀ» °°°Ô µË´Ï´Ù. dirÀº ÆÄÀÏÀÌ ÀúÀåµÉ µð·ºÅ丮¸¦ ³ªÅ¸³»¸ç, filenameÀº ½ÇÁ¦ ÀúÀåµÉ file¸íÀ» ³ªÅ¸³À´Ï´Ù. ±×·¸°Ô Çϸé, ¸¹Àº ¹æ¸í·ÏÀ» ÇϳªÀÇ ¼­ºí¸´À¸·Î ó¸®ÇÒ ¼ö ÀÖ°ÚÁö¿ä? ¸¸ÀÏ È¥ÀÚ »ç¿ëÇÒ °ÍÀ̶ó¸é, ±×³É º¯¼ö¿¡ ³Ö¾î ³õ±¸¼± »ç¿ëÇÏ¸é µÇ°ÚÁö¿ä. Á¿찣 ±×·¸°Ô ÇÑ ÈÄ¿¡, req°´Ã¼·ÎºÎÅÍ ¸ðµç °ªµéÀ» ¹Þ¾Æ µéÀÔ´Ï´Ù.

  today = Today.toString();

À§ÀÇ ¶óÀÎÀº today¶ó´Â °´Ã¼¸¦ »ç¿ëÇÏ¿© °Ô½Ã¹°ÀÌ ÀÛ¼ºµÈ ³¯ÀÚ¿Í ½Ã°¢µîÀ» ±â·ÏÇÕ´Ï´Ù. today.toString()¸Þ¼Òµå´Â Date Ŭ·¡½º¿¡¼­ StringÇüÀ¸·Î º¯È¯½ÃÄÑ ÁÖ´Â ¸Þ¼ÒµåÀÔ´Ï´Ù. ´Ù¸¥ Ŭ·¡½º¿¡¼­µµ ¿ª½Ã toString()À̶ó´Â ¸Þ¼Òµå°¡ ÀÖÀ¸¸é StringÀ¸·Î ¹Ù²Ù¾î ÁÖ´Â °ÍÀ̶ó°í »ý°¢ÇÏ½Ã¸é µË´Ï´Ù.

  bbs = new File(dir);   if(bbs.exists())

File Ŭ·¡½º¸¦ »ç¿ëÇÕ´Ï´Ù. File Ŭ·¡½º´Â ÆÄÀÏÀÇ »ý¼ºÀ̳ª »èÁ¦, Á¸ÀçÀ¯¹«µîÀ» È®ÀÎÇÒ ¼ö Àִ Ŭ·¡½ºÀÔ´Ï´Ù. ÀÚ¼¼ÇÑ File classÀÇ »ç¿ëÀº dirÀ̶ó´Â directory°¡ ÀÖ´ÂÁö È®ÀÎÇØ º¾´Ï´Ù. dir·Î '/home/user01/htdocs/book' À̶ó´Â °ªÀÌ ³Ñ¾î¿Â´Ù¸é, ½ÇÁ¦·Î ¼­¹öÃø¿¡¼­ 'home/user01/htdocs/book'À̶ó´Â µð·ºÅ丮ÀÇ Á¸Àç À¯¹«¸¦ »ìÆì¼­ ÀÖÀ¸¸é, ±× µð·ºÅ丮 ³»¿¡¼­ ÀÛ¾÷À» ÇÏ°í, ¾øÀ¸¸é »õ·Î ¸¸µì´Ï´Ù. ÇÏÁö¸¸ Àý´ë °æ·Î º¸´Ù´Â »ó´ë °æ·Î¸¦ »ç¿ëÇÏ´Â °ÍÀÌ ´õ ÁÁÀ» °Í °°½À´Ï´Ù. °£ÆíÇϴϱî¿ä....*^^*

      RandomAccessFile fout = new RandomAccessFile(dir+"/"+filename+".dat","rw");

À§ÀÇ ¹®ÀåÀº RandomAccessFile·Î ±âÁ¸ÀÇ ³»¿ëÀÌ ÀúÀåµÇ¾î ÀÖ´Â fileÀ» RW»óÅ·ΠOpen ÇÑ´Ù´Â ÀǹÌÀÔ´Ï´Ù. ¹æ¸í·ÏÀº ±âÁ¸ÀÇ fileÀ» Àбâ/¾²±â ¸ðµå·Î ¿­¾î¼­ À̹øÀÇ ³»¿ëÀ» AppendÇÏ¸é µÇ´Ï±î¿ä. Append´Â µÚ¿¡ µ¡ºÙÀδٴ Àǹ̶ó´Â °Å ´Ù ¾Æ½ÃÁÒ? ÀÚ ±×·¯¸é ¾î¶»°Ô AppendÇÒ±î¿ä? ÆÄÀÏÀÇ ¸Ç ¸¶Áö¸·À» ã¾Æ¼­ ±× µÚ¿¡´Ù°¡ ´õ ¾²¸é µÇ°ÚÁö¿ä? ±× ¹®Àå¿¡ ¹Ù·Î ¾Æ·¡¿¡ ÀÖ´Â ¹®ÀåÀÔ´Ï´Ù.

      fout.seek(fout.length());

 RandomAccessFile Àº ±× class nameÀÌ ¸»ÇØ ÁÖµíÀÌ ¾Æ¹« À§Ä¡³ª ã¾Æ¼­ ÀÐ°í ¾²±â°¡ °¡´ÉÇÏ´Ù´Â ¶æÀÔ´Ï´Ù. ±×·¡¼­ ÆÄÀÏÀ» AppendÇÒ À§Ä¡¸¦ ã¾Æ °©´Ï´Ù. ±× À§Ä¡°¡ ¾Æ±î Append¸¦ ÇÒ °ÍÀ̶ó°í ÇßÀ¸´Ï ÆÄÀÏÀÇ ¸Ç ³¡ÀÌ°ÚÁö¿ä. fout.seek(int )¶ó´Â ¸Þ¼Òµå´Â ±× À§Ä¡¸¦ ã¾Æ °¡´Â ¸Þ¼ÒµåÀÔ´Ï´Ù. int °ªÀÎ paramÀº byteÀÇ ¼ö¸¦ ³ªÅ¸³À´Ï´Ù. ±×¸®°í length()¶ó´Â ¸Þ¼Òµå´Â ÆÄÀÏÀÇ Å©±â¸¦ ³ªÅ¸³À´Ï´Ù. ±×·¯´Ï±î ÆÄÀÏÀÇ ¸Ç ¸¶Áö¸·À» ãÀ¸·Á¸é fout.length()¶ó´Â ¸Þ¼Òµå¸¦ ½ÇÇà½ÃÄÑ¸é ¾Ë ¼ö ÀÖ°ÚÁö¿ä?

     fout.writeBytes("<center><hr width=300 border=1></center><br><BR>");

fout.writeBytes()¶ó´Â ¸Þ¼Òµå´Â ½ÇÁ¦·Î ÆÄÀÏÀ» ¾²´Â ¸Þ¼ÒµåÀÔ´Ï´Ù. ¸Þ¼ÒµåÀÇ ÀÎÀÚ·Î ³Ñ¾î°¡´Â StringÀ» ½ÇÁ¦·Î ÆÄÀÏ¿¡ ½á ÁÝ´Ï´Ù. ±×¿Ü¿¡ ´Ù¸¥ ¿©·¯ °¡Áö Write¸Þ¼Òµå µéÀÌ ÀÖ½À´Ï´Ù. À̵éÀº RandomAccessFileŬ·¡½º¸¦ ÂüÁ¶ÇØ º¸¼¼¿ä. ÇÏ¿©°£ ÀÌ°÷¿¡¼­´Â StringÀ» ¾²´Ï±î, writeBytes¶ó´Â ¸Þ¼Òµå¸¦ »ç¿ëÇÕ´Ï´Ù.

      StringTokenizer st = new StringTokenizer(content,"\n");
      while(st.hasMoreTokens()) {
        fout.writeBytes("<br>"+st.nextToken());
      }

À§ÀÇ ¹®ÀåÀÌ ¹«¾ùÀÎÁö ¾Æ½Ã°Ú½À´Ï±î? HTML·Î º¸¿©ÁÙ ¶§¿¡´Â °³Çà ¹®ÀÚ°¡ ¾Æ¹« Àǹ̰¡ ¾ø½À´Ï´Ù. ¾Æ¹«¸® Enter¸¦ ¸¹ÀÌ ÃÄÁ־ ÁÙÀÌ ¹Ù²îÁö ¾Ê´Â °ÍÀ» Àß ¾Æ½ÃÁö¿ä? ±×·¡¼­ »ç¿ëÀÚ°¡ Enter¸¦ Ä£ À§Ä¡¿¡ '<BR>' ű׸¦ ³Ö¾î ÁÖ´Â °ÍÀÔ´Ï´Ù. ±×·¯±â À§Çؼ­´Â '\n'À» ã¾Æ ±× À§Ä¡¸¶´Ù '<BR>'À» ÀÔ·ÂÇØ¾ß ÇÏÁö¿ä. ASCII°ªÀ¸·Î enter°¡ ´­¸° À§Ä¡´Â '\n'ÀÌ µé¾î °¡±â ¶§¹®ÀÔ´Ï´Ù. ±×·¡¼­ StringTokenizer¶ó´Â Ŭ·¡½º¸¦ »ç¿ëÇÕ´Ï´Ù. StringTokenizer¶ó´Â Ŭ·¡½º´Â °£´ÜÈ÷ ¸»Çؼ­ ¿øÇÏ´Â char°¡ ÀÖ´Â À§Ä¡±îÁö·Î StringÀ» ³ª´©¾î ÁÖ´Â ¿ªÇÒÀ» ÇÕ´Ï´Ù. Áï, TokenÀ» »ç¿ëÇÏ¿© ±× TokenÀÌ ³ª¿Ã ¶§¸¶´Ù StreamÀ» ¸ØÃß°Ô µË´Ï´Ù. while¹®À» »ç¿ëÇÏ¿© ´õÀÌ»óÀÇ TokenÀÌ ¾øÀ» ¶§±îÁö ±×·± ÀÏÀ» ¹Ýº¹ÇØ ÁÝ´Ï´Ù.

  fout.close();

±×¸®°í File ¾²±â°¡ ³¡³ª¸é ¿­¾ú´ø fileÀ» ´Ý½À´Ï´Ù.

    bbs.mkdir()

¸¸ÀÏ dirÀ̶ó´Â µð·ºÅ丮°¡ ¾ø´Â °æ¿ì¿¡´Â »õ·Î directory¸¦ ¸¸µé¾î ÁÝ´Ï´Ù. ±×¸®°í À§¿Í °°Àº ÀÏÀ» °°ÀÌ ÇÏ¸é µË´Ï´Ù. ¾Æ, »ý°¢ÇØ º¸´Ï ¾Æ·¡ ¹®ÀåµéÀº ¼³¸íÀ» ¾Èµå·È³×¿ä....--;

    } else {
      System.out.println("Error : bbs is not a directory");
      System.exit(1);
    }

¹«½¼ ¹®ÀåÀÎÁö ¾Æ½Ã°ÚÁö¿ä? ÇÁ·Î±×·¥ÀÇ È帧À» ÀÌÇØ ¸øÇÑ´Ù Çϼŵµ ÇÁ¸°Æ® ÇÏ´Â ¸Þ½ÃÁö¸¦ º¸½Ã¸é ¾Æ½Ã°ÚÁö¿ä? dir·Î µé¾î¿Â À§Ä¡°¡ µð·ºÅ丮¿©¾ß Çϴµ¥, ÀÏ¹Ý ÆÄÀÏ·Î Á¸ÀçÇÏ¿©¼­ »ç¿ëÇÒ ¼ö°¡ ¾ø´Ù°í ÇÏ´Â ³»¿ëÀÔ´Ï´Ù.

±× ¿ÜÀÇ ¹®ÀåµéÀº ¼³¸í ¾Èµå·Áµµ µÇÁö¿ä? ContentTypeÀ» ¼³Á¤ÇÏ°í °£´ÜÇÑ ¸Þ½ÃÁö¸¦ Ãâ·ÂÇÏ´Â ºÎºÐÀÔ´Ï´Ù. ÀÌ·¸°Ô Çؼ­ °Ô½ÃÆÇ ¾²±â ±â´ÉÀº ´Ù ¼³¸íµÇ¾ú³×¿ä.....¼³¸íÀÌ ³Ê¹« ºÎÁ·Çß³ª¿ä?