servlet和jsp的区别_获取servletcontext

servlet和jsp的区别_获取servletcontext关于serveletContext.getRealPath()方法1.关于request.getRealPath问题:Stringfilename=request.getRealPath(filename)——————-信息:warning:[deprecation]getRealPath(java.lang.String)in

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

关于serveletContext.getRealPath()方法

1.关于request.getRealPath
问题:
String filename=request.getRealPath(filename)
——————-
信息:
warning: [deprecation] getRealPath(java.lang.String) in javax.servlet.ServletRequest has been deprecated

解决:
这个getRealPath方法已经不建议使用了

参看request.getRealPath的java doc:
Deprecated. As of Version 2.1 of the Java Servlet API, use ServletContext.getRealPath(java.lang.String) instead.


而在servlet中使用getServletContext.getRealPath()这个方法受到war 和non-war的影响,以及不同app server实现的影响,运气好的话,你常常会得到null,嘿嘿,比如你在weblogic上部署war文件,又调用这个方法..

推荐ServletContext.getResourceAsStream


2.关于serveletContext.getRealPath返回NULL和不同的app server返回不同的结果

问题:

有几个配置文本配置文件(是一些报表的模板),放在WEB-INF下面的config目录下,程序中是这样得到这个config的实际路径的:
先用 serveletContext.getRealPath得到根路径,tomcat中比如是
c:\tomcat\webapp\test

然后我加上 “/WEB-INF/config/aa.config”,这样得到文件的path然后进行读入,应用在tomcat上跑是ok的,后来将war放到weblogic上,出错,原因是:
在weblogic上用getRealPath得到的是像
myserver\stage\_appsdir_test_war\test.war!\WEB-INF\config….
这样的路径,于是一直报FileNotFoundException

解决:

serveletContext.getRealPath
这个方法在不同的服务器上所获得的实现是不一样的, 建议是通过classloader来获得你配置的资源文件

context.getRealPath(“/”)可能返回了null,你可以输入来看看,
对一个打包的应用来说,是没有RealPath的概念的,调用getRealPath只会简单地返回null。其实,也很

好理解,一个文件被打包入了.war文件,就不存在目录结构了(虽然包中仍然存在目录结构,但这不等同于文件系统中的目录结构)。所以,对war包中的资源是无法得到RealPath的。这样也就无从通过文件IO进行读取了。

那么,如何读取war包中的资源呢?答案是使用:
ServletContext.getResourceAsStream(“/WEB-INF/config/aa.config”)方法。


原则:基本上就是尽量使用j2ee规范中的各层次classloader来获取资源,而不是试图去找文件的绝对路
方法:调用this.getClass().getClassLoader().getResource(“/”).getPath(); 获取到classes目录的全路径

使用:在得到classes目录的全路径后再根据字符串的截取与拼装达到你的要求即可。



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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191887.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • js中find的用法_js中find函数

    js中find的用法_js中find函数首先简单的介绍一下ES6是什么,可能很多人还是第一次听说,我们都知道H5是html的新一代的标准,同样,ES6是javascript的新一代标准,全称是ECMAScript6.0,简称ES6,其实不是什么神秘的东西。15年6月发布的。今天我们要说的是结合ES6新特性谈一下js里面的一个很好用的方法-find()现在的前端和过去的不一样,过去的前端只要会画页面就行了,但是现在仅仅会画页面已…

    2022年10月14日
    2
  • PLC基础知识(PLC入门必看)[通俗易懂]

    PLC基础知识(PLC入门必看)[通俗易懂]1 PLC的发展历程在工业生产过程中,大量的开关量顺序控制,它按照逻辑条件进行顺序动作,并按照逻辑关系进行连锁保护动作的控制,及大量离散量的数据采集。传统上,这些功能是通过气动或电气控制系统来实现的。1968年美国GM(通用汽车)公司提出取代继电气控制装置的要求,第二年,美国数字公司研制出了基于集成电路和电子技术的控制装置,首次采用程序化的手段应用于电气控制,这就是第一代可编程序控制器,称ProgrammableController(PC)。个人计算机(简称PC)发展起来后,为了方便,也为了反映可

    2025年8月28日
    11
  • 什么是词向量?(NPL入门)

    什么是词向量?(NPL入门)什么是词向量?我们组实训选择的主题是与自然语言识别相关的,那么就不得不学习和了解一下自然语言识别中非常重要和基础的。于是我对于自己对词向量的学习进行了以下的总结。简而言之,词向量技术是将词转化成为稠密向量,并且对于相似的词,其对应的词向量也相近。一、词的表示在自然语言处理任务中,首先需要考虑词如何在计算机中表示。通常,有两种表示方式:one-hotrepresenta…

    2022年6月14日
    43
  • sql语句大全(详细)

    sql语句大全(详细)数据库操作查看所有数据库showdatabases;查看当前使用的数据库selectdatabase();创建数据库createdatabases数据库名charset=utf8;5.删除数据库dropdatabase数据库名6.使用数据句库usedatabase数据库名7.查看数据库中所有表showtables;表的操作1…

    2022年4月30日
    36
  • 分析型数据库 AnalyticDB学习 —-基本介绍

    分析型数据库 AnalyticDB学习 —-基本介绍分析型数据库AnalyticDB学习—-基本介绍AnalyticDB简介阿里巴巴自主研发的海量数据实时并发在线分析的云计算服务,可以在毫秒级针对千亿级数据进行多维分析和业务探索.具备海量数据的自由计算和极速响应能力(数据很多,反应很快,计算很快,可以处理高并发这个意思)Analytic核心功能和特点*Analytic核心功能(1) 分档的储存(2) 自由的查询(3) …

    2022年9月17日
    3
  • python django做网页_响应式网页怎么做啊

    python django做网页_响应式网页怎么做啊这篇文字适合刚学习Django的同学,如果比较熟的就不用看了。以下都是讲在windows上的部署情况;准备:1、python3.62、pycharm profession(专业版)3、安装Django模块以上的安装就不讲了,比较简单,网上也有很多教程。都去官网下载安装即可。 前言:学习django框架其实就是学习它的文件目录,目录下有一些必须的模块和包,当然你也…

    2022年10月13日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号