Java web path problem in

jsp page for the following tests

<%
    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());
%>

Results

getContextPath():/website
getServletPath():/jsp/test.jsp
getRequestURI():/website/jsp/test.jsp
getRequestURL():http://localhost:8080/website/jsp/test.jsp
getRealPath("/")E:\eclipse\apache-tomcat-9.0.22-windows-x64\apache-tomcat-9.0.22\webapps\website\
getServerName():localhost
getServerPort():8080
getScheme():http

application

Often the file path error occurs during forwarding the jsp and servlet.

If set to a relative URL, the file path errors may occur during the forwarding process.

If set to an absolute URL, and when for the development, the host is localhost, and runtime server IP or domain name, modify complex.

So use a dynamic URL, the Java path function to read at this time request protocol name, host name, port number, and web application project name, set it to base property jsp, and other documents within jsp pages directly opposite in the path of the site's root directory.

<%
String basePath=request.getScheme()+"://"+request.getServerName()+":"+
request.getServerPort()+request.getContextPath()+"/";
%>
<base href="${pageScope.basePath}">

 

Guess you like

Origin www.cnblogs.com/blunFan/p/11641446.html