關於J2EE裏面getContextPath()和getRealPath()的區別
阿新 • • 發佈:2017-08-07
ava tex 獲得 .cn iter() com 取地址 字符 關於
一直老搞不清楚這兩個方法的區別,只知道他們都是拿來獲取地址的。今天特意寫了個小demo試了一下,代碼如下:
@Override protected void service(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { ServletContext context=arg0.getServletContext(); String contextPath=context.getContextPath(); String realPath=context.getRealPath("hahha"); arg1.getWriter().write(contextPath+"\n"); arg1.getWriter().write(realPath+"\n"); }
具體的運行的結果如下:
可以理解為getContextPath()獲得的是整個項目的路徑位置
而getRealPath()獲得的是項目在系統上的真正位置,而且包含了後面的字符串,即使系統上沒有此目錄
關於J2EE裏面getContextPath()和getRealPath()的區別