ユーザからの要求パラメタ(GETとPOSTによるパラメタ渡しの差)

 


ユーザからサーバにパラメタをHTTPで送るときは、URLに直接書き込むGET要求と、フォームデータのGET要求またはPOST要求による送信がある。これらについてその基本を理解しよう。

 

次のようなHTMLテキストをワードパッドなどを使って自分のコンピュータの適当なファイルに例えばSimpleHttpRequestTest.htmlという名前でセーブし、これをブラウザで表示させよう。名前と名字に入力したら提出ボタンを押すと、ブラウザはhttp://localhost:8080/HttpRequestDumpをパラメタつきでアクセスする。<FORM METHOD=GET…のタグのGETPOSTに置き換えるとどうなるか調べてみよう。

 

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<HTML><HEAD><TITLE>Simple HTTP Request Test Document #1</TITLE></HEAD>

<BODY>

<H1>姓名の入力</H1><P>名前と名字を入力したら<STRONG>提出</STRONG>ボタンを押してください</P><HR>

<FORM METHOD=GET ACTION="http://localhost:8080/HttpRequestDump">

<P>名前: <INPUT TYPE=TEXT NAME="firstName"><P>

<P>名字: <INPUT TYPE=TEXT NAME="familyName"><P>

<P><INPUT TYPE=SUBMIT VALUE="提出">

</FORM>

<HR></BODY></HTML>

 

GET要求の場合とPOST要求の場合のIE上の表示をならべると下表のようになる。両者が相違している部分を青で示してある。要求パラメタとしてサーブレット・エンジンが渡してくれるデータは同じであっても、GET要求の場合はフォームに入力されたデータがURLのクエリ文字列として送信され、POST要求の場合はHTTPパケットのボディ(コンテント)部分に50バイトのデータとして送信されたことがわかる。

 

GET要求の場合

POST要求の場合

要求オブジェクトの情報

getAuthType: null

getCharacterEncoding: EUC-JP

getContentLength: 0

getContentType: null

getMethod: GET

getPathInfo: /HttpRequestDump

getPathTranslated: (省略)

getProtocol: HTTP/1.1

getQueryString: firstName=%8F%83%88%EA%98Y&familyName=%8F%AC%90%F2

getRemoteAddr: 127.0.0.1

getRemoteHost: localhost

getRemoteUser: null

getRequestURI: /HttpRequestDump

getRequestedSessionId: null

isRequestedSessionIdValid: false

isRequestedSessionIdFromCookie: false

isRequestedSessionIdFromURL: false

getScheme: http

getServerName: localhost

getServerPort: 8080

getServletPath: null

 

要求パラメタ:

firstName:

純一郎

familyName:

小泉

 

要求ヘッダのダンプ:

accept : application/vnd.ms-powerpoint, application/vnd.ms-excel

accept-language : ja

accept-encoding : gzip, deflate

user-agent : Mozilla/4.0 (compatible; MSIE 5.01; Windows 98)

host : localhost:8080

connection : Keep-Alive

 

属性(Attributes):

com.ibm.websphere.olt.include.bool : false

com.ibm.websphere.olt.forward.request : HttpRequestDump

 

クッキー(Cookies):

 

*** 以上 ***

 

要求オブジェクトの情報

getAuthType: null

getCharacterEncoding: EUC-JP

getContentLength: 50

getContentType: application/x-www-form-urlencoded

getMethod: POST

getPathInfo: /HttpRequestDump

getPathTranslated: (省略)

getProtocol: HTTP/1.1

getQueryString: null

getRemoteAddr: 127.0.0.1

getRemoteHost: localhost

getRemoteUser: null

getRequestURI: /HttpRequestDump

getRequestedSessionId: null

isRequestedSessionIdValid: false

isRequestedSessionIdFromCookie: false

isRequestedSessionIdFromURL: false

getScheme: http

getServerName: localhost

getServerPort: 8080

getServletPath: null

 

要求パラメタ:

firstName:

純一郎

familyName:

小泉

 

要求ヘッダのダンプ:

accept : application/vnd.ms-powerpoint, application/vnd.ms-excel

accept-language : ja

content-type : application/x-www-form-urlencoded

accept-encoding : gzip, deflate

user-agent : Mozilla/4.0 (compatible; MSIE 5.01; Windows 98)

host : localhost:8080

content-length : 50

connection : Keep-Alive

 

属性(Attributes):

com.ibm.websphere.olt.include.bool : false

com.ibm.websphere.olt.forward.request : HttpRequestDump

 

クッキー(Cookies):

 

*** 以上 ***

 

 

content-type: application/x-www-form-urlencodedなるヘッダ行は一体何を意味するのだろうか?

 

firstName:純一郎、familyName:小泉なる二つの名前と値の要求パラメタは

firstName=%8F%83%88%EA%98Y&familyName=%8F%AC%90%F2

なる50バイトのASCII文字列に変換されてURLのクエリ文字やPOSTパケットコンテントとしてサーバに送られる。ASCII文字列に統一すれば言語の相違による混乱を防止できるからである。これをURLエンコードと呼ぶ。

 

 

前節     目次     次節