HttpServletRequest에서 getRequestURI와 getPathInfo 메소드의 차이점은 무엇입니까?
간단하고 매우 가벼운 프론트 컨트롤러를 만들고 있습니다. 올바른 것을 선택하려면 요청 경로를 다른 처리기 (작업)와 일치시켜야합니다.
내 로컬 컴퓨터에서 HttpServletRequest.getPathInfo()
와 HttpServletRequest.getRequestURI()
동일한 결과를 반환합니다. 그러나 그들이 프로덕션 환경에서 무엇을 반환할지 잘 모르겠습니다.
그렇다면이 방법의 차이점과 무엇을 선택해야합니까?
getPathInfo()
URI 뒤에 추가 경로 정보를 getRequestURI()
제공하고 서블릿에 액세스하는 데 사용되는 완전한 경로를 제공합니다.
서블릿이 먼저 고유 한 URI 패턴으로 구성되어야한다는 점에서 그것들이 다르다고 생각했을 것이다. 루트 (/)에서 서블릿을 제공 한 적이 없다고 생각합니다.
예를 들어 서블릿 'Foo'가 URI '/ foo'에 매핑되면 URI를 생각했을 것입니다.
/foo/path/to/resource
결과는 다음과 같습니다.
RequestURI = /foo/path/to/resource
과
PathInfo = /path/to/resource
여기에 작은 비교 테이블을 넣을 것입니다 (어딘가에 갖기 위해).
서블릿은로 매핑되고 /test%3F/*
응용 프로그램은 아래에 배포됩니다 /app
.
http://30thh.loc:8480/app/test%3F/a%3F+b;jsessionid=S%3F+ID?p+1=c+d&p+2=e+f#a
Method URL-Decoded Result
----------------------------------------------------
getContextPath() no /app
getLocalAddr() 127.0.0.1
getLocalName() 30thh.loc
getLocalPort() 8480
getMethod() GET
getPathInfo() yes /a?+b
getProtocol() HTTP/1.1
getQueryString() no p+1=c+d&p+2=e+f
getRequestedSessionId() no S%3F+ID
getRequestURI() no /app/test%3F/a%3F+b;jsessionid=S+ID
getRequestURL() no http://30thh.loc:8480/app/test%3F/a%3F+b;jsessionid=S+ID
getScheme() http
getServerName() 30thh.loc
getServerPort() 8480
getServletPath() yes /test?
getParameterNames() yes [p 2, p 1]
getParameter("p 1") yes c d
위의 예에서 서버는에서 실행 중이며 localhost:8480
이름 30thh.loc
은 OS hosts
파일에 저장되었습니다.
코멘트
"+"는 쿼리 문자열에서 공백으로 만 처리됩니다.
"#a"앵커는 서버로 전송되지 않습니다. 브라우저 만 사용할 수 있습니다.
경우
url-pattern
서블릿 매핑은 끝나지 않습니다*
(예 :/test
또는*.jsp
)getPathInfo()
반환null
.
Spring MVC를 사용하는 경우
메소드가를
getPathInfo()
반환합니다null
.메소드
getServletPath()
는 컨텍스트 경로와 세션 ID 사이의 부분을 리턴합니다. 위의 예에서 값은/test?/a?+b
의 URL 인코딩 된 부분에주의
@RequestMapping
하고@RequestParam
봄입니다. 버그가 많으며 (현재 버전 3.2.4) 일반적 으로 예상대로 작동하지 않습니다 .
클라이언트가 주소 표시 줄에 입력하여 서블릿에 도달하는 전체 URL을 분류 해 보겠습니다.
http://www.example.com:80/awesome-application/path/to/servlet/path/info?a=1&b=2#boo
부품은 다음과 같습니다.
- 계획:
http
- 호스트 이름 :
www.example.com
- 포트:
80
- 컨텍스트 경로 :
awesome-application
- 서블릿 경로 :
path/to/servlet
- 경로 정보 :
path/info
- 질문:
a=1&b=2
- 파편:
boo
요청 URI ( getRequestURI 로 반환 )는 파트 4, 5 및 6에 해당합니다.
(incidentally, even though you're not asking for this, the method getRequestURL would give you parts 1, 2, 3, 4, 5 and 6).
Now:
- part 4 (the context path) is used to select your particular application out of many other applications that may be running in the server
- part 5 (the servlet path) is used to select a particular servlet out of many other servlets that may be bundled in your application's WAR
- part 6 (the path info) is interpreted by your servlet's logic (e.g. it may point to some resource controlled by your servlet).
- part 7 (the query) is also made available to your servlet using getQueryString
- part 8 (the fragment) is not even sent to the server and is relevant and known only to the client
The following always holds (except for URL encoding differences):
requestURI = contextPath + servletPath + pathInfo
The following example from the Servlet 3.0 specification is very helpful:
Note: image follows, I don't have the time to recreate in HTML:
Consider the following servlet conf:
<servlet>
<servlet-name>NewServlet</servlet-name>
<servlet-class>NewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>NewServlet</servlet-name>
<url-pattern>/NewServlet/*</url-pattern>
</servlet-mapping>
Now, when I hit the URL http://localhost:8084/JSPTemp1/NewServlet/jhi
, it will invoke NewServlet
as it is mapped with the pattern described above.
Here:
getRequestURI() = /JSPTemp1/NewServlet/jhi
getPathInfo() = /jhi
We have those ones:
getPathInfo()
returns
a String, decoded by the web container, specifying extra path information that comes after the servlet path but before the query string in the request URL; or null if the URL does not have any extra path informationgetRequestURI()
returns
a String containing the part of the URL from the protocol name up to the query string
'Programing' 카테고리의 다른 글
macOS High Sierra에서 작동하지 않는 CocoaPod (0) | 2020.06.29 |
---|---|
ListView가 비어있을 때 빈보기 표시 (0) | 2020.06.29 |
Java가 10에서 99까지의 모든 숫자의 곱이 0이라고 생각하는 이유는 무엇입니까? (0) | 2020.06.29 |
LogCat에 필터 옵션이 없습니다. (0) | 2020.06.29 |
Emacs Ruby 자동 완성 거의 작동 (0) | 2020.06.29 |