zoukankan      html  css  js  c++  java
  • 在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="<%=basePath%>/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=" <%=basePath%>">   
    </head>
    <body>
        <h1>图片访问</h1>
        <div>     
            <img alt="图片" src="image/a.png">
        </div>
    </body>
    </html>

  • 相关阅读:
    Oracle notes
    jQuery笔记
    sql developer 要求enter the full pathname for java.exe
    [Error] WCF: SOAP security negotiation
    Unity
    Windows Form 开发笔记
    WP开发 资料整理
    乔迁新居:http://longwarelive.spaces.live.com/
    2008年1月1日启用 longware@live.cn
    《程序员》杂志揭晓2007软件中国年度评选
  • 原文地址:https://www.cnblogs.com/maocs/p/4256302.html
Copyright © 2011-2022 走看看