现在的位置: 首页 > 综合 > 正文

JSP 中路径的获得

2012年08月17日 ⁄ 综合 ⁄ 共 1152字 ⁄ 字号 评论关闭

如果地址为 http://localhost:8080/deviantART/test/MyJsp.jsp

request.getContextPath()                    /deviantART 
    获得容器上下文路径,一般为网站的相对路径(项目名)。如果为根目录,则返回值为 ""。

request.getServletPath()                    /test/MyJsp.jsp
    获得 Servlet 相对路径。

request.getRequestURI()                  /deviantART/test/MyJsp.jsp 
    获得请求路径。

request.getRealPath("")                                D:\Program Files\apache-tomcat-6.0.16\webapps\deviantART
request.getRealPath("/AAAA/BBBB")          D:\Program Files\apache-tomcat-6.0.16\webapps\deviantART\AAAA\BBBB 
    获得绝对路径(不推荐使用)。

########################################################

application.getContextPath()                        /deviantART

application.getRealPath("")                          D:\Program Files\apache-tomcat-6.0.16\webapps\deviantART
application.getRealPath("AA/BB")               D:\Program Files\apache-tomcat-6.0.16\webapps\deviantART\AA\BB

application.getRealPath(request.getServletPath())
                                        D:\Program Files\apache-tomcat-6.0.16\webapps\deviantART\test\MyJsp.jsp

##########################################################
replaceAll("\\\\","xxxx");                     表示一个"\"

抱歉!评论已关闭.