IBM Korea Skip to main content
       IBM Ȩ    |  Á¦Ç° & ¼­ºñ½º  |  °í°´Áö¿ø & ´Ù¿î·Îµå  |  È¸¿ø°¡ÀÔ  

IBM developerWorks > ÀÚ¹Ù
developerWorks

JSP best practices : JSP ű׿ë Ä¿½ºÅÒ ¾ÖÆ®¸®ºäÆ® ¸¸µé±â
ÅÂ±× ±â´ÉÀ» »ç¿ëÀÚ Á¤ÀÇ ¾ÖÆ®¸®ºäÆ®·Î È®ÀåÇϱâ

Level: Introductory

Brett McLaughlin
Author, O'Reilly and Associates
2003³â 8¿ù 5ÀÏ

Column iconÄ¿½ºÅÒ Å¸ÀÓ-½ºÅÆÇÁ¸¦ È®ÀåÇÏ¿© ÆäÀÌÁö ÀÛ¼ºÀÚ°¡ ÀÚ½ÅÀÇ Å¸ÀÓ-½ºÅÆÇÁ Æ÷¸ËÀ» ¼±ÅÃÇÒ ¼ö ÀÖµµ·Ï ÇÏ´Â ¹æ¹ýÀ» ¼³¸íÇÑ´Ù.

Áö³­ ½Ã°£¿¡´Â JSP ÆäÀÌÁö¿¡¼­ Ä¿½ºÅÒ ÅÂ±× ¶óÀ̺귯¸®ÀÇ ±âº»ÀûÀÎ »ç¿ë ¹æ¹ýÀ» ¼³¸íÇß´Ù. °£´ÜÇÑ Å±׸¦ Á¤ÀÇÇÏ°í ÅÂ±× ¶óÀ̺귯¸® µð½ºÅ©¸³ÅÍ(TLD)¸¦ ÅëÇØ ´Ù¸¥ JSP ÀÛ¼ºÀÚ±îÁö »ç¿ë ÇÒ ¼ö ÀÖµµ·Ï ¸¸µé¾ú´Ù. À̹ø ÁÖ¿¡´Â Ä¿½ºÅÒ Å±׿¡ ´ëÇØ ¾Ë°í ÀÖ´Â Áö½Ä¿¡ ±â¹ÝÇÏ¿© ±¸ÇöÀ» ÇÒ °ÍÀÌ´Ù. ¿©±â¿¡¼­ »ç¿ëÇÒ ¿¹Á¦ ű״ ¸Å¿ì °£´ÜÇÏ´Ù. µû¶ó¼­ Ä¿½ºÅÒ ¾ÖÆ®¸®ºäÆ®¸¦ °áÇÕÇÏ¿© ±â´ÉÀ» È®ÀåÇÒ °ÍÀÌ´Ù.

¿¹Á¦¿¡ ´ëÇÏ¿©: ÀÌ ±Û¿¡ »ç¿ëµÈ ¸ðµç ¿¹Á¦ ÄÚµå´Â Áö³­ ¹ø ¿ì¸®°¡ °³¹ßÇÑ lastModified ű׿¡ ±â¹ÝÇÏ¿© ±¸ÇöµÈ´Ù.

"Hello, world" Ä¿½ºÅ͸¶ÀÌ¡
JSP ű×ÀÇ °¡Àå ÀϹÝÀûÀÎ ¿ä±¸»çÇ×Àº ÆäÀÌÁö·ÎºÎÅÍ ¿À´Â µ¥ÀÌÅ͸¦ ¹Þ¾ÆµéÀÌ°í ±× µ¥ÀÌÅÍ¿¡ ÀÀ´äÇÒ ¼ö ÀÖ¾î¾ß ÇÑ´Ù´Â °ÍÀÌ´Ù. ÅÂ±× ¾ÖÆ®¸®ºäÆ®·Î ÀÌ ±â´ÉÀ» ¿ì¸®ÀÇ Ä¿½ºÅÒ Å±׿¡ °áÇÕÇÒ ¼ö ÀÖ´Ù.

°£´ÜÇÑ ¿¹Á¦·Î "Hello, world" ¾ÖÇø®ÄÉÀ̼ÇÀ» µé¾îº¸°Ú´Ù. ÀÌ ½ºÅ©¸³Æ²¸´ÀÇ ±â´ÉÀ» ±¸ÇöÇÏ´Â Ä¿½ºÅÒ Å±׸¦ »ó»óÇÏ´Â °ÍÀº ½±Áö¸¸ À̸¦ ¾î¶»°Ô È®ÀåÇÒ °ÍÀΰ¡?

