이 글은 http://www.ibm.com/developerworks/java/library/j-pg03155/index.html?S_TACT=105AGX02&S_CMP=EDU를 보면서 개인적으로 정리한 문서입니다.

설정하기#

필요한 jar파일#

groovlet과 gsp를 사용하기 위해서는 java를 이용한 일반적인 웹 애플리케이션 구조에서 위 3개의 jar파일만 추가하면 된다. 단 차이점이라고 하면 스크립트 언어라 groovlet도 src 밑에 들어가는것이 아니라 일종의 jsp파일처럼 작성을 하면 된다는 점이다.

샘플#

간단한 groovlet#

println """
<html><head>
<title>Groovlets 101</title>
</head>
<body>
<p>
Welcome to Groovlets 101. As you can see
this Groovlet is fairly simple.
</p>
<p>
This course is being run on the following servlet container: </br>
${application.getServerInfo()}
</p>
</body>
</html>
"""

jdk 버전 알아내기#

def testVMVersion(){
  println "<h3>JVM Version Check: </h3>"
  vers = System.getProperty("java.version")
  println "<p>JVM version: ${vers} </p>"
}
println """
<html><head>
<title>Diagnostics Check</title></head>
<body>
"""
testVMVersion()
println """
</body></html>
"""

간단한 gsp#

<html>
<head><title>index.gsp</title></head>
<body>
<b><% println "hello gsp" %></b>
<p>
<% wrd = "Groovy"
   for (i in wrd){ 
 %>
 <h1> <%=i%> <br/>
   
 <%} %>
</p>
</body>
</html>

groovlet과 gsp 함께 사용하기#

groovlet(/groovlet/db.groovy)
import groovy.sql.Sql
/**
 * forwards to passed in page
 */
def forward(page, req, res){
  dis = req.getRequestDispatcher(page);
  dis.forward(req, res);
}
sql = Sql.newInstance("jdbc:mysql://127.0.0.1:3306/test", "root", "c12345", "com.mysql.jdbc.Driver")
   
def uptime, questions, insertnum, selectnum, updatenum
sql.eachRow("show status"){ 
  if(it.variable_name == "Uptime"){
         uptime =  it[1]
	 request.setAttribute("uptime", uptime)
  }else if (it.variable_name == "Questions"){
         questions =  it[1]
	 request.setAttribute("questions", questions)
  }
}
request.setAttribute("qpm", Integer.valueOf(questions) / Integer.valueOf(uptime) )
sql.eachRow("show status like 'Com_%'"){ 
    if(it.variable_name == "Com_insert"){
         insertnum =  Integer.valueOf(it[1])
    }else if (it.variable_name == "Com_select"){
         selectnum =  Integer.valueOf(it[1])
    }else if (it.variable_name == "Com_update"){
          updatenum =  Integer.valueOf(it[1])
    }
}
request.setAttribute("qinsert", 100 * (insertnum / Integer.valueOf(uptime)))
request.setAttribute("qselect", 100 * (selectnum / Integer.valueOf(uptime)))
request.setAttribute("qupdate", 100 * (updatenum / Integer.valueOf(uptime)))

forward("/gsp/mysqlreport.gsp", request, response)

gsp(/gsp/mysqlreport.gsp)

<html><head>
<title>MySql Health Report</title>
</head>
<body>
<table>
<tr>
  <td>Database Uptime:</td><td><% println "${request.getAttribute("uptime")}" %></td>
</tr>
<tr>
  <td>Number of Queries:</td><td><% println "${request.getAttribute("questions")}" %></td>
</tr>
<tr>
  <td>Queries per Minute =</td><td><% println "${request.getAttribute("qpm")}" %></td>
</tr>
<tr>
  <td>% Queries Inserts =</td><td><% println "${request.getAttribute("qinsert")}" %></td>
</tr>
<tr>
  <td>% Queries Selects =</td><td><% println "${request.getAttribute("qselect")}" %></td>
</tr>
<tr>
  <td>% Queries Updates =</td><td><% println "${request.getAttribute("qupdate")}" %></td>
</tr>
</table>
</body>
</html>

Add new attachment

Only authorized users are allowed to upload new attachments.

List of attachments

Kind Attachment Name Size Version Date Modified Author Change note
png
groovlet.png 9.6 kB 1 21-Dec-2008 20:42 DongGukLee
« This page (revision-3) was last changed on 21-Dec-2008 20:48 by DongGukLee