JavaWeb-使用绝对路径后-虚拟目录设置原则(JavaWeb – after using absolute path – virtual directory setting principle)

虚拟目录设置原则

规则: 判断定义的路径针对的对象,或者请求发出的源头位置

  • 需要虚拟目录的情况: [外部请求]请求从客户端浏览器发出,如点击网页链接,重定向。在需要使用虚拟目录的情况下,建议设置动态虚拟目录。
  • 不需要虚拟目录的情况: [内部请求]请求从服务器发出,如内部转发

理解: 05:56-08:21

动态虚拟目录

使用动态虚拟目录的原因

  • 防止出现因项目的虚拟目录路径变动导致原本资源无法访问情况,减少出现上述情况下,频繁改动请求地址。

设置动态虚拟目录

//        1. 定义动态虚拟目录
String contextPath = req.getContextPath();
//        2. 使用动态虚拟目录作为请求地址
resp.sendRedirect(contextPath+"/responseDemo2");
————————

Virtual directory setting principle

Rule: judge the object targeted by the defined path or the source location of the request

  • When a virtual directory is required: [external request] the request is sent from the client browser, such as clicking a web page link and redirection. If you need to use a virtual directory, it is recommended to set a dynamic virtual directory.
  • When a virtual directory is not required: [internal request] the request is sent from the server, such as internal forwarding

Understanding: 05:56-08:21

Dynamic virtual directory

Reasons for using dynamic virtual directories

  • Prevent the original resources from being inaccessible due to the change of the virtual directory path of the project, and reduce the frequent change of the request address in the above cases.

Set dynamic virtual directory

//        1. 定义动态虚拟目录
String contextPath = req.getContextPath();
//        2. 使用动态虚拟目录作为请求地址
resp.sendRedirect(contextPath+"/responseDemo2");