자바(Java)/JAVA 2EE

서블릿의 시스템 경로 및 요청 경로 구하기

본클라쓰 2010. 4. 27. 14:03

서블릿의 시스템 경로 및 요청 경로 구하기

 

 ServletContext 인터페이스는 직접 객체를 생성할 수 없습니다. 따라서 서블릿의 경로를 알기 위해서는 요청 객체를 통해 서블릿의 정보를 얻을 수 있습니다.  다음과 같이 요청 객체를 통해 서블릿의 정보를 확인할 수 있습니다.

 

HashMap<String, String> data = new HashMap<String, String>();


data.put("requestContextPath", request.getContextPath());
data.put("requestPathInfo", request.getPathInfo());
data.put("requestServletPath", request.getServletPath());
data.put("requestRealPath", request.getRealPath("/"));
data.put("ServletContextPath", request.getSession().getServletContext().getContextPath());
data.put("ServletRealPath", request.getSession().getServletContext().getRealPath(""));
data.put("ServletInfo", request.getSession().getServletContext().getServerInfo());
  
request.setAttribute("data", data);
  

 

 

출력 페이지 

<body>
요청 컨텍스트 경로 : ${data.requestContextPath }<br />
요청 경로 정보 : ${data.requestPathInfo }<br />
요청 서블릿 경로 : ${data.requestServletPath }<br />
요청 실제 경로 : ${data.requestRealPath }<br />
서블릿 컨텍스트 경로 : ${data.ServletContextpath }<br />
서블릿 실제 경로 : ${data.ServletRealPath }<br />
서블릿 정보 : ${data.ServletInfo }
</body>

 

 

결 과 

요청 컨텍스트 경로 :
요청 경로 정보 : /test/
요청 서블릿 경로 : /service
요청 실제 경로 : D:\work\Web Project\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\project\
서블릿 컨텍스트 경로 :
서블릿 실제 경로 : D:\work\Web Project\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\project
서블릿 정보 : Apache Tomcat/6.0.20