jsp中引入js、css时出现net::ERR_ABORTED 404 (Not Found)错误

GET http://localhost:8080/static/My/Sub.js net::ERR_ABORTED 404 (Not Found)

出现此问题主要是引入的js、css不起作用。
相对路径引入

<%@ 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>
<script type="text/javascript" src="../static/My/Sub.js"  charset="utf-8"></script>
</head>

不起作用

改为绝对路径引入:

<%@ 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">
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
<script type="text/javascript" src="<%=basePath%>/static/My/Sub.js"  charset="utf-8"></script>
</head>

问题解决

猜你喜欢

转载自blog.csdn.net/qq_40572277/article/details/88053289