Artisoc/HTTPサーバ機能の紹介
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]
開始行:
[[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スクリプト...
モデルでは、Univ_InitでHTTPサーバの処理を記述したPnutsス...
#pre{{
Univ_Init{
ExecPnuts("p_HTTP_01.pnuts")
}
}}
次に、p_HTTP_01.pnutsの内容を行番号付きで表示します。
- 6-11行:HTTPの応答用ヘッダを変数headerにセットします。p...
- 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を入力します。Artiso...
#pre{{
http://localhost:8080/hello
}}
ブラウザーに以下の様にHello World!が表示されます。
&ref(Browser_hello_world.png);
** テンプレートを使ったHello World [#e34c865b]
出力するHTMLをpnutsスクリプトに直接書くのはあまり便利では...
次の例題 &ref(p_HTTP_02.model); では、Velocityテンプレー...
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サーバと同じようにコー...
とテンプレートに表示する内容(Model)を返します。
上記の例では、Viewとして、p_HTTP_02.vmをセットし、Modelに...
p_HTTP_02.vmには、表示するHTMLをそのまま記述します。((も...
以下に&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...
HTMLをサーバから返す方法を紹介します。
例題 &ref(p_HTTP_03.model); では、Universeの直下にhtmlと...
Univ_Initで以下の様にHTMLをセットします。
#pre{{
Univ_Init{
Universe.html = "<html><body><h3>Hello World from Ar...
ExecPnuts("p_HTTP_03.pnuts")
}
}}
Pnutsスクリプト(&ref(P_HTTP_03.pnuts);)は、以下の様にし...
registHmltPage関数を使ってサービス/helloに対して返す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" v...
</td>
</tr>
<tr>
<td align="right">
<input type="submit" value="Submit" n...
</td>
<td align="left">
<input type="reset" value="Cancel" na...
</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変...
こんなに簡単にフォームのコールバックが作れるのもArtisoc H...
#pre{{
function setuserInput(commands)
{
PnutsUtil::setValue(engine, "Universe.userInput", co...
return (createModelAndView("<html><body><h3>Done.</h...
}
// コールバックを定義
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
終了行:
[[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スクリプト...
モデルでは、Univ_InitでHTTPサーバの処理を記述したPnutsス...
#pre{{
Univ_Init{
ExecPnuts("p_HTTP_01.pnuts")
}
}}
次に、p_HTTP_01.pnutsの内容を行番号付きで表示します。
- 6-11行:HTTPの応答用ヘッダを変数headerにセットします。p...
- 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を入力します。Artiso...
#pre{{
http://localhost:8080/hello
}}
ブラウザーに以下の様にHello World!が表示されます。
&ref(Browser_hello_world.png);
** テンプレートを使ったHello World [#e34c865b]
出力するHTMLをpnutsスクリプトに直接書くのはあまり便利では...
次の例題 &ref(p_HTTP_02.model); では、Velocityテンプレー...
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サーバと同じようにコー...
とテンプレートに表示する内容(Model)を返します。
上記の例では、Viewとして、p_HTTP_02.vmをセットし、Modelに...
p_HTTP_02.vmには、表示するHTMLをそのまま記述します。((も...
以下に&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...
HTMLをサーバから返す方法を紹介します。
例題 &ref(p_HTTP_03.model); では、Universeの直下にhtmlと...
Univ_Initで以下の様にHTMLをセットします。
#pre{{
Univ_Init{
Universe.html = "<html><body><h3>Hello World from Ar...
ExecPnuts("p_HTTP_03.pnuts")
}
}}
Pnutsスクリプト(&ref(P_HTTP_03.pnuts);)は、以下の様にし...
registHmltPage関数を使ってサービス/helloに対して返す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" v...
</td>
</tr>
<tr>
<td align="right">
<input type="submit" value="Submit" n...
</td>
<td align="left">
<input type="reset" value="Cancel" na...
</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変...
こんなに簡単にフォームのコールバックが作れるのもArtisoc H...
#pre{{
function setuserInput(commands)
{
PnutsUtil::setValue(engine, "Universe.userInput", co...
return (createModelAndView("<html><body><h3>Done.</h...
}
// コールバックを定義
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
ページ名:
SmartDoc