Request的getRequestURI()和getPathInfo()

在servlet處理請求的路徑時request里有幾個常用的函數(shù):

getRequestURL()
getRequestURI()
getContextPath()
getServletPath()
getPathInfo()

這里的前三個函數(shù)的返回值基本是可以預期的,與servlet映射的URL基本沒關(guān)系,而后兩個返回的值就是與servlet映射的URL相關(guān)的了篙挽。(對于URL和URI的關(guān)系刁标,請看這里

先看一下前三個:

訪問 : http://localhost:8080/myapp/test/first.html
URL : http://localhost:8080/myapp/test/first.html
URI : /myapp/test/first.html
ContextPath : /myapp

StringBuffer getRequestURL()
Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.

String getRequestURI()
Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. The web container does not decode this String. For example:

First line of HTTP request Returned Value
POST /some/path.html HTTP/1.1 /some/path.html
GET http://foo.bar/a.html HTTP/1.0 /a.html
HEAD /xyz?a=b HTTP/1.1 /xyz

String getContextPath()
Returns the portion of the request URI that indicates the context of the request. The context path always comes first in a request URI. The path starts with a "/" character but does not end with a "/" character. For servlets in the default (root) context, this method returns "". The container does not decode this string.

關(guān)于PathInfo和ServletPath

情況1(path mapping):
@WebServlet("/test1/*")
Request : http://localhost:8080/myapp/test1/first.html
ServletPath : /test1
PathInfo : /first.html

情況2(exact match):
@WebServlet("/test2/t2")
Request : http://localhost:8080/myapp/test2/t2
ServletPath : /test2/t2
PathInfo : null

情況3(extension mapping):
@WebServlet("*.do")
Request : http://localhost:8080/myapp/test3/t3.do
ServletPath : /test3/t3.do
PathInfo : null

情況4(default match):
@WebServlet("/")
Request : http://localhost:8080/myapp/
ServletPath : /
PathInfo : null

String getPathInfo()
Returns any extra path information associated with the URL the client sent when it made this request. The extra path information follows the servlet path but precedes the query string and will start with a "/" character.
This method returns null if there was no extra path information.

String getServletPath()
Returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. Same as the value of the CGI variable SCRIPT_NAME.
This method will return an empty string ("") if the servlet used to process this request was matched using the "/*" pattern.

擴展閱讀:
關(guān)于servlet mapping

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末素标,一起剝皮案震驚了整個濱河市麻削,隨后出現(xiàn)的幾起案子妆档,更是在濱河造成了極大的恐慌僻爽,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件贾惦,死亡現(xiàn)場離奇詭異胸梆,居然都是意外死亡,警方通過查閱死者的電腦和手機须板,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門碰镜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人习瑰,你說我怎么就攤上這事绪颖。” “怎么了甜奄?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵柠横,是天一觀的道長。 經(jīng)常有香客問我课兄,道長牍氛,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任第喳,我火速辦了婚禮,結(jié)果婚禮上踱稍,老公的妹妹穿的比我還像新娘曲饱。我一直安慰自己,他們只是感情好珠月,可當我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布扩淀。 她就那樣靜靜地躺著,像睡著了一般啤挎。 火紅的嫁衣襯著肌膚如雪驻谆。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天庆聘,我揣著相機與錄音胜臊,去河邊找鬼。 笑死伙判,一個胖子當著我的面吹牛象对,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播宴抚,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼勒魔,長吁一口氣:“原來是場噩夢啊……” “哼甫煞!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起冠绢,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤抚吠,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后弟胀,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體楷力,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年邮利,在試婚紗的時候發(fā)現(xiàn)自己被綠了弥雹。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡延届,死狀恐怖剪勿,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情方庭,我是刑警寧澤厕吉,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站械念,受9級特大地震影響头朱,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜龄减,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一项钮、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧希停,春花似錦烁巫、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至违崇,卻和暖如春阿弃,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背羞延。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工渣淳, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人伴箩。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓水由,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子砂客,可洞房花燭夜當晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理泥张,服務發(fā)現(xiàn),斷路器鞠值,智...
    卡卡羅2017閱讀 134,693評論 18 139
  • =========================================================...
    lavor閱讀 3,492評論 0 5
  • 已經(jīng)同步到gitbook媚创,想閱讀的請轉(zhuǎn)到gitbook: Django 1.10 中文文檔 URL dispatc...
    leyu閱讀 14,207評論 0 16
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc閱讀 2,873評論 0 0
  • 最后你還是離開我了。 我們那么小心翼翼彤恶,不要壓力钞钙,不要諾言,不要認真的努力相處声离。 最后還是會分開芒炼。 還是不能忍受,...
    趙鋮zxy閱讀 185評論 0 1