¼Ò¼³°°ÀºJSP1ºÎ  5Àå Core of JSP  06 ³»À尴ü pageContextÀÇ ½ÇÁ¦Àǹ̠ 

 

06 ³»À尴ü pageContextÀÇ ½ÇÁ¦ ÀǹÌ

 

ÇϳªÀÇ ÆäÀÌÁö´ç ÇϳªÀÇ PageContext°¡ Á¸ÀçÇÕ´Ï´Ù. ´Ù½Ã ¸»¾¸ µå¸®ÀÚ¸é, JSP¸¦ ½ÇÇà½ÃÄѼ­ »ý±â´Â ¼­ºí¸´ Çϳª´ç ÇϳªÀÇ PageContext°¡ ÀÖ´Â °ÍÀÔ´Ï´Ù. ±×¸®°í ÀÌ·± PageContext´Â ÇØ´ç ÆäÀÌÁöÀÇ ´Ù¾çÇÑ Á¤º¸¿Í Ư¼ºµéÀ» °ü¸®ÇØÁÖ¸ç, ÆäÀÌÁö ¹üÀ§ ³»¿¡¼­ Á¤º¸°øÀ¯¸¦ µ½´Â ¿ªÇÒÀ» ÇØ ÁÝ´Ï´Ù. ÀÌ·¸°Ô Á¤º¸°øÀ¯ÀÇ Àǹ̿¡¼­ »ý°¢ÇØ º¸¸é, PageContext Ŭ·¡½ºÀÇ ³»Àå °´Ã¼ÀÎ pageContext¸¦ ÀÌ¿ëÇÑ Á¤º¸ÀÇ °øÀ¯ÀÇ ¹üÀ§´Â ÇöÀç ÆäÀÌÁö ³»¿¡ Á¦Çѵȴٴ °ÍÀ» ¾î·ÆÁö ¾Ê°Ô »ý°¢ÇÒ ¼ö ÀÖ½À´Ï´Ù.

 

¿ì¸®´Â ¾ÕÀå¿¡¼­ ServletContext¿¡ ´ëÇØ ¹è¿ü½À´Ï´Ù. ÇϳªÀÇ À¥ ¾ÖÇø®ÄÉÀ̼ǿ¡ ÇϳªÀÇ ServletContext°¡ Á¸ÀçÇØ¼­ °°Àº ¾ÖÇø®ÄÉÀÌ¼Ç ³»¿¡¼­´Â ServletContext¸¦ ÀÌ¿ëÇØ¼­ Á¤º¸¸¦ °øÀ¯ÇÒ ¼ö ÀÖ¾ú½À´Ï´Ù. ÀÌ·¸°Ô Á¤º¸°øÀ¯ÀÇ ¹üÀ§¿Í °ü·ÃÇØ ServletContext¿Í PageContext¿¡ ´ëÇØ »ìÆìº»´Ù¸é, ¾Æ·¡ ±×¸²°ú °°ÀÌ ³ªÅ¸³¾ ¼ö ÀÖ½À´Ï´Ù.

 

 ±×¸²À» º¸½Ã¸é, À§¿¡¼­ ¼³¸í µå·È´ø PageContext¿Í ServletContextÀÇ À§Ä¡¿Í ¹üÀ§ ±×¸®°í µÑÀÇ °ü°è¿¡ ´ëÇØ¼­ ½±°Ô ÀÌÇØÇÏ½Ç ¼ö ÀÖÀ» °ÍÀÔ´Ï´Ù.  Áï, ÇϳªÀÇ ServletContext¸¦ ¹ÙÅÁÀ¸·Î °ü¸®µÇ´Â ¿©·¯ °³ÀÇ servletÀº ÀÚ½ÅÀÇ Á¤º¸¸¦ °ü¸®ÇÏ´Â PageContext¸¦ °¢°¢ °¡Áö°í ÀÖ´Ù´Â °ÍÀÔ´Ï´Ù. ±×¸®°í PageContextÀÇ ¹üÀ§´Â ÀÚ½ÅÀÇ ¼­ºí¸´ ³»ºÎ·Î Á¦Çѵǰí ÀÖ½À´Ï´Ù.

 

             ServletContext¿Í PageContextÀÇ ¹üÀ§

