<%@ 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>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String jspPath = request.getSession().getServletContext().getRealPath("")+request.getRequestURI().replace(path, "");
%>
</body>
path:<%=path %><br>
basePath:<%=basePath %><br>
jspPath:<%=jspPath %><br>
</html>
上面的代码输出到浏览器中是,:
path:/webdemo
basePath:http://localhost:8082/webdemo/
jspPath:D:\project\new-workspace-eclipse-jk-shihong-new-branch\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\webdemo/NewFile.jsp
我的项目架构图:
项目的名称是webdemo,tomcat端口是8082
所以,在jsp中,最好用basePath这个绝对路径,基本上不会错
js我们一般放在WebContent下面建个js的文件夹,jsp一般放在WebContent的WEB-INFO下面,来保证jsp的源码不暴露
那么在jsp中引用js就可以这么写:
<script type="text/javascript" src="${basePath}/js/platform/tbacollectionlog/queryTBaCollectionLog.js?d=<%=myDate%>"></script>
好了,jsp路径问题就讲到这里,欢迎留言