1. 程式人生 > >JSP之專案路徑問題${pageContext.request.contextPath},path和 basePath

JSP之專案路徑問題${pageContext.request.contextPath},path和 basePath

<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName()
                       + ":" + request.getServerPort() + path + "/";

%>

${pageContext.request.contextPath}

測試的案例index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   
 <%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() 
	                   + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%out.println(basePath);%>
<br>
<% out.println(path); %>
<br>
 ${pageContext.request.contextPath}

</body>
</html>

輸出結果:

常用路徑問題的解決:

    案例專案


其中customer.jsp訪問一些資源的方法:

login.jsp訪問一些資源的做法: