songzj

JSP中获取绝对路径

在JSP中为了防止因目录变更找不到要加载的CSS,js文件和Servlet程序,有三种种方式可以获取绝对路径

1.<%=request.getContextPath()%>

代码实例:

//获取WebContent/js中的demo.js文件
<script type="text/javascript" src="<%=request.getContextPath() %>/js/demo.js"></script>

 2.${pageContext.request.contextPath}

代码实例:

//获取WebContent/js中的demo.js文件
<script type="text/javascript" src="${pageContext.request.contextPath}/js/demo.js"></script>

3.JSTL标签

代码示例

//访问DemoServlet
<c:url value="/DemoServlet">

Java程序中获取文件路径

比如读取根目录下的db.properties文件,代码如下:

Properties pro = new Properties();
InputStream in = this.getClass().getClassLoader().getResourceAsStream("db.properties");
try {
            pro.load(in);
            this.driver = pro.getProperty("dataSource.driverClass");
            this.url = pro.getProperty("dataSource.jdbcUrl");
            this.username = pro.getProperty("dataSource.user");
            this.password = pro.getProperty("dataSource.password");
        }catch(Exception e) {
            e.printStackTrace();
        }

 

分类:

技术点:

相关文章:

  • 2021-11-28
  • 2021-11-27
  • 2021-12-09
  • 2021-12-09
  • 2021-08-21
  • 2021-09-24
  • 2021-11-27
  • 2021-06-18
猜你喜欢
  • 2021-11-30
  • 2021-11-27
  • 2021-08-10
  • 2021-11-27
  • 2021-12-19
  • 2021-11-27
  • 2021-11-30
相关资源
相似解决方案