Listing 1Àº ÀüÇüÀûÀÎ "Hello, world!"¸¦ °áÇÕÇÑ JSP ÆäÀÌÁöÀÌ´Ù. nameÀ̶ó°í ÇÏ´Â ¾ÖÆ®¸®ºäÆ®¸¦ Æ÷ÇÔÇÏ°í Àִ ű×ÀÌ´Ù:

Listing 1. "Hello, world!" ű×

<p>
    <examples:hello name="Reader" />
</p>

name ¾ÖÆ®¸®ºäÆ®´Â ÆäÀÌÁö ÀÛ¼ºÀÚ°¡ hello ű׿¡ µ¥ÀÌÅ͸¦ °ø±ÞÇÒ ¼ö ÀÖ´Â °÷¿¡ °ø°£À» ¸¸µç´Ù. ÀÌ °æ¿ì »ç¶÷ÀÇ À̸§Àº ¾ÖÇø®ÄÉÀ̼ÇÀÌ º¸³»´Â ¸Þ½ÃÁö¸¦ ¹ÞÀ» »ç¶÷ÀÌ´Ù. Listing 2´Â hello ű׸¦ ±¸ÇöÇÏ´Â ÀÚ¹Ù ÄÚµåÀÌ´Ù:

Listing 2. hello ű׿ë ÄÚµå

package com.ibm.examples;

import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;

public class HelloTag extends TagSupport {

    // The "person" to say hello to
    private String name;

    // Accept the attribute data
    public void setName(String name) {
      this.name = name;
    }

    public int doEndTag() {
      try {
	  StringBuffer message = new StringBuffer("Hello, ");
	  message.append(name)
	         .append("!");
	  pageContext.getOut().println(message.toString());
	} catch (IOException ignored) { }
	return EVAL_PAGE;
    }

Listing 2ÀÇ ÄÚµå´Â ¸Å¿ì ´Ü¼øÇÏ´Ù. setXXX() ¸Þ¼Òµå¸¦ Ãß°¡Çß´Ù. XXX¿¡´Â ¾ÖÆ®¸®ºäÆ® À̸§ÀÌ µé¾î°£´Ù. º¸±â¿¡´Â °£´ÜÇÏÁö¸¸ ű×ÀÇ ±â´ÉÀ» Å©°Ô È®ÀåÇÑ °ÍÀÌ´Ù. ÆäÀÌÁö ÀÛ¼ºÀڴ ƯÁ¤ ¸ñÀûÀ» À§ÇØ Ä¿½ºÅÒ µ¥ÀÌÅ͸¦ ¼³Á¤ÇÒ ¼ö ÀÖ°í ±× µ¥ÀÌÅÍ´Â ÀúÀå, Á¶ÀÛ, È°¼ºÈ­ µÉ ¼ö ÀÖ´Ù. doEndTag() ¸Þ¼Òµå·Î´Â ÇÊ¿äÇÑ ¾î¶² ¹æ½ÄÀ¸·Îµç ÅÂ±× µ¥ÀÌÅ͸¦ »ç¿ëÇÒ ¼ö ÀÖµµ·Ï ÇÑ´Ù.

lastModified ű׿¡ ¾ÖÆ®¸®ºäÆ®¸¦ Ãß°¡ÇÒ ¶§ ¾î¶»°Ô µÇ´ÂÁö °ËÅäÇغ¸ÀÚ.

lastModified È®ÀåÇϱâ
ÆäÀÌÁö ÀÛ¼ºÀÚ¿¡°Ô ´ÜÁö ÇϳªÀÇ µð½ºÇ÷¹ÀÌ ¿É¼ÇÀ» ÁÖ´Â ´ë½Å ¿øÇÏ´Â ´ë·Î ¾Æ¿ôDz Æ÷¸ËÆÃÀ» ¼³Á¤ÇÒ ¼ö ÀÖµµ·Ï ÇÏ°í ½Í´Ù. lastModifiedTag Ŭ·¡½º°¡ ¾Æ¿ôDz Æ÷¸ËÆÃÀ» À§ÇØ java.text.SimpleDateFormatÀ» Æ÷ÇÔÇϵµ·Ï È®Àå½ÃÄѼ­ ¹é¿£µå¿¡¼­ ½ÃÀÛÇÏ°Ú´Ù. (Listing 3):

Listing 3. LastModifiedTag Ŭ·¡½º¿¡¼­ SimpleDateFormat »ç¿ëÇϱâ

package com.newInstance.site.tags;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.tagext.TagSupport;

public class LastModifiedTag extends TagSupport {

    private String format = "MMM d, yyyy";

