getrealpathgetrealpath弃用
本文目录一览:
- 1、javaWeb中 request.getRealPath("") 这个方法为什么不推荐使用了 ?
- 2、getContextPath和getRealPath的区别
- 3、如何通过全路径获取 realpath
- 4、getRealPath("/")函数中的"/"表示什么意思?
- 5、getRealPath("/")和getContextPath()的区别
- 6、Jsp中applicatiON.getRealPath()问题
javaWeb中 request.getRealPath("") 这个方法为什么不推荐使用了 ?
request.getRealPath("") 就是取得你当前运行文件在服务器上的绝对路径.
request.getRealPath("")灵活性太差,只能得到当前文件绝对路径,不能在当前文件下获得其他文件的绝对路径
不是工程的物理路径封装在Session里 是工程的路径被封装在了ServletContext中
只是我们可以通过session对象获得这个ServletContext
获得ServletContext的方法:
1.FilterConfig的getServletContext();
2.ServletConfig的getServletContext();
3.ServletContextEvent的getServletContext()
4.HttpSession的getServletContext();
getContextPath和getRealPath的区别
实验一下就出来了,其实主要区别就是相对路径和绝对路径:
getContextPath返回的是相对路径,工程的项目的相对路径;
getRealPath返回的绝对路径,就是在文件系统的实际路径;
下面是我自己做的实验,在SpringMvc+Spring+Hibernate的项目中做的实验
@Controller
public class IndexController {
@RequestMapping("/")
public String index(HttpServletRequest request){
System.out.println(request.getContextPath());
System.out.println(request.getSession().getServletContext().getContextPath());
System.out.println(request.getServletContext().getContextPath());
System.out.println(request.getServletContext().getRealPath("/"));
System.out.println(request.getSession().getServletContext().getContextPath());
System.out.println(request.getSession().getServletContext().getRealPath("/"));
return "index";
}
}
上面的打印结果如下:
/cn.test
/cn.test
/cn.test
E:\StudyResource\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\cn.test\
/cn.test
E:\StudyResource\Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp3\wtpwebapps\cn.test\
其实request、session、ServletContext调用getContextPath返回的结果是相同的
如何通过全路径获取 realpath
根目录所对应的绝对路径:request.getServletPath();
文件的绝对路径 :request.getSession().getServletContext().getRealPath
(request.getRequestURI())
当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath(”/”);
(ServletContext对象获得几种方式:
javax.servlet.http.HttpSession.getServletContext()
javax.servlet.jsp.PageContext.getServletContext()
javax.servlet.ServletConfig.getServletContext()
getRealPath("/")函数中的"/"表示什么意思?
1、表示获得服务器的绝对路径的意思。
2、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
3、关于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。
getRealPath("/")和getContextPath()的区别
1.getRealPath("/")
@RequestMapping(path="/test")
public String test1(HttpServletRequest req) {
//使用fileupload组件进行文件上传
//指定文件上传的位置
String path = req.getSession().getServletContext().getRealPath("/");
String path1 = req.getSession().getServletContext().getRealPath("/WEB-INF/pages");
String path2 = req.getSession().getServletContext().getContextPath();
System.out.println(path);
System.out.println(path1);
System.out.println(path2);
return "success";
}
控制台返回:
D:\IDEAProject\springmvcdemo\src\main\webapp\
D:\IDEAProject\springmvcdemo\src\main\webapp\WEB-INF\pages
/springmvcdemo
总结:.getRealPath("/")方法返回的是项目在服务器的绝对路径,而getRealPath("/WEB-INF/pages")返回的是包含一个给定虚拟路径的绝对路径,其中/pages是虚拟的。
2.getContextPath()方法返回的是项目相对路径,并不会返回其绝对的路径,就是说并不知道项目在哪个位置。
Jsp中application.getRealPath()问题
你显示结果当然是上面的那个了。因为你给path赋值是这样
这段字符串,所以打印出来的结果是:
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\fio\http:\localhost:8080\fio\liuyan1.jsp
你应该这样修改就可以了
String path ="liuyan1.jsp"
out.print("取得的文件实际路径:");
out.print(application.getRealPath(path)+"br");
这样打印出来就是你要的结果了。下面的取路径代码你可以参考一下:
工程名为TEST为例:
(1)得到包含工程名的当前页面全路径:request.getRequestURI()
结果:/TEST/test.jsp
(2)得到工程名:request.getContextPath()
结果:/TEST
(3)得到当前页面所在目录下全名称:request.getServletPath()
结果:如果页面在jsp目录下 /TEST/jsp/test.jsp
(4)得到页面所在服务器的全路径:application.getRealPath("页面.jsp")
结果:D:\resin\webapps\TEST\test.jsp
(5)得到页面所在服务器的绝对路径:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent();
结果:D:\resin\webapps\TEST
getrealpath的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于getrealpath弃用、getrealpath的信息别忘了在万域城进行查找喔。
相关文章
发表评论
评论列表
- 这篇文章还没有收到评论,赶紧来抢沙发吧~