request 中关于路径的部分方法举例解析
2008-08-06 12:13
417 查看
request 中关于路径的部分方法举例解析
我们假设这个web应用的虚拟目录为UploadAdnDownload
其下有两个文件:form.html,另一为 down.jsp, form.html中调用down.jsp
String str=request.getRealPath("/");
String str1=request.getContextPath();
String str2=request.getRequestURI();
StringBuffer str3=request.getRequestURL();
System.out.println("str="+str);
System.out.println("str1="+str1);
System.out.println("str2="+str2);
System.out.println("str3="+str3);
如上几个方法均在down.jsp中,执行后输出的内容如下:
str=C:/Documents and Settings/华禹
/jbproject/updateanddownload/UploadAdnDownload/ -----------工程全局目录
str1=/UploadAdnDownload ------------------------------工程虚拟目录
str2=/UploadAdnDownload/down.jsp----------------------------取得请求的URL
str3=http://localhost:8080/UploadAdnDownload/down.jsp-------取得请求的全局URL
request.getRealPath("url");//虚拟目录映射为实际目录
request.getRealPath("./");//网页所在的目录
request.getRealPath("../");//网页所在目录的上一层目录
-------------------------------------------------------------------------
转载自:http://tb.blog.csdn.net/TrackBack.aspx?PostId=1717528
JavaEE程序有一大路径陷阱,那就是ServletContext的getRealPath方法。我们常常使用getRealPath(“/”)来获得Web应用程序根目录的绝对路径。这是绝对要不得的!提供这个方法绝对是JavaEE API开发组的一大败笔。使用它,我们会万劫不复!
绝对不要使用ServletContext的getRealPath方法获取Web应用的路径!应该使用ServletContext的getResource()方法,直接使用相对于Web应用根目录的相对路径来获取资源。
ServletContext接口中定位资源的方法
getResource
java.net.URL getResource(java.lang.String path)throws java.net.MalformedURLException
Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.
This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.
The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.
This method returns null if no resource is mapped to the pathname.
Some containers may allow writing to the URL returned by this method using the methods of the URL class.
The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of an execution.
This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders.
Parameters:
path - a String specifying the path to the resource
Returns:
the resource located at the named path, or null if there is no resource at that path
Throws:
java.net.MalformedURLException - if the pathname is not given in the correct form
getResourceAsStream
java.io.InputStream getResourceAsStream(java.lang.String path)
Returns the resource located at the named path as an InputStream object.
The data in the InputStream can be of any type or length. The path must be specified according to the rules given in getResource. This method returns null if no resource exists at the specified path.
Meta-information such as content length and content type that is available via getResource method is lost when using this method.
The servlet container must implement the URL handlers and URLConnection objects necessary to access the resource.
This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader.
Parameters:
path - a String specifying the path to the resource
Returns:
the InputStream returned to the servlet, or null if no resource exists at the specified path
getRealPath
java.lang.String getRealPath(java.lang.String path)
Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext..
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).
Parameters:
path - a String specifying a virtual path
Returns:
a String specifying the real path, or null if the translation cannot be performed
说明
可以看到,ServletContext接口中的getResource()等方法,可以找到任何从应用程序的根目录开始的资源。包括在.war包这样的压缩文件中。参数必须以/开头。
而我们常用的getRealPath(“/”)方法,在.war包发布时,就会失效。会返回null。
因此,我们应该避免使用getRealPath(“/”)这样的方法来获取应用程序的绝对路径。
方法,URL对象可以方便的转为URI,和String对象。
尽管没有ServletContext的源码,但是我可以猜想到getResource等方法一定在底层使用了ClassLoader的getResource方法。
我们假设这个web应用的虚拟目录为UploadAdnDownload
其下有两个文件:form.html,另一为 down.jsp, form.html中调用down.jsp
String str=request.getRealPath("/");
String str1=request.getContextPath();
String str2=request.getRequestURI();
StringBuffer str3=request.getRequestURL();
System.out.println("str="+str);
System.out.println("str1="+str1);
System.out.println("str2="+str2);
System.out.println("str3="+str3);
如上几个方法均在down.jsp中,执行后输出的内容如下:
str=C:/Documents and Settings/华禹
/jbproject/updateanddownload/UploadAdnDownload/ -----------工程全局目录
str1=/UploadAdnDownload ------------------------------工程虚拟目录
str2=/UploadAdnDownload/down.jsp----------------------------取得请求的URL
str3=http://localhost:8080/UploadAdnDownload/down.jsp-------取得请求的全局URL
request.getRealPath("url");//虚拟目录映射为实际目录
request.getRealPath("./");//网页所在的目录
request.getRealPath("../");//网页所在目录的上一层目录
-------------------------------------------------------------------------
转载自:http://tb.blog.csdn.net/TrackBack.aspx?PostId=1717528
JavaEE程序有一大路径陷阱,那就是ServletContext的getRealPath方法。我们常常使用getRealPath(“/”)来获得Web应用程序根目录的绝对路径。这是绝对要不得的!提供这个方法绝对是JavaEE API开发组的一大败笔。使用它,我们会万劫不复!
绝对不要使用ServletContext的getRealPath方法获取Web应用的路径!应该使用ServletContext的getResource()方法,直接使用相对于Web应用根目录的相对路径来获取资源。
ServletContext接口中定位资源的方法
getResource
java.net.URL getResource(java.lang.String path)throws java.net.MalformedURLException
Returns a URL to the resource that is mapped to a specified path. The path must begin with a "/" and is interpreted as relative to the current context root.
This method allows the servlet container to make a resource available to servlets from any source. Resources can be located on a local or remote file system, in a database, or in a .war file.
The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.
This method returns null if no resource is mapped to the pathname.
Some containers may allow writing to the URL returned by this method using the methods of the URL class.
The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a RequestDispatcher instead to include results of an execution.
This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This method does not use class loaders.
Parameters:
path - a String specifying the path to the resource
Returns:
the resource located at the named path, or null if there is no resource at that path
Throws:
java.net.MalformedURLException - if the pathname is not given in the correct form
getResourceAsStream
java.io.InputStream getResourceAsStream(java.lang.String path)
Returns the resource located at the named path as an InputStream object.
The data in the InputStream can be of any type or length. The path must be specified according to the rules given in getResource. This method returns null if no resource exists at the specified path.
Meta-information such as content length and content type that is available via getResource method is lost when using this method.
The servlet container must implement the URL handlers and URLConnection objects necessary to access the resource.
This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet containers to make a resource available to a servlet from any location, without using a class loader.
Parameters:
path - a String specifying the path to the resource
Returns:
the InputStream returned to the servlet, or null if no resource exists at the specified path
getRealPath
java.lang.String getRealPath(java.lang.String path)
Returns a String containing the real path for a given virtual path. For example, the path "/index.html" returns the absolute file path on the server's filesystem would be served by a request for "http://host/contextPath/index.html", where contextPath is the context path of this ServletContext..
The real path returned will be in a form appropriate to the computer and operating system on which the servlet container is running, including the proper path separators. This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).
Parameters:
path - a String specifying a virtual path
Returns:
a String specifying the real path, or null if the translation cannot be performed
说明
可以看到,ServletContext接口中的getResource()等方法,可以找到任何从应用程序的根目录开始的资源。包括在.war包这样的压缩文件中。参数必须以/开头。
而我们常用的getRealPath(“/”)方法,在.war包发布时,就会失效。会返回null。
因此,我们应该避免使用getRealPath(“/”)这样的方法来获取应用程序的绝对路径。
如果你不想使用我在《Java路径问题最终解决方案—可定位所有资源的相对路径寻址》中提出的助手类ClassLoaderUtil 的public static URL getExtendResource(String relativePath)方法,那么你应该使用ServletContext接口的
java.net.URL getResource(java.lang.String path)
throws java.net.MalformedURLException
方法,URL对象可以方便的转为URI,和String对象。
尽管没有ServletContext的源码,但是我可以猜想到getResource等方法一定在底层使用了ClassLoader的getResource方法。
相关文章推荐
- HttpServletRequest的部分方法解析
- Request的关于路径的方法详解
- ASP.NET 取得 Request URL 的各个部分和通过ASP.NET获取URL地址的方法
- 基于从request获取各种路径的方法介绍
- Linux中C/C++关于include路径部分解惑
- 解决关于jquery中$.get()方法总是报“HierarchyRequestError: Node cannot be inserted at the specified point in the hierarchy”错的方法
- 关于 web应用程序建立在非默认路径【wwwroot】的方法 vs2002 vs2003 相关
- 一个DNS转发故障,造成的部分域名不能解析的解决方法
- 【Json】关于json解析时异常org.json.JSONException: A JSONObject text must begin with '{' at character 1 of {的解决方法
- 举例说明关于Android中SharedPreferences的使用方法
- php解析url的三种方法举例
- 关于利用Jsoup解析HTML中 变成非传统空格或乱码问题解决方法
- 关于解析JSON的方法
- js unicode 编码解析关于数据转换为中文的两种方法
- php.ini中文注释版 ---关于路径查找那部分很好,解决问题了。还可以看phpinfo() 信息
- 获取当前访问的部分虚拟路径(Request.Url.GetLeftPart)
- 黑马程序员-关于Collections类中fill方法的一个拓展练习(实现将list中部分元素替换)
- ${pageContext.request.contextPath} :JSP取得绝对路径方法
- 关于request获取路径的几个参数实例
- iOS 中的关于json解析的四种方法