2014/03/21からのアクセス回数 1558
Artisocには、Pnutsで実装したHTTPサーバが含まれています。
Artisocのsetup.pnutsにバグがありました。以下のファイルと入れ替えて下さい。
簡単な例を使ってArtisoc HTTPサーバの使い方を紹介します。
プログラムの基本は、Hello Worldと表示する例題からはじまります。
これをArtisoc HTTPサーバで行う例を最初に示します。
使用するモデルは、p_HTTP_01.modelとPnutsスクリプト、
p_HTTP_01.pnutsです。
モデルでは、Univ_InitでHTTPサーバの処理を記述したPnutsスクリプトを実行します。
Univ_Init{ ExecPnuts("p_HTTP_01.pnuts") }
次に、p_HTTP_01.pnutsの内容を行番号付きで表示します。
1 /* 2 * Hello World! 3 */ 4 5 // HTTP応答ヘッダを定義 6 header=`HTTP 1.0 200 OK 7 Server: MAS Server 1.0 8 Control-cache: noCache 9 Content-type: text/html 10 11 ` 12 // HTMLの本体を定義 13 body=` 14 <html> 15 <head> 16 <title>Artisoc HTTP Test</title> 17 </head> 18 19 <body> 20 <h3>Hello World!</h3> 21 </body> 22 </html> 23 ` 24 // コールバックを定義 25 function hello(commands) 26 { 27 ::out.write(header.getBytes()) 28 ::out.write(body.getBytes()) 29 } 30 31 // サービス /hello にそのコールバック関数helloを登録 32 registService("/hello", hello) 33 // HTTPサーバスレッドを起動 34 child = fork(HttpServiceThread)
モデルを実行し、ブラウザーで以下のURLを入力します。Artisoc HTTPサーバのポートのデフォルトは、8080です。
http://localhost:8080/hello
ブラウザーに以下の様にHello World!が表示されます。
出力するHTMLをpnutsスクリプトに直接書くのはあまり便利ではありません。
次の例題 p_HTTP_02.model では、Velocityテンプレートを使ってHTMLを出力します。
Pnutsスクリプト(
P_HTTP_02.pnuts)は、以下の様にとても簡単になっています。
helloコールバック関数のcreateModelAndViewがポイントです。
// コールバックを定義 function hello(commands) { return (createModelAndView("p_HTTP_02.vm", null)) } // サービス /hello にそのコールバック関数helloを登録 registService("/hello", hello) // HTTPサーバスレッドを起動 child = fork(HttpServiceThread)
Artisoc HTTPサーバでは、Spring Webサーバと同じようにコールバック関数が表示するHTMLのテンプレート(View) とテンプレートに表示する内容(Model)を返します。
上記の例では、Viewとして、p_HTTP_02.vmをセットし、Modelには、null(なし)ということをセットしています。
p_HTTP_02.vmには、表示するHTMLをそのまま記述します。*1
以下にp_HTTP_02.vmの内容を示します。*2
<html> <head> <title>Artisoc HTTP Test</title> </head> <body> <h3>Hello World again.</h3> </body> </html>
先ほどと同様にブラウザーで表示すると、Hello World again.と出てきます。
これまでの例では、Artisocのモデルが登場してきませんが、3番目の方法はArtisocの変数にセットした HTMLをサーバから返す方法を紹介します。
例題 p_HTTP_03.model では、Universeの直下にhtmlという名前の文字型の変数を定義してあります。
Univ_Initで以下の様にHTMLをセットします。
Univ_Init{ Universe.html = "<html><body><h3>Hello World from Artisoc.</h3></body></html>" ExecPnuts("p_HTTP_03.pnuts") }
Pnutsスクリプト(P_HTTP_03.pnuts)は、以下の様にします。
registHmltPage関数を使ってサービス/helloに対して返すHTMLを保持した変数のパス(Universe.html)を渡します。
// サービス /hello にそのコールバック関数helloを登録 registHtmlPage("/hello", "Universe.html") // HTTPサーバスレッドを起動 child = fork(HttpServiceThread)
HTTPサーバーでは、HTMLのフォームを使ってブラウザーから値をサーバに渡すことができます。 Artisoc HTTPサーバでも同様の処理ができます。
テンプレート(p_HTTP_04.vm)は、HTMLの場合とほとんど同じです。
<html> <head> <title>HTTP Server Sample</title> </head> <body> <h3>Input user Value</h3> <form method="POST" ACTION="setUserInput"> <table border="0"> <tr> <th align="right"> userInput: </th> <td align="left"> <input type="text" name="userInput" value="$!model.userInput"> </td> </tr> <tr> <td align="right"> <input type="submit" value="Submit" name="submit"> </td> <td align="left"> <input type="reset" value="Cancel" name="reset"> </td> </tr> </table> </form> </body> </html>
ブラウザーの表示は、以下の様になります。
Artisocのモデルは、p_HTTP_04.model には、userInput変数がセットされており、
出力設定で、Universe.userInput変数の値を表示するようにしています。
それでは、スクリプト(P_HTTP_04.pnuts)を見てみましょう。
setuserInput関数では、引数のcommandsからuserInputの値を取り出し、 それをPnutsUtilのsetValue関数を使ってUniverse.userInput変数にセットしているだけです。 こんなに簡単にフォームのコールバックが作れるのもArtisoc HTTPサーバの特徴です。
function setuserInput(commands) { PnutsUtil::setValue(engine, "Universe.userInput", commands.get("userInput")) return (createModelAndView("<html><body><h3>Done.</h3></body></html>", null)) } // コールバックを定義 function userInput(commands) { return (createModelAndView("p_HTTP_04.vm", null)) } // サービス /userInput にそのコールバック関数userInputを登録 registService("/userInput", userInput) registService("/setUserInput", setuserInput) // HTTPサーバスレッドを起動 child = fork(HttpServiceThread)
ちょっとここらで、一息いれます。
皆様のご意見、ご希望をお待ちしております。