web项目路径问题
转发推荐绝对路径
java转发不需要加项目名
/*
①web项目转发不用加项目名
②path:以/开始,是绝对路径。
最好不要用相对路径,跳来跳去容易出问题
*/
request.getRequestDispatcher("/list.jsp").forward(request, response);
重定向
java重定向需要加项目名
/*
①web项目重定向必须加项目名
②拼接path:/dept/list首位/必须加上!!!
*/
response.sendRedirect(request.getContextPath() + "/dept/list");
jsp推荐绝对路径跳转
<!-- 绝对路径跳转 推荐 -->
<a href="<%=request.getContextPath()%>/dept/ToEditPage?deptno=<%=dept.getDeptno()%>">修改</a>
<form action="${pageContext.request.contextPath}/dept/edit" method="post">
<!-- 相对路径 路径无/开头-->
<a href="ToEditPage?deptno=<%=dept.getDeptno()%>">修改</a>
html的base标签
可能对js(JavaScript)标签不起作用
<%--设置整个网页的基础路径--%>
<base href="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${pageContext.request.contextPath}/">
类的根路径vs相对路径
import org.junit.Test;
import java.io.*;
import java.net.URL;
import java.util.*;
public class T {
/*
资源绑定器,必须是类路径下的.properties文件且 路径后边的扩展名不能写
*/
@Test
public void resourceBundleByPathFromSourceRoot(){
//db/db1路径:类的根路径加载文件【path from source root】
ResourceBundle bundle = ResourceBundle.getBundle("db/db1");
System.out.println("【日志】读取是类的根路径下的.properties文件username="+bundle.getString("username"));
}
/*
通过类的根路径获取文件的绝对路径
运行结果:【日志】文件路径:/E:/code/javaweb1.0/donglijiedian_moniservlet/target/classes/file/hello.txt
target文件夹是一个存放编译生成的文件和目录的文件夹
classLoader.getResource("文件");
加载的是以target/classes相对路径下的文件【path from source root】
*/
@Test
public void getAbsolutePathByPathFromSourceRoot() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
URL resource = classLoader.getResource("file/hello.txt");
String path = resource.getPath();
System.out.println("【日志】文件路径:" + path);
}
/*
获取文件的输入流并打印
通过类的根路径加载文件【path from source root】
*/
@Test
public void readByPathFromSourceRoot() {
try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("file/hello.txt")) {
readFile(in);
} catch (IOException e) {
e.printStackTrace();
}
}
/*获取文件的输入流并打印
相对路径【path from content root】src/main/resources/file/hello.txt */
@Test
public void readByPathFromContentRoot() throws Exception {
try (FileInputStream in = new FileInputStream("src/main/resources/file/hello.txt");) {
readFile(in);
}
}
/*Properties读取properties文件
相对路径【path from content root】src/main/resources/file/hello.txt */
@Test
public void readPropertiesByPathFromContentRoot() {
try (FileInputStream in = new FileInputStream("src/main/resources/db/db1.properties")) {
Properties properties = new Properties();
properties.load(in);//文件中的数据顺着管道加载到map集合中。
System.out.println(properties.getProperty("username")
+ ":" + properties.getProperty("password"));//abc:123
//读取properties文件,反射机制实例化类
String objxx = properties.getProperty("objxx");
System.out.println(Class.forName(objxx).newInstance());//java.lang.Object@6d21714c
} catch (Exception e) {
e.printStackTrace();
}
}
/*io流:读取txt文件*/
private void readFile(InputStream in) {
try {
byte[] bytes = new byte[1024 * 1024];
int readLengh = 0;
System.out.print("读取txt:");
while ((readLengh = in.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, readLengh));
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
绝对路径vs相对路径
- 从移植性能上看,绝对路径优于相对路径。相对路径只能在IDEA中使用,绝对路径可以跨操作系统、跨开发环境使用。
- 从开发效率上看,绝对路径优于相对路径。