• Java web中的路径问题


    jsp页面作以下测试

    <%
        System.out.println("getContextPath():"+request.getContextPath());
        System.out.println("getServletPath():"+request.getServletPath());
        System.out.println("getRequestURI():"+request.getRequestURI());
        System.out.println("getRequestURL():"+request.getRequestURL());
        System.out.println("getRealPath("/")"+request.getRealPath("/"));
        System.out.println("getServerName():"+request.getServerName());
        System.out.println("getServerPort():"+request.getServerPort());
        System.out.println("getScheme():"+request.getScheme());
    %>

    结果为

    getContextPath():/website
    getServletPath():/jsp/test.jsp
    getRequestURI():/website/jsp/test.jsp
    getRequestURL():http://localhost:8080/website/jsp/test.jsp
    getRealPath("/")E:eclipseapache-tomcat-9.0.22-windows-x64apache-tomcat-9.0.22webappswebsite
    getServerName():localhost
    getServerPort():8080
    getScheme():http

    应用

    在jsp和servlet的转发过程中常常会出现文件路径错误。

    如果设为相对URL,则在转发过程中可能出现文件路径错误。

    如果设为绝对URL,在作开发时,主机是localhost,而运行时是服务器IP或域名,修改复杂。

    因此选用动态的URL,由Java路径函数来读取此时请求协议名,主机名,端口号,和网站程序项目名,将其设置为jsp的base属性,在jsp页面内的其它文件直接使用相对于网站根目录的路径。

    <%
    String basePath=request.getScheme()+"://"+request.getServerName()+":"+
    request.getServerPort()+request.getContextPath()+"/";
    %>
    <base href="${pageScope.basePath}">
  • 相关阅读:
    python——(os, shutil)
    python-(subprocess, commands)
    PHP设计模式二:单例模式
    PHP设计模式一:工厂方法设计模式
    PHP垃圾回收机制
    PHP异常处理机制
    超文本传送协议HTTP
    IP地址
    Linux系统网络基本配置
    Linux系统LVM基本使用
  • 原文地址:https://www.cnblogs.com/blunFan/p/11641446.html
Copyright © 2020-2023  润新知