본문 바로가기

JSP

HttpServletRequest 클래스의 메소드 - request.getContextPath() ,request.getRequestURI() 등

request.getContextPath()  = 프로젝트 Path (컨텍스트 path) 만 가져옵니다.
예)  http://localhost:8080/project/list.jsp
[return] /project 

request.getRequestURI() = 프로젝트 + 파일경로까지 가져옵니다.
예)  http://localhost:8080/project/list.jsp
[return] /project/list.jsp  

String url = request.getRequestURI.split("/");
String Name = url[url.length -1];       // list.jsp 반환

request.getRequestURL() = 전체 경로를 가져옵니다. (L만 바뀜)
예) http://localhost:8080/project/list.jsp
[return]  http://localhost:8080/project/list.jsp

request.ServletPath() = 파일명만 가져옵니다.
예) http://localhost:8080/project/list.jsp
[return] /list.jsp

request.getRealPath("") = 서버 or 로컬 웹 애플리케이션 절대경로 가져옵니다.
예) http://localhost:8080/projectname/list.jsp
[return] c:\project\webapps\projectname\

request.getScheme() = 사용하는 요청 프로토콜을 가져옵니다. https, http, ftp ...

예) http://localhost:8080/project/list.jsp

[return] http

 

request.getServerName() = 서버이름

[return] localhost

 

request.getServerPort() = 서버포트

[return] 8080

 

 

 

도메인을 가져오는 함수 (전체 주소 반환)

public static String getDomain(HttpServletRequest req) {
        StringBuffer sbDomain = new StringBuffer(); 
        sbDomain.append(req.getScheme()).append("://").append(
        		req.getServerName()).append(":").append(
        				req.getServerPort()).append(req.getContextPath());
        return sbDomain.toString(); //  http://localhost:8080/project/list.jsp 반환
    }