ILoveCoffee, ILoveJava/JSP

getRequestURL jsp / servlet / java 현재 경로 알아내기

jeeyong 2012. 5. 31. 09:51

Context : <%= request.getContextPath() %> <BR>

   ex) http://localhost:8080/board/list.jsp

        return => /board ( 프로젝트 context path 만 가져온다 )

   El(표현언어) 사용시 : ${pageContext.request.contextPath}


URL : <%= request.getRequestURL() %> <BR> 
URI : <%= request.getRequestURI() %> <BR>

   ex) http://localhost:8080/board/list.jsp

        return => /board/list.jsp ( 프로젝트 context path와 파일 경로까지 가져온다 )
Path : <%= request.getServletPath() %> <BR> 

전달 파라미터 : <%=request.getQueryString()%>

전달 파라미터 Map으로 return : <%=reqeust.getParameterMap()%>

 


String u = javax.servlet.http.HttpUtils.getRequestURL(request).toString();



javax.servlet.ServletRequest 를 참조하시면 될 것 같습니다.

 

그 중에 getParameterNames()를 이용해서 parameter명을 Enumeration 으로 저장한 후

Enumeration 내 값을 루프문을 이용해서 추출 하면서 getParameter 메소드를 이용해서 추출 하시면 됩니다.

 

StringBuffer sb = new StringBuffer();

for (Enumeration en = request.getParameterNames(); en.hasMoreElements(); ){

    String key = (String)en.nextElement();

    sb.append ( key + "=" + request.getParameter(key) + "&");

String paramStr = sb.toString();

paramStr = paramStr.substring(0, paramStr.length()-1);

 

 

현재 페이지를 가져올 경우

 

Tomcat 5.5 이후 forward 된 jsp 에서 request.getRequestURL() 의 값이 이상해진 경우

 

forward 된 jsp 페이지에서 최초의 브라우저 또는 클라이언트 프로그램으로 부터 호출될 때 사용되었던 URL을 구하기 위해 request.getRequestURL() 이나 request.getRequestURI() 를 사용했던 페이지들 중 tomcat 을 5.0에서 5.5 로 업그레이드 한 이후에 출력값이 달라지는 문제가 발생함을 경험하신 분들이 계실겁니다.

이는 Tomcat 5.5.7 버전부터 getRequestURL() 의 구현이 바뀌었기 때문인데(bug fix), 더이상 최초의 URL 을 넘겨주지 않고 forward 된 jsp 페이지의 URL을 넘겨주도록 되었습니다.

해결책부터 적는다면 5.5.7 이후부터는 getRequestURL() 대신에

request.getAttribute("javax.servlet.forward.request_uri"); 


를 사용하면 됩니다.

좀 더 자세한 내용은 wiki 에 작성해 둔 http://www.potatosoft.com/wiki/wiki.php/TOMCAT5#s-9  을 참고하시기 바랍니다.

 

 

하나더 include 된 페이지 uri가져올경우

<%= request.getAttribute( "javax.servlet.include.request_uri" ) %>

를 사용


추가로

이렇게 5개 항목이 있음.

forward 와 include 2개가 있음.


request.getAttribute("javax.servlet.include.query_string")

request.getAttribute("javax.servlet.include.path_info")

request.getAttribute("javax.servlet.include.servlet_path")

request.getAttribute("javax.servlet.include.context_path")

request.getAttribute("javax.servlet.include.request_uri")

 

 

======================================================================================================================

Servlet

 

절대경로

getServletContext().getRealPath("/")); // 웹서버의 Document Root

    ex) getServletContext().getRealPath("/WEB-INF/web.xml"));

 

or

 

config.getServletContext().getRealPath("/");

 

or

 

getServletConfig().getServletContext().getRealPath("/");

 

or

 

request.getSession().getServletContext().getRealPath("/");

 

 

======================================================================================================================

Java

 

절대경로

this.getClass().getResource("").getPath(); // 현재 자신의 절대 경로
this.getClass().getResource("/").getPath(); // classes 폴더의 최상위 경로
this.getClass().getResource("/com/test/config/config.properties").getPath(); // classes 폴더에서부터 시작하여 해당파일까지의 절대 경로
this.getClass().getProtectionDomain().getCodeSource().getLocation(); // 자신의 파일명(*.class)이 포함된 절대경로
 
System.getProperty("user.home"); // 사용자 홈 디렉토리
System.getProperty("user.dir");  // 이클립스에서 실행시 이클립스 워크스페이스 (해당 프로젝트명 전까지)
 
 
ClassLoader 사용법
ClassLoader classLoader = (Thread.currentThread()).getContextClassLoader();
if(classLoader==null) classLoader = ClassLoader.getSystemClassLoader();
URL url = classLoader.getResource("struts.properties");
System.out.println(url.getPath());
WEB-INF/classes 에 있는 리소스만 참조한다.
WEB-INF 와 같이 바로 아래에 있는 있는 리소스는 참조하지 못한다.
getSystemClassLoader는 java application에서 사용되는 방법이고
(Thread.currentThread()).getContextClassLoader() 는 web에서 사용되는 방법임.
 
현재 클래스가 상속되는(부모) 클래스라면 클래스명.class.getResource 로 해야 한다.
getClass()는 실행되는 현재 자신(자식클래스가 될 수 있음)을 가리키기 때문이다.
WEB의 절대경로와 다르다..

 

new File("").getAbsolutePath() : 절대경로

new File("").getCanonicalPath() : 상대경로


출처: http://blog.naver.com/tyboss?Redirect=Log&logNo=70023317991