读取项目某个图片的所有路径并显示
1、利用ServletContext对象获得全局的属性值
2、通过getResourcePath()返回值为Set<String>对象A
3、利用req.setAttribute设置A
4、跳转dispatcher进行传参
ServletContext context = this.getServletContext();
Set<String> set = context.getResourcePaths("/photo");
req.setAttribute("photoList", set);
req.getRequestDispatcher("/ShowPhoto.jsp").forward(req, resp);
return ;
在页面当中的显示方法
<%
Set<String> set = (Set<String>)request.getAttribute("photoList");
for(String str : set){
str = str.substring(1);
%>
<img src="<%=str %>" width="100px" height="50px;">
<%} %>
GetAllPhoto.java
package com.pk.mylogin.web.servlet;
import java.io.IOException;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetAllPhoto extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ServletContext context = this.getServletContext();
Set<String> set = context.getResourcePaths("/photo");
req.setAttribute("photoList", set);
req.getRequestDispatcher("/ShowPhoto.jsp").forward(req, resp);
return ;
}
}
jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'ShowPhoto.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
Set<String> set = (Set<String>)request.getAttribute("photoList");
for(String str : set){
str = str.substring(1);
%>
<img src="<%=str %>" width="100px" height="50px;">
<%} %>
</body>
</html>