博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA获取服务器路径的方法
阅读量:7095 次
发布时间:2019-06-28

本文共 1265 字,大约阅读时间需要 4 分钟。

1、在JSF环境中获取到ServletContext:

1
2
ServletContext sc = (ServletContext)FacesContext.
        
getCurrentInstance().getExternalContext().getContext();

2、servlet中获得项目绝对路径

1
2
String filePath=
this
.getServletConfig().
        
getServletContext().getRealPath(
"/"
);

根目录所对应的绝对路径
request.getServletPath();
文件的绝对路径
request.getSession().getServletContext().getRealPath(request.getRequestURI())
当前web应用的绝对路径
servletConfig.getServletContext().getRealPath(“/”);
3、jsp中获取服务器路径

1
2
3
4
5
String contextPath = request.getContextPath();   
String realPath = request.getSession().
                
getServletContext().getRealPath(
"/"
);   
String basePath = request.getScheme()+
"://"
+request.getServerName()+
":"
+
                
request.getServerPort()+contextPath+
"/"
;

说明:

contextPath =”/项目名称”; //获取的是项目的相对路径
realPath = F:\tomcat_home\webapps\项目名称\ //获取的是项目的绝对路径
basePath = http://localhost:8080/项目名称/ //获取的是服务的访问地址
4、ServletContext对象获得几种方式

1
2
3
Javax.servlet.http.HttpSession.getServletContext()
Javax.servlet.jsp.PageContext.getServletContext()
Javax.servlet.ServletConfig.getServletContext()

文件的绝对路径

request.getSession().getServletContext().getRealPath(request.getRequestURI())
当前web应用的绝对路径
servletConfig.getServletContext().getRealPath(“/”);

除非注明,文章为原创,欢迎转载!转载请注明本文地址,谢谢。

转载于:https://www.cnblogs.com/zhoushihui/p/5546452.html

你可能感兴趣的文章
用户态和内核态
查看>>
VR+生物plus 遐想
查看>>
Java并发编程:线程控制
查看>>
今天聊一聊Java引用类型的强制类型转换
查看>>
把数据保存到数据库archives表时出错,请检查
查看>>
JavaSE基本语法、数据类型、操作符等:int、long、Integer、Long、if、else、for、while...
查看>>
解析XML文件
查看>>
牛客练习赛46
查看>>
netty线程模型
查看>>
Codeforces Round #237 Div.2 A
查看>>
initrd.gz的解压和制作
查看>>
LeetCode:Edit Distance(字符串编辑距离DP)
查看>>
设计流程及工具记录
查看>>
关于CDialogBar的编程
查看>>
吹きすさぶ风の中で
查看>>
对象引用前加const 报错
查看>>
linux 0.11 源码学习(十一)
查看>>
编码风格——linux内核开发的coding style
查看>>
表格隔行变色案例
查看>>
IOS 模拟不同网络环境 - Network Link Conditioner
查看>>