- 追加された行はこの色です。
- 削除された行はこの色です。
[[FrontPage]]
#contents
2014/03/21からのアクセス回数 &counter;
** ArtisocのHTTPサーバ機能(β版)の紹介 [#i154ddea]
Artisocには、Pnutsで実装したHTTPサーバが含まれています。
Artisocのsetup.pnutsにバグがありました。以下のファイルと入れ替えて下さい。
- &ref(setup.pnuts);
簡単な例を使ってArtisoc HTTPサーバの使い方を紹介します。
** Hello World [#gfbcb6c8]
プログラムの基本は、Hello Worldと表示する例題からはじまります。
これをArtisoc HTTPサーバで行う例を最初に示します。
使用するモデルは、&ref(p_HTTP_01.model);とPnutsスクリプト、&ref(p_HTTP_01.pnuts);です。
モデルでは、Univ_InitでHTTPサーバの処理を記述したPnutsスクリプトを実行します。
#pre{{
Univ_Init{
ExecPnuts("p_HTTP_01.pnuts")
}
}}
次に、p_HTTP_01.pnutsの内容を行番号付きで表示します。
- 6-11行:HTTPの応答用ヘッダを変数headerにセットします。pnutsではアクセント記号`を使って複数行にまたがる文字列を扱うことができます。
- 13-23行:HTMLの本体を変数bodyにセットします。
- 25-29行:helloコールバック関数を定義
- 32行:/helloというサービスを受けたときに処理するコールバックを登録します
- 34行:HTTPサービススレッドを起動(fork)します
#pre{{
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です。
#pre{{
http://localhost:8080/hello
}}
ブラウザーに以下の様にHello World!が表示されます。
&ref(Browser_hello_world.png);
** テンプレートを使ったHello World [#e34c865b]
出力するHTMLをpnutsスクリプトに直接書くのはあまり便利ではありません。
次の例題 &ref(p_HTTP_02.model); では、Velocityテンプレートを使ってHTMLを出力します。
Pnutsスクリプト(&ref(P_HTTP_02.pnuts);)は、以下の様にとても簡単になっています。
helloコールバック関数のcreateModelAndViewがポイントです。
#pre{{
// コールバックを定義
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をそのまま記述します。((もちろんVelocityのテンプレートを書くこともできます))
以下に&ref(p_HTTP_02.vm);の内容を示します。((HTMLそのものですね!))
#pre{{
<html>
<head>
<title>Artisoc HTTP Test</title>
</head>
<body>
<h3>Hello World again.</h3>
</body>
</html>
}}
先ほどと同様にブラウザーで表示すると、Hello World again.と出てきます。
** Artisocの変数にHTMLをセット [#y0f60522]
これまでの例では、Artisocのモデルが登場してきませんが、3番目の方法はArtisocの変数にセットした
HTMLをサーバから返す方法を紹介します。
例題 &ref(p_HTTP_03.model); では、Universeの直下にhtmlという名前の文字型の変数を定義してあります。
Univ_Initで以下の様にHTMLをセットします。
#pre{{
Univ_Init{
Universe.html = "<html><body><h3>Hello World from Artisoc.</h3></body></html>"
ExecPnuts("p_HTTP_03.pnuts")
}
}}
Pnutsスクリプト(&ref(P_HTTP_03.pnuts);)は、以下の様にします。
registHmltPage関数を使ってサービス/helloに対して返すHTMLを保持した変数のパス(Universe.html)を渡します。
#pre{{
// サービス /hello にそのコールバック関数helloを登録
registHtmlPage("/hello", "Universe.html")
// HTTPサーバスレッドを起動
child = fork(HttpServiceThread)
}}
** HTMLのフォームを使ってArtisocに値を渡す [#b43fd46b]
HTTPサーバーでは、HTMLのフォームを使ってブラウザーから値をサーバに渡すことができます。
Artisoc HTTPサーバでも同様の処理ができます。
テンプレート(&ref(p_HTTP_04.vm);)は、HTMLの場合とほとんど同じです。
#pre{{
<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>
}}
ブラウザーの表示は、以下の様になります。
ブラウザーで、以下のURLを入力すると以下の様になります。
#pre{{
http://localhost:8080/userInput
}}
&ref(p_HTTP_04Brower.png);
Artisocのモデルは、&ref(p_HTTP_04.model); には、userInput変数がセットされており、
出力設定で、Universe.userInput変数の値を表示するようにしています。
&ref(p_HTTP_04.png);
それでは、スクリプト(&ref(P_HTTP_04.pnuts);)を見てみましょう。
setuserInput関数では、引数のcommandsからuserInputの値を取り出し、
それをPnutsUtilのsetValue関数を使ってUniverse.userInput変数にセットしているだけです。
こんなに簡単にフォームのコールバックが作れるのもpnuts言語の特徴です。
こんなに簡単にフォームのコールバックが作れるのもArtisoc HTTPサーバの特徴です。
#pre{{
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)
}}
ちょっとここらで、一息いれます。
** コメント [#p46ea21d]
#vote(おもしろかった,そうでもない,わかりずらい)
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha