讓我們分步驟看看會發生什麼:
src
http://
file://
http://example.com/context/images/foo.png
http:/ /example.com/context/images?id=1
/images/*
byte[]
InputStream
#ResultSet #getBytes()
ResultSet#getBinaryStream()
#@Lob
OutputStream
Content-Type
ServletContext#getMimeType()
web.xml 中的## 擴充功能和/或覆寫該副檔名。
## 擴充功能和/或覆寫該副檔名
JSP 中):
<img src="${pageContext.request.contextPath}/images/foo.png"> <img src="${pageContext.request.contextPath}/images/bar.png"> <img src="${pageContext.request.contextPath}/images/baz.png">
EL 動態設定src ="https://stackoverflow.com/tags/jstl/info">JSTL:
="https://stackoverflow.com/tags/jstl/info">JSTL
<c:forEach items="${imagenames}" var="imagename"> <img src="${pageContext.request.contextPath}/images/${imagename}"> </c:forEach>
servlet,它會偵聽 /images/*,下面的範例使用普通的 JDBC 來完成這項工作: @WebServlet("/images/*") public class ImageServlet extends HttpServlet { // content=blob, name=varchar(255) UNIQUE. private static final String SQL_FIND = "SELECT content FROM Image WHERE name = ?"; @Resource(name="jdbc/yourDB") // For Tomcat, define as <Resource> in context.xml and declare as <resource-ref> in web.xml. private DataSource dataSource; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String imageName = request.getPathInfo().substring(1); // Returns "foo.png". try (Connection connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL_FIND)) { statement.setString(1, imageName); try (ResultSet resultSet = statement.executeQuery()) { if (resultSet.next()) { byte[] content = resultSet.getBytes("content"); response.setContentType(getServletContext().getMimeType(imageName)); response.setContentLength(content.length); response.getOutputStream().write(content); } else { response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. } } } catch (SQLException e) { throw new ServletException("Something failed at SQL/DB level.", e); } } }
,下面的範例使用普通的 JDBC 來完成這項工作: @WebServlet("/images/*") public class ImageServlet extends HttpServlet { // content=blob, name=varchar(255) UNIQUE. private static final String SQL_FIND = "SELECT content FROM Image WHERE name = ?"; @Resource(name="jdbc/yourDB") // For Tomcat, define as <Resource> in context.xml and declare as <resource-ref> in web.xml. private DataSource dataSource; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String imageName = request.getPathInfo().substring(1); // Returns "foo.png". try (Connection connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL_FIND)) { statement.setString(1, imageName); try (ResultSet resultSet = statement.executeQuery()) { if (resultSet.next()) { byte[] content = resultSet.getBytes("content"); response.setContentType(getServletContext().getMimeType(imageName)); response.setContentLength(content.length); response.getOutputStream().write(content); } else { response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. } } } catch (SQLException e) { throw new ServletException("Something failed at SQL/DB level.", e); } } }
@WebServlet("/images/*") public class ImageServlet extends HttpServlet { // content=blob, name=varchar(255) UNIQUE. private static final String SQL_FIND = "SELECT content FROM Image WHERE name = ?"; @Resource(name="jdbc/yourDB") // For Tomcat, define as <Resource> in context.xml and declare as <resource-ref> in web.xml. private DataSource dataSource; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String imageName = request.getPathInfo().substring(1); // Returns "foo.png". try (Connection connection = dataSource.getConnection(); PreparedStatement statement = connection.prepareStatement(SQL_FIND)) { statement.setString(1, imageName); try (ResultSet resultSet = statement.executeQuery()) { if (resultSet.next()) { byte[] content = resultSet.getBytes("content"); response.setContentType(getServletContext().getMimeType(imageName)); response.setContentLength(content.length); response.getOutputStream().write(content); } else { response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404. } } } catch (SQLException e) { throw new ServletException("Something failed at SQL/DB level.", e); } } }
靜態資源servlet的抽像模板。
讓我們分步驟看看會發生什麼:
元素。
src
屬性。src
屬性需要指向有效的http://
URL,因此不是本機磁碟檔案系統路徑file://
因為當伺服器和用戶端在物理上不同的機器上運行時,這永遠不會起作用。http://example.com/context/images/foo.png
)或作為請求參數(例如http:/ /example.com/context/images?id=1
)。/images/*
,這樣您就可以在特定的 URL 上執行一些 Java 程式碼。byte[]
或InputStream
的形式獲取,JDBC API 提供#ResultSet #getBytes()
和ResultSet#getBinaryStream()
為此,JPA API 提供#@Lob
為此。 李>byte[]
或InputStream
寫入回應的OutputStream
(通常的Java IO 方式。Content-Type
回應頭也需要設定。您可以透過ServletContext#getMimeType()
基於映像檔副檔名,您可以透過web.xml 中的
## 擴充功能和/或覆寫該副檔名
。JSP 中):
如果需要,您也可以在使用EL 動態設定src
然後定義/建立一個="https://stackoverflow.com/tags/jstl/info">JSTL
:servlet,它會偵聽 /images/*
就是這樣。如果您擔心 HEAD 和快取標頭並正確回應這些請求,請使用此,下面的範例使用普通的 JDBC 來完成這項工作:
靜態資源servlet的抽像模板。
另請參閱: