java absolute_Java's absolute and relative path

在java中文件目录中带“/”的是绝对路径,不带“/”的是相对路径。

请求页面的写法,在web中,页面路径主要写的有以下几种

1、重定向

2、转发

3、超链接

4、form表单提交的action

5、servlet web.xml中的配置参数的路径

6、properties类型的文件

本文以项目部署在tomcat服务器为例,其他相信也是一样的。

先说明,一定要考虑的问题:

1、路径是谁发起的(浏览器,服务器)

2、路径开头直接 “/” 表示绝对路径

例如

7a5b786c20dbfeb39cb096581adbcb68.png

项目的根目录是指FirstWEB下,即http://localhost:8080/FirstWEB,此时由浏览器发起的请求,代码中的“/”表示的当前路径就应该表示的是这个根目录

服务器的根目录是指wptwebapps下,即 localhost:8080,

(1)重定向是是由浏览器发起的,所以“/”代表的是服务器根目录

(2)转发是由服务器发起的,所以“/”代表的是项目根目录

(3、4) 前台页面不论时等标签,所有的绝对路径,“/”都代表服务器根目录

(5)我们在编写项目的web.xml中编写servlet映射时,所有的servlet前都要加“/”,这个“/”指的是服务器根目录

(6)properties文件不能直接放置在项目根目录下,因为项目编译时,只会编译src下的文件和webcontent下的文件,properties文件就会被忽略

因此,放置properties文件时 ,要么放在src文件目录下,要么放置在webcontent目录下,由于编译后java文件放置在web-inf的下的classes下,所以建议把properties文件就放置在src下,跟着程序的编译一起编译,这样保证查找文件时,不会路径出错。

此时,通过classes来找资源 就是通过类加载器来加载文件:

//获取jdbc.properties 需要在类路径下找

//类加载器获取资源会在类路径下找(WEB-INF/classes)

ClassLoader loader = JDBCUtils.class.getClassLoader();

InputStream stream = loader.getResourceAsStream("jdbc.properties");

为了进行静态文件与源码分离,可以创建一个名为conf的资源文件夹,这个文件夹下的内容会进行分类,但不编译:

d9a2c70831d9728e343c15d0e2b9a144.png

总结:

1、由浏览器发起的路径,“/”表示%tomcat根目录%/webapps根目录

2、由服务器发起的路径,“/”表示项目中的WebRoot根目录

3、以上所有情况,除了“转发”是服务器发起的,其他都是由浏览器发起的

转发后的页面变化,但地址不变,所以当前页面的相对路径的地址都会根据浏览器的地址来查找资源,就可能会报错,所以如果要使前台页面保证无误,可以都是用绝对路径。

推荐使用标签,标签指定页面中所有以相对路径的基础路径,所有路径都是以该标签指定的为开始路径,最后通过拼接来进行访问。

例如: 之后如果访问相对路径下的 index.html,在访问时,浏览器自动装载为http://localhost:8080/FirstWEB/index.html来访问。

由于转发时,要在路径中写项目名,而项目可能时动态的,所以需要动态获得项目的根目录,如下:

第一种

ServletContext context = getServletContext();

String path = context.getContextPath();

response.sendRedirect(path1 + "index.html");

第二种

String path2 = request.getContextPath();

response.sendRedirect(path2 + "index.html");

项目在编译发布时,只会有项目名称,且带着webcontent下的所有文件,编写的java代码 会编译在 WEB-INF下的classes中,

所以不论写相对路径还是绝对路径,都注意不要写WEB-INF。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
For courses in computer programming and engineering. Beginner to Intermediate Programming in Java Absolute Java provides a comprehensive reference to programming in the Java language. Accessible to both beginner and intermediate programmers, the text focuses around specifically using the Java language to practice programming techniques. The Sixth Edition is extremely flexible and easily applicable to a wide range of users. Standalone and optional chapters allow instructors to adapt the text to a variety of curse content. Highly up-to-date with new content and information regarding the use of Java, this text introduces readers to the world of programming through a widely used and relevant language. Also Available with MyProgrammingLab ™ This title is also available with MyProgrammingLab – an online homework, tutorial, and assessment program designed to work with this text to engage students and improve results. Within its structured environment, students practice what they learn, test their understanding, and pursue a personalized study plan that helps them better absorb course material and understand difficult concepts. Students, if interested in purchasing this title with MyProgrammingLab, ask your instructor for the correct package ISBN and Course ID. Instructors, contact your Pearson representative for more information. Interactive Practice helps students gain first-hand programming experience in an interactive online environment. Step-by-step VideoNote Tutorials enhance the programming concepts presented in your Pearson textbook by allowing students to view the entire problem-solving process outside of the classroom–when they need help the most. Pearson eText gives students access to their textbook anytime, anywhere. In addition to note taking, highlighting, and bookmarking, the Pearson eText offers interactive and sharing features. Rich media options let students watch lecture and example videos as they read or do their homework. Instructors can share their comments or highlights, and students can add their own, creating a tight community of learners in your class. The Pearson eText companion app allows existing subscribers to access their titles on an iPad or Android tablet for either online or offline viewing. Dynamic grading and assessment ensure your students’ submissions are automatically graded, both saving you time, and offering students immediate learning opportunities. Gradebook results can be exported to Excel to use with your LMS.
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值