n        ServletContext : ÇϳªÀÇ ¾ÖÇø®ÄÉÀ̼ǿ¡ ÇϳªÀÇ ServletContextÁ¸Àç

n        PageContext : ÇϳªÀÇ ¼­ºí¸´¿¡ ÇϳªÀÇ PageContextÁ¸Àç

 

 PageContext¸¦ ÀÌ¿ëÇÑ Á¤º¸ÀÇ °øÀ¯°¡ ÇöÀçÀÇ ÆäÀÌÁö ³»¿¡¼­¸¸ ÀÌ·ç¾îÁö´ÂÁö ¿¹Á¦¸¦ ÅëÇØ Áõ¸íÇØ º¸°Ú½À´Ï´Ù. ÇϳªÀÇ StringÇü º¯¼ö°ªÀ» pageContextÀÇ ÀÎÀÚ·Î ÀúÀåÇÑ ´ÙÀ½ ±× ÆäÀÌÁö³»¿¡¼­ pageContext¸¦ ÀÌ¿ëÇØ º¯¼ö°ªÀ» ¿©·¯ ¹ø Àоî¿Í °øÀ¯°¡ µÇ´ÂÁö È®ÀÎÇϰí, ¶Ç ´Ù¸¥ ÆäÀÌÁö¿¡¼­ pageContext¸¦ ÅëÇÏ¿© À§ º¯¼ö¸¦ °øÀ¯ÇØ Àд °ÍÀÌ °¡´ÉÇÑÁö »ìÆìº¸°Ú½À´Ï´Ù.

 

realPageContext.jsp

ÆäÀÌÁö ÄÁÅØ½ºÆ®ÀÇ Á¤º¸°øÀ¯ Å×½ºÆ®

<%@ page contentType= "text/html;charset = KSC5601"%>

<%

String real  = "realPageContext page";

pageContext.setAttribute("pageValue",real);

%>

<HTML><BODY>

<h2>real PageContext</h2>

<h3>** ÆäÀÌÁöÄÁÅØ½ºÆ®ÀÇ ÆäÀÌÁö³» °øÀ¯** <h3>

<h4>-È£Ãâ-</h4>

<%

Object cobj = pageContext.getAttribute("pageValue");

           if(cobj != null){

                     String pValue = (String)cobj;

                     out.println("<h3>page Value = " + pValue + "</h3>");

 

           }

             else{

                           out.println("Attrubute Not found!!");

             }

%>

<h4>-´Ù½Ã È£Ãâ-</h4>

<%

Object dobj = pageContext.getAttribute("pageValue");

           String qValue = (String)dobj;

           out.println("<h3>page Value again =" + qValue + "</h3>");

%>

</BODY></HTML>

 

 

 

 ¼Ò½º¸¦ ¸ÕÀú ¼³¸íµå¸®°í °á°ú¿¡ ´ëÇØ »ìÆìº¸°Ú½À´Ï´Ù.

 

StringÇü º¯¼ö real¿¡ ¹®ÀÚ¿­À» ³Ö¾î¼­ pageContext¿¡ pageValue¶õ À̸§À¸·Î ÁöÁ¤Çϰí ÀÖ½À´Ï´Ù.

 

n        String real  = "realPageContext page";

n        pageContext.setAttribute("pageValue",real);

 

±×¸®°í ´Ù½Ã pageContext¿¡ ¡°pageValue¡±¶õ °ªÀ¸·Î ÁöÁ¤µÇ¾î ÀÖ´Â °ªÀ» ObjectÇüÀ¸·Î ¹Þ¾Æ³À´Ï´Ù. ±×¸®°í ±× °ªÀ» °Ë»çÇØ ³Î°ªÀÌ ¾Æ´Ï¶ó¸é StringÇüÀ¸·Î ´Ù¿îij½ºÆÃÇÏ¿© ±× °ªÀ» Ãâ·ÂÇØÁÝ´Ï´Ù.  ¸¸¾à ³Î°ªÀ̶ó¸é, ¡°Attribute Not found¡±¶õ ¸Þ¼¼Áö¸¦ Ãâ·ÂÇÕ´Ï´Ù.

 

