servlet使用dispatcher轉jsp頁面,原本圖片無法顯示

在servlet處理完之後使用dispatcher轉到jsp頁面,原本的圖片無法顯示, 因爲servlet轉jsp的路徑目錄與原本的路徑目錄不同。所以會有無法顯示的問題;

解決方案;就是在jsp頁面添加

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

在head中添加 base路徑

<head>
  <base href="<%=basePath%>">
    <title>My JSP 'demo.jsp' starting page</title>
  </head>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章