web获取路径

来源:互联网 发布:新加坡国家顶级域名 编辑:程序博客网 时间:2024/05/16 12:54

我访问的是http://localhost:8080/bottleBlog/useless/path.jsp这个网址,这是如下API得到的结果:

<h3>    getServletPath=<%= request.getServletPath()%><br>    getContextPath=<%= request.getContextPath()%><br><!-- The method getRealPath(String) from the type ServletRequest is deprecated -->    <%--    getRealPath=<%= request.getRealPath("/")%><br> --%>    getRealPath=<%= request.getServletContext().getRealPath("/")%><br>     getServletContext=<%= request.getServletContext()%><br>    getRequestURI=<%= request.getRequestURI()%><br>    getRequestURL=<%= request.getRequestURL()%><br>    </h3>
getServletPath=/useless/path.jspgetContextPath=/bottleBloggetRealPath=D:\JavaEEWorkSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\bottleBloggetServletContext=org.apache.catalina.core.ApplicationContextFacade@4a272157getRequestURI=/bottleBlog/useless/path.jspgetRequestURL=http://localhost:8080/bottleBlog/useless/path.jsp

其中URI和URL的区别可以见传送门

其余的几个API的话,我自己是讲不清的,所以还是上API文档吧。

String javax.servlet.http.HttpServletRequest.getServletPath()Returns:    a String containing the name or path of the servlet being called, as specified in the request URL, decoded, or an empty string if the servlet used to process the request is matched using the "/*" pattern.

返回当前URL请求调用servlet的一部分。

String javax.servlet.http.HttpServletRequest.getContextPath()Returns:    a String specifying the portion of the request URI that indicates the context of the request

指定请求URI的一部分的字符串,表示请求的上下文。

String javax.servlet.ServletContext.getRealPath(String path)Returns a String containing the real path for a given virtual path. 

返回给出的虚拟路径的真实路径。

1 0
原创粉丝点击