//网络相对路径
String imagePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
结果如:http://192.168.0.12:8080/aa/这种样式
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>
<%="request.getContextPath() = " + request.getContextPath() + "<BR />"%>
<%="request.getServletPath() = " + request.getServletPath() + "<BR />"%>
<%="request.getRequestURI() = " + request.getRequestURI() + "<BR />"%>
<%="request.getRequestURL() = " + request.getRequestURL() + "<BR />"%>
<%
String realPath = session.getServletContext().getRealPath("/");
%>
<%="request.getRealPath(/"//") = " + realPath + ""%>
</body>
</html>
返回结果:
request.getContextPath() = /TestProject
request.getServletPath() = /TestPath.jsp
request.getRequestURI() = /TestProject/TestPath.jsp
request.getRequestURL() = http://localhost:8080/TestProject/TestPath.jsp
request.getRealPath("/") = C:/Tomcat/webapps/TestProject/