    public int doEndTag() {
      try {
        HttpServletRequest request =
          (HttpServletRequest)pageContext.getRequest();
        String path = pageContext.getServletContext().getRealPath(
          request.getServletPath());
        File file = new File(path);

        DateFormat formatter = new SimpleDateFormat(format);

        pageContext.getOut().println(
          formatter.format(new Date(file.lastModified())));
      } catch (IOException ignored) { }
      return EVAL_PAGE;
    }
}

Listing 4¿¡¼­´Â »õ·Î¿î Æ÷¸ËÆà ±â´ÉÀ» ű׷Π°¡Á®¿Ô´Ù. format ¾ÖÆ®¸®ºäÆ® °ªÀº Listing 3¿¡ Àû¿ëµÈ »õ·Î¿î format ¸Þ¼Òµå º¯¼ö¿¡ ¾îÅÂÄ¡µÈ´Ù.

Listing 4. »õ·Î¿î ¾ÖÆ®¸®ºäÆ® Çڵ鸵Çϱâ

package com.newInstance.site.tags;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.tagext.TagSupport;

public class LastModifiedTag extends TagSupport {

    private String format = "MMM d, yyyy";

    public void setFormat(String format) {
      this.format = format;
    }

    public int doEndTag() {
      try {
        HttpServletRequest request = 
          (HttpServletRequest)pageContext.getRequest();
        String path = pageContext.getServletContext().getRealPath(
          request.getServletPath());
        File file = new File(path);

        DateFormat formatter = new SimpleDateFormat(format);

        pageContext.getOut().println(
          formatter.format(new Date(file.lastModified())));
      } catch (IOException ignored) { }
      return EVAL_PAGE;
    }
}

±¸Çö »ó¼¼
format ¾ÖÆ®¸®ºäÆ®·Î ÆäÀÌÁö ÀÛ¼ºÀÚ´Â ³¯Â¥/½Ã°£ ¾Æ¿ôDz Æ÷¸ËÀ» ¿øÇÏ´Â ´ë·Î ¼³Á¤ÇÒ ¼ö ÀÖ´Ù. ÇÏÁö¸¸ ÀÌ »õ·Î¿î ¾ÖÆ®¸®ºäÆ®¸¦ »ç¿ëÇϱâ Àü¿¡ TLD ÆÄÀÏÀ» ¾à°£ º¯°æÇؾßÇÑ´Ù. (Listing 5):

Listing 5. TLD ¼öÁ¤Çϱâ

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib
    PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2/EN"
           "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >

<taglib>
    <tlib-version>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>site-utils</short-name>
    <uri>http://www.newInstance.com/taglibs/site-utils</uri>

    <tag>
      <name>lastModified</name>
      <tag-class>com.newInstance.site.tags.LastModifiedTag</tag-class>
      <body-content>empty</body-content>
      <attribute>
        <name>format</name>
      </attribute>
    </tag>
</taglib>

µðÆúÆ® °ª
TLD¸¦ ¾÷µ¥ÀÌÆ® ÇÒ ¶§, »õ·Î¿î ¾ÖÆ®¸®ºäÆ®¸¦ »ç¿ëÇؾ߸¸ ÇÑ´Ù. Å×½ºÆ®µµ ÇʼöÀÌ´Ù. ¼­ºí¸´ ÄÁÅ×À̳ʸ¦ Àç½ÃÀÛÇÏ¿© ÀÌ°ÍÀÌ »õ·Î¿î ÅÂ±×¿Í TLD º¯È­¸¦ °¨ÁöÇß´ÂÁö¸¦ È®ÀÎÇÏ°í ¿©±â¿¡ lastModified ű׷ΠÆäÀÌÁö¸¦ ÀÛµ¿½ÃŲ´Ù.

´ç¿¬È÷ ŸÀÓ ½ºÅÆÇÁ°¡ ³ªÅ¸³­´Ù. ¸¸ÀÏ ¿©·¯ºÐÀÌ ³»°¡ º¸°íÀÖ´Â °ÍÀ» º¸°íÀÖ´Ù¸é ¾Æ¿ôDzÀÇ Æ÷¸ËÆÃÀº Àü°ú °°´Ù. ¹®Á¦´Â ¾î¶² »õ·Î¿î format °ªµµ Ãß°¡µÇÁö ¾Ê¾Ò´Ù´Â Á¡ÀÌ´Ù. µû¶ó¼­ ¿¹Àü°ú °°Àº °ÍÀ» º¸°íÀÖ´Â °ÍÀÌ µÈ´Ù. ÀÌ ÀÛÀº Å×½ºÆ® ½ÇÇàÀ¸·Î format ¾ÖÆ®¸®ºäÆ®¿¡ µðÆúÆ® °ªÀ» Ãß°¡ÇÏ´Â °ÍÀÌ ¾ó¸¶³ª Áß¿äÇÑÀÏÀÎÁö ¾Ë°ÔµÈ´Ù.

Ä¿½ºÅÒ ¾ÖÆ®¸®ºäÆ®¿¡ µðÆúÆ® °ªÀ» ÁÖ´Â °ÍÀº ÁÁÀº »ý°¢ÀÌ´Ù. ¿Ö³ÄÇϸé ÆäÀÌÁö ÀÛ¼ºÀÚ°¡ °íÀ¯ÀÇ °ªÀ» Á¦°øÇÏ°í ½ÍÁö ¾ÊÀ» ¶§ °ï°æ¿¡ óÇÏÁö ¾Ê±â ¶§¹®ÀÌ´Ù. ¶§·Î´Â »õ·Î¿î ¾ÖÆ®¸®ºäÆ®¿Í Æ÷¸ËÀ» ¹è¿ì´Âµ¥ ½Ã°£ÀÌ ÇÊ¿äÇÏ°í ±× »çÀÌ µðÆúÆ®´Â ÈǸ¢ÇÑ ´ë¸®ÀÎÀÌ µÇ´Â °ÍÀÌ´Ù.

¸ÎÀ½¸»
Listing 6 Àº ŸÀÓ ½ºÅÆÇÁ ¿¹Á¦("JSP best practices : ŸÀÓ ½ºÅÆÇÁÀÇ Èû" ÂüÁ¶)ÀÇ footer.jsp ÀÌ´Ù. format ¾ÖÆ®¸®ºäÆ®¿¡ °ªÀÌ Á¦°øµÇ¾ú´Ù:

Listing 6. Æ÷¸Ë ¾ÖÆ®¸®ºäÆ® »ç¿ëÇϱâ

<%@ taglib prefix="site-utils"
             uri="http://www.newInstance.com/taglibs/site-utils"%>

