Scripting Elements는 JSP Page에 직접 코드를 끼워 넣을 수 있게 합니다. JSP Scripting Elements는 세가지 타입( Declaration, Expression, Scriptlet  ) 로 나눌 수 있습니다.

 

 

1. Declaration

변수와 메소드를 정의할 때 사용하는 태크입니다.

Syntax

<%! declaration %>

 

2. Expression

페이지 출력을 위한 문법입니다.

Syntax

<%= expression %>    

 

Example

ex4.jsp

<%@ page contentType="text/html;charset=euc-kr" %>

<%!

       /*

       이태릴 글자로 표현된 부분이 변수와 메소드를 선언한 Declaration

     부분입니다.

       */

       int[] mathPoint = { 30,50,60,20 };

 

       public double getAverage(){

                    double sum = 0;

                    for(int i=0;i<mathPoint.length;i++) sum += mathPoint[i];

                    return sum/mathPoint.length;

       }

 

       public double getBunsan(){

                    double sum = 0;

                    double average = getAverage();

 

                    for(int i=0;i<mathPoint.length;i++){

                                 sum += Math.pow((mathPoint[i]-average),2);

                    }

                    return sum/mathPoint.length;

       }

 

       public double getPyosun(){

                    return Math.sqrt(getBunsan());

       }

%>

<html>

<body>

Declaration & Expression<p>

 

해당 점수에 대한 결과

평균 : <%= getAverage() %><br>  // Expression

분산: <%= getBunsan() %><br>

표준편차 : <%= getPyosun() %><br>

 

 

3. Scriptlet

Declaration & Expression은 사용이 제한적이지요. 그러나 Scriptlet은 아주 자유스럽게 자바코드를 사용할 수 있습니다. PHP의 <? php code ?>, ASP의 <% asp code %> 와 동일합니다.

실제로는 Scriptlet이 많이 사용되면 될수록 Presentation Layer와 Business Layer 분리의 문제로 좋지 않다고 합니다. 될 수 있는 한 사용을 지양하는 것이 좋습니다.

 

Syntax

<% expression %>    

 

Example

<%@ page contentType="text/html;charset=euc-kr" %>

<%

       int intYear =0;

       String strYear = request.getParameter("YEAR");

      

       if(strYear != null){

                    intYear = Integer.parseInt(strYear);

       } else {

                    intYear = -1;

                    strYear = "";

       }

      

%>

<html>

<body>

Scriptlet<p>

<form action="ex6.jsp">

Input your year : <input type=text name=YEAR value="<%=strYear%>">

<input type=submit value="확인">

<p>

</form>

<p>

<%

/*  특별한 것이 없지요.. 아주아주 간단하지요…   */

       if(intYear >= 19){ %>

                    <font color=blue>상모의 아주 야한 사진</font>

<%

       } else if(intYear > 0 && intYear < 18) { %>

                    <font color=red>아직은 때가 아닌것 같군요. ㅋㅋㅋㅋㅋ</font>

<%

       }

%>

 

강좌의 reference

- Web Development with Java Server Pages. by Duane K. Fields & Mark A. Kolb ,곽용재편역 ; InfoBook, Manning

-  http://java.sun.com/products/jsp/tags/tags.html

-  http://myhome.shinbiro.com/~jimmy/home.htm