在編寫JSP/Servlet應用時常常需要從傳入的鏈接中提取一些信息。HttpServletRequest提供了多個方法,這些方法之間容易混淆。總結并舉例如下,希望能對大家有所幫助。
舉例:
http://localhost:7001/myservlet/somepath/test?someparam=somevalue
request.getPathInfo():返回/somepath/test
request.getRequestURL():返回http://localhost:7001/myservlet/somepath/test
request.getRequestURI():返回/myservlet/somepath/test
request.getServletPath():返回/myservlet
request.getQueryString():返回someparam=somevalue
舉例:
http://localhost:7001/myservlet/somepath/test?someparam=somevalue
request.getPathInfo():返回/somepath/test
request.getRequestURL():返回http://localhost:7001/myservlet/somepath/test
request.getRequestURI():返回/myservlet/somepath/test
request.getServletPath():返回/myservlet
request.getQueryString():返回someparam=somevalue