关于web工程中java类如何获取webapp路径下的文件
2015-11-18 23:42
543 查看
1.需要创建一个servlet继承ServletContextListener监听器
2.web.xml配置监听器,
<listener>
<listener-class>com.stee.configurationmanagement.init.TestWebPath</listener-class>
</listener>
3.如果需要servlet在web容器启动的时候就实例化,可以配置<load-on-startup>1</load-on-startup>,大于0.
4.监听器在servlet实例化的时候会调用contextInitialized(ServletContextEvent servletContextEvent)方法,然后在这个方法中使用这个方法的参数即可获取webapp路径
String webRootPath = servletContextEvent.getServletContext()
.getRealPath("/");
System.out.println(webRootPath);
if (webRootPath == null) {
try {
webRootPath = servletContextEvent.getServletContext()
.getResource("/").getFile();
System.out.println(webRootPath);
} catch (MalformedURLException e) {
LoggerUtil.logger.info(("获取WEBROOT失败!"));
}
}
2.web.xml配置监听器,
<listener>
<listener-class>com.stee.configurationmanagement.init.TestWebPath</listener-class>
</listener>
3.如果需要servlet在web容器启动的时候就实例化,可以配置<load-on-startup>1</load-on-startup>,大于0.
4.监听器在servlet实例化的时候会调用contextInitialized(ServletContextEvent servletContextEvent)方法,然后在这个方法中使用这个方法的参数即可获取webapp路径
String webRootPath = servletContextEvent.getServletContext()
.getRealPath("/");
System.out.println(webRootPath);
if (webRootPath == null) {
try {
webRootPath = servletContextEvent.getServletContext()
.getResource("/").getFile();
System.out.println(webRootPath);
} catch (MalformedURLException e) {
LoggerUtil.logger.info(("获取WEBROOT失败!"));
}
}
相关文章推荐
- Eclipse+Maven创建webapp项目<一>
- webapp font-size解决问题的方案
- PhoneGap创建的安卓WebApp项目如何去除全屏,显示手机顶部状态栏
- 移动WebApp之iScroll插件的使用介绍
- WebApp网站有关meta标签
- [Bug] 未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets”
- jasperreport学习官方例子:webapp
- 移动开发webapp开发常用meta设置手机浏览器全屏模式,webappmeta
- myeclipse部署maven时,src/main/resources里面配置文件加载不到webapp下classes路径下的问题 分类: Maven Myeclipse 2014-07-11 1
- myeclipse部署maven时,src/main/resources里面配置文件加载不到webapp下classes路径下的问题
- spring WebApplicationInitializer
- 移动webAPP前端开发技巧汇总2
- 移动webAPP前端开发技巧汇总
- webapp通过JS代码与移动APP交互
- 使用spring容器时,服务器不能顺利启动信息: Initializing Spring root WebApplicationContext log4j:WARN No appenders cou
- WebApp之基础知识
- Spring中的WebAppRootListener
- hybridapp/webapp的前端UI框架推荐
- WebApp的前端所遇问题
- webapp,ios Safari打开新窗口