          </td>
          <td width="16" align="left" valign="top"> </td>
    </tr>
    <!-- End main content -->

<!-- Begin footer section -->
    <tr>
      <td width="91" align="left" valign="top" bgcolor="#330066"> </td>
      <td align="left" valign="top"> </td>
      <td class="footer" align="left" valign="top"><div align="center"><br>
          &copy; 2003 
          <a href="mailto:webmaster@newInstance.com">Brett McLaughlin</a><br>
          Last Updated: <site-utils:lastModified 
            format="HH:mm a zz :: MM/dd/yyyy"/>
        </div></td>
          <td align="left" valign="top"> </td>
      <td width="141" align="right" valign="top" bgcolor="#330066"> </td>
    </tr>
</table>
<!-- End footer section -->

Âü°íÀÚ·á

¸ñ Â÷:
"Hello, world" Ä¿½ºÅ͸¶ÀÌ¡
lastModified È®ÀåÇϱâ
±¸Çö »ó¼¼
µðÆúÆ® °ª
¸ÎÀ½¸»
Âü°í ÀÚ·á
ÇÊÀÚ ¼Ò°³
±â»ç¿¡ ´ëÇÑ Æò°¡
°ü·Ã dW ¸µÅ©:
JSP best practices series
Ä¿½ºÅÒ Å±׷ΠJSP ÆäÀÌÁö Á¦¾îÇϱâ
JSP taglibs: Better usability by design
Subscribe to the developerWorks newsletter
US ¿ø¹® Àбâ
Also in the Java zone:
Tutorials
Tools and products
Code and components
Articles
ÇÊÀÚ¼Ò°³
Photo of Brett McLaughlinBrett McLaughlinÀº ÇöÀç ÀÚ¹Ù ¹× °ü·Ã ±â¼úÀ» ÀÌ¿ëÇÏ¿© ¾ÖÇø®ÄÉÀÌ¼Ç ±â¹Ý±¸Á¶ ±¸ÇöÀ» Àü¹®ÀûÀ¸·Î ¼öÇàÇÏ°í ÀÖ´Ù. BrettÀº Java Apache ÇÁ·ÎÁ§Æ®ÀÎ TurbineÀÇ °øµ¿ ⸳ÀÚÀÌ´Ù.
ÀÌ ±â»ç¿¡ ´ëÇÏ¿© ¾î¶»°Ô »ý°¢ÇϽʴϱî?

Á¤¸» ÁÁ´Ù (5) ÁÁ´Ù (4) ±×Àú±×·¸´Ù (3) ¼öÁ¤º¸¿ÏÀÌ ÇÊ¿äÇÏ´Ù(2) ÇüÆí¾ø´Ù (1)

  È¸»ç¼Ò°³  |  °³ÀÎÁ¤º¸ º¸È£Á¤Ã¥  |  ¹ý·ü  |  ¹®ÀÇ