首页 > 代码库 > 在Jsp中调用静态资源,路径配置问题
在Jsp中调用静态资源,路径配置问题
在Jsp中调用图片、JS脚本等,针对取得的路径有两种调用方式:
1、放入Body中生成绝对路径(不建议)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>image调用</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>图片访问</h1>
<div>
<img alt="图片" src="http://www.mamicode.com//image/a.png">
</div>
</body>
</html>
2、在Head中指定,Body中使用相对路径(建议)
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>image调用</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- base需要放到head中 -->
<base href="http://www.mamicode.com/">
</head>
<body>
<h1>图片访问</h1>
<div>
<img alt="图片" src="http://www.mamicode.com/image/a.png">
</div>
</body>
</html>
在Jsp中调用静态资源,路径配置问题