一文彻底搞清楚JavaWeb中request的路径区分(详解)

2023-12-26 23:58

本文主要是介绍一文彻底搞清楚JavaWeb中request的路径区分(详解),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

JavaWeb中request的路径问题

      • 1. request 中的路径分类
        • 1.1 资源路径
        • 1.2 部署路径
        • 1.3 项目路径
        • 1.4 URI 与 URL
      • 2. 源码验证 + 输出截图

1. request 中的路径分类

项目名称:/demo (IDEA中修改了当前Tomcat配置项目访问根目录为 /demo)
当前资源:/path
访问路径:http://127.0.0.1:8080/demo/path

1.1 资源路径
  • request.getServletPath();

毋庸置疑为 Servlet 资源路径: /path

1.2 部署路径
  • request.getRealPath(“index.jsp”); // 过时了
  • request.getServletContext().getRealPath(“index.jsp”);
  • request.getSession().getServletContext().getRealPath(“index.jsp”);

getRealPath(String path) 统一是部署路径:
磁盘路径\out\artifacts\demo_job_war_exploded\index.jsp
应用:

  1. 上传文件路径:xxx.getRealPath(“uploadDir”) + File.separator + uploadFilename
1.3 项目路径
  • this.getServletContext().getContextPath();
  • request.getContextPath();
  • request.getServletContext().getContextPath();
  • request.getSession().getServletContext().getContextPath();

getContextPath() 统一是项目路径: /demo
应用:

  1. 重定向路径: request.getContextPath() + servletPath或htmlPath
  2. 下载文件路径:request.getContextPath() + File.separator + “uploadDir” + File.separator + uploadFilename
1.4 URI 与 URL
  • request.getRequestURI();
  • request.getRequestURL();

访问:http://127.0.0.1:8080/demo/path 验证结果:
URI 路径为:/demo/path (当前 Servlet 在项目的绝对路径)
URL 路径为:http://127.0.0.1:8080/demo/path (当前 Servlet 在项目中带协议和地址端口的绝对路径)
总结:URL = 协议 + ip:port + URI

2. 源码验证 + 输出截图

@WebServlet(name = "TestPathServlet", urlPatterns = "/path")
public class TestPathServlet extends HttpServlet {@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/* 项目名称:/demo 当前资源:/path */// 毋庸置疑 ServletPath 为 "/path"String servletPath = request.getServletPath();// getRealPath(String path) 统一是部署路径: "磁盘路径\out\artifacts\demo_job_war_exploded\index.jsp"// 应用:// 1. 上传文件路径:xxx.getRealPath("uploadDir") + File.separator + uploadFilenameString realPath1 = request.getRealPath("index.jsp");String realPath2 = request.getServletContext().getRealPath("index.jsp");String realPath3 = request.getSession().getServletContext().getRealPath("index.jsp");// getContextPath() 统一是项目路径: "/demo"// 应用:// 1. 重定向路径: request.getContextPath() + servletPath或htmlPath// 2. 下载文件路径:request.getContextPath() + File.separator + "uploadDir" + File.separator + uploadFilenameString contextPath = this.getServletContext().getContextPath();String contextPath1 = request.getContextPath();String contextPath2 = request.getServletContext().getContextPath();String contextPath3 = request.getSession().getServletContext().getContextPath();// 访问:http://127.0.0.1:8080/demo/path得出的结果String remoteUser = request.getRemoteUser(); // nullint localPort = request.getLocalPort(); // 8080String localAddr = request.getLocalAddr(); // 127.0.0.1String remoteAddr = request.getRemoteAddr(); // 127.0.0.1int remotePort = request.getRemotePort(); // 8080// /demo/path (当前 Servlet 在项目的绝对路径)String requestURI = request.getRequestURI();// http://127.0.0.1:8080/demo/path (当前 Servlet 在项目中带协议和地址端口的绝对路径)StringBuffer requestURL = request.getRequestURL();}@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}
}

输出截图:
在这里插入图片描述

这篇关于一文彻底搞清楚JavaWeb中request的路径区分(详解)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/541240

相关文章

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

MySQL中的LENGTH()函数用法详解与实例分析

《MySQL中的LENGTH()函数用法详解与实例分析》MySQLLENGTH()函数用于计算字符串的字节长度,区别于CHAR_LENGTH()的字符长度,适用于多字节字符集(如UTF-8)的数据验证... 目录1. LENGTH()函数的基本语法2. LENGTH()函数的返回值2.1 示例1:计算字符串

Spring Boot spring-boot-maven-plugin 参数配置详解(最新推荐)

《SpringBootspring-boot-maven-plugin参数配置详解(最新推荐)》文章介绍了SpringBootMaven插件的5个核心目标(repackage、run、start... 目录一 spring-boot-maven-plugin 插件的5个Goals二 应用场景1 重新打包应用

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

Java中读取YAML文件配置信息常见问题及解决方法

《Java中读取YAML文件配置信息常见问题及解决方法》:本文主要介绍Java中读取YAML文件配置信息常见问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要... 目录1 使用Spring Boot的@ConfigurationProperties2. 使用@Valu