类的根路径vs相对路径vs绝对路径 web项目路径 IO流加载路径

本文讨论了JavaWeb项目中,转发和重定向时路径的区别,推荐使用绝对路径,以及类的根路径和相对路径在资源访问、文件操作和测试中的应用。
摘要由CSDN通过智能技术生成

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中使用,绝对路径可以跨操作系统、跨开发环境使用。
  • 从开发效率上看,绝对路径优于相对路径。
  • 11
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值