当前位置: 首页 > news >正文

网站对接微信接口智能网站推广优化

网站对接微信接口,智能网站推广优化,征婚网站开发,做闪图的网站前言:JavaEE程序有一大路径陷阱,那就是ServletContext的getRealPath方法。我们常常使用getRealPath(“/”)来获得Web应用程序根目录的绝对路径。这是绝对要不得的!绝对不要使用ServletContext的getRealPath方法获取Web应用的路径!…

前言:JavaEE程序有一大路径陷阱,那就是ServletContext的getRealPath方法。我们常常使用getRealPath(“/”)来获得Web应用程序根目录的绝对路径。这是绝对要不得的!

绝对不要使用ServletContext的getRealPath方法获取Web应用的路径!应该使用ServletContext的getResource()方法,直接使用相对于Web应用根目录的相对路径来获取资源。

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(“/”)这样的方法来获取应用程序的绝对路径。

参考:JavaEE路径陷阱之getRealPath

http://www.lbrq.cn/news/2488861.html

相关文章:

  • 淘宝找做网站株洲企业seo优化
  • 游戏网站怎么赚钱chinaz站长素材
  • 企业网站建设的上市公司电商运营seo
  • 互联网有多少网站济南网站推广
  • wordpress投稿者用户权限seo怎么优化步骤
  • 外贸公司起名seo优化
  • 遵义做网站哪家好哪家好seo优化推广技巧
  • 网站是否必须做认证山西seo排名厂家
  • 国家企业信用网企业查询标题关键词优化报价
  • 枣庄高端网站定制厦门关键词排名提升
  • 如何利用网站做产品推广旺道网站排名优化
  • 厦门网站制作seo流量的提升的软件
  • 企业网站建设百度浏览器下载
  • 电商代运营公司怎么样长沙百度网站优化
  • 网站范例自媒体十大平台
  • 萧山做网站设计抖音优化公司
  • 网站关于我们怎么做会计培训班要多少钱一般要学多久
  • 免费自己做网站手机网络推广seo是什么
  • 上海网安网站建设网站建设定制
  • 北京公司网站制作微信软文范例100字
  • 网站ftp根目录电商运营去哪里学比较好
  • 网站CDN怎么做防攻击吗电子商务网站开发
  • 管理登陆网站开发软件谷歌网站推广
  • 狗和女主人做爰网站百度标注平台怎么加入
  • 设计师互动平台完善的seo网站
  • wordpress和域名seo关键字优化软件
  • 响应式网站应该怎么做麒麟seo软件
  • 做网站去哪个公司什么是seo文章
  • 那些免费网站做推广比较好seo推广营销公司
  • 网站的域名在哪里看b站24小时自助下单平台网站
  • OpenCv中的 KNN 算法实现手写数字的识别
  • k8s pod生命周期、初始化容器、钩子函数、容器探测、重启策略
  • 嵌入式——单片机的独立按键
  • 【Linux系统编程】环境变量,进程地址空间与进程控制
  • CSP2025模拟赛2(2025.7.26)
  • CS231n-2017 Lecture7训练神经网络(二)笔记