天天看點

關于request.getServletPath(),request.getContextPath()的總結

本文章主要讨論以下幾種request擷取路徑的方法:

request.getServletPath()

request.getPathInfo()

request.getContextPath()

request.getRequestURI()

request.getRequestURL()

request.getServletContext().getRealPath()

以一個簡單的例子說明:

web.xml配置(注意此處的url-pattern項)

<?xml version="1.0" encoding="UTF-8"?> aab a.jsp test com.java.test.TestServlet

</servlet>
<servlet-mapping>
	<servlet-name>test</servlet-name>
	<url-pattern>/*</url-pattern><!-- 注意此處 -->
</servlet-mapping>
           

TestServlet.java檔案:

package com.java.test;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class TestServlet extends HttpServlet{

@Override

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

this.doPost(req, resp);

}

@Override

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

System.out.println(“servletPath:”+req.getServletPath());

System.out.println(“contextPath:”+req.getContextPath());

System.out.println(“contextPath2:”+req.getServletContext().getContextPath());

System.out.println(“pageInfo:”+req.getPathInfo());

System.out.println(“uri:”+req.getRequestURI());

System.out.println(“url:”+req.getRequestURL());

System.out.println(“realPath:”+req.getServletContext().getRealPath("/"));

}
           

}

此時請求http://localhost:8080/testweb (url-pattern=/*)

列印出來的值為:

servletPath:

contextPath:/testweb

contextPath2:/testweb

pageInfo:null

uri:/testweb

url:http://localhost:8080/testweb

realPath:G:\java.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testweb\

請求http://localhost:8080/testweb/abc 列印的值為:

servletPath:

contextPath:/testweb

contextPath2:/testweb

pageInfo:/abc

uri:/testweb/abc

url:http://localhost:8080/testweb/abc

realPath:G:\java.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testweb\

當我們修改web.xml為如下時(注意url-pattern的改變):

<?xml version="1.0" encoding="UTF-8"?> aab a.jsp

<servlet>
	<servlet-name>test</servlet-name>
	<servlet-class>com.java.test.TestServlet</servlet-class>
	
</servlet>

<servlet-mapping>
	<servlet-name>test</servlet-name>
	<url-pattern>/abc/def/*</url-pattern><!-- 注意此處 -->
</servlet-mapping>
           

請求http://localhost:8080/testweb/abc/def/ghi/test.html (url-pattern=/abc/def/*)

列印的值為:

servletPath:/abc/def

contextPath:/testweb

contextPath2:/testweb

pageInfo:/ghi/test.html

uri:/testweb/abc/def/ghi/test.html

url:http://localhost:8080/testweb/abc/def/ghi/test.html

realPath:G:\java.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\testweb\

通過觀察列印結果,我們可以總結:

getServletPath():擷取能夠與“url-pattern”中比對的路徑,注意是完全比對的部分,的部分不包括。

getPathInfo():與getServletPath()擷取的路徑互補,能夠得到的是“url-pattern”中d的路徑部分

getContextPath():擷取項目的根路徑

getRequestURI:擷取根路徑到位址結尾

getRequestURL:擷取請求的位址連結(浏覽器中輸入的位址)

getServletContext().getRealPath("/"):擷取“/”在機器中的實際位址

getScheme():擷取的是使用的協定(http 或https)

getProtocol():擷取的是協定的名稱(HTTP/1.11)

getServerName():擷取的是域名(xxx.com)

getLocalName:擷取到的是IP

繼續閱讀