n           Object cobj = pageContext.getAttribute("pageValue");

n           if(cobj != null){

n                String pValue = (String)cobj;

n                out.println("<h3>page Value = " + pValue + "</h3>");

n           }

n           else{

n                out.println("Attrubute Not found!!");

n           }

n           SampleObject obje = new SampleObject();

 

 

 °øÀ¯ÀÇ È®ÀÎÀ» À§Çؼ­ ´Ù½Ã Çѹø  pageContext¿¡¼­ ¡°pageValue¡±°ªÀ» ºÒ·¯¿É´Ï´Ù.

 

n        Object dobj = pageContext.getAttribute("pageValue");

n                     String qValue = (String)dobj;

n                     out.println("<h3>page Value again =" + qValue + "</h3>");

 

 ½ÇÇà °á°ú¸¦ º¸½Ã¸é, pageContext·Î ÁöÁ¤ÇÑ º¯¼ö°ªÀÌ ½ºÅ©¸³Æ®·¿ÀÇ ¾îµð¼­³ª ºÒ·¯¼­ ¾µ ¼ö ÀÖÀ½À» ¾Ë ¼ö ÀÖ½À´Ï´Ù.

 

 ´ÙÀ½Àº ´Ù¸¥ ÆäÀÌÁö¿¡¼­ À§¿¡¼­ ÁöÁ¤ÇÑ º¯¼ö°ªÀ» °øÀ¯ÇÒ ¼ö ÀÖ´ÂÁö Å×½ºÆ®ÇÏ´Â ¿¹Á¦ÀÔ´Ï´Ù.

 

realPageContext1.jsp

ÆäÀÌÁö ÄÁÅØ½ºÆ®ÀÇ Á¤º¸°øÀ¯ Å×½ºÆ®

<%@ page contentType= "text/html;charset = KSC5601"%>

<HTML><BODY>

<h2>real PageContext</h2>

<h3>** ÆäÀÌÁöÄÁÅØ½ºÆ®ÀÇ ÆäÀÌÁö³» °øÀ¯** <h3>

<h4>-È£Ãâ-</h4>

<%

Object cobj = pageContext.getAttribute("pageValue");

           if(cobj != null){

                     String pValue = (String)cobj;

                     out.println("<h3>page Value = " + pValue + "</h3>");

           }

           else{

                     out.println("Attrubute Not found!!");

           }

%>

</BODY></HTML>

 

 

 

¼Ò½º¸¦ º¸½Ã¸é, À§¿Í ¸¶Âù°¡Áö·Î pageContext°´Ã¼¿¡¼­ ¡°pageValue¡±¶õ º¯¼ö°ªÀ» ã¾Æ³»¾î¼­ Ãâ·ÂÇÏ·Á Çϰí ÀÖ½À´Ï´Ù. ÇÏÁö¸¸ °á°ú¿¡¼­ È®ÀÎÇÒ ¼ö ÀÖ´Â °Í°ú °°ÀÌ ¿©±â¼­´Â ¡°pageValue¡±¶õ ÀÎÀÚ¸¦ ãÁö ¸øÇÏ°í ¡°Attribute Not found¡±¶õ ¸Þ¼¼Áö¸¸ Ãâ·ÂÇϰí ÀÖ½À´Ï´Ù.

 

n        Object cobj = pageContext.getAttribute("pageValue");

n                     if(cobj != null){

n                                   String pValue = (String)cobj;

n                                   out.println("<h3>page Value = " + pValue + "</h3>");

n                     }

n                     else{

n                                   out.println("Attrubute Not found!!");

 

 ÀÌ·¸°Ô pageContext´Â Á¤º¸¸¦ ÀÚ½ÅÀÇ ÆäÀÌÁö ³»¿¡¼­¸¸ °øÀ¯ÇÒ ¶§ »ç¿ëµÉ ¼ö ÀÖ½À´Ï´Ù.



jabookÀúÀÚ¸íÇÔ
Á¦¸ñ:¼Ò¼³°°ÀºJSP1ºÎ
ÀúÀÚ:ÃÖ¿µ°ü