java代码:
1、获得某个文件夹下面的所有文件名称;
ServletContext context = this.getServletContext();Set<String> set = context.getResourcePaths("/photo");
2、注意获得的context文件名称的返回值是个set类型;
3、通过EL表达式获得具体的值;
在 <c:forEach var="str" items="${photoList }" varStatus="strs">当中,str表示list值,item表示从作用域当中获得具体的值;
varStatus可以用来控制循环的数;
strs.index表示下标;
strs.count表示从1开始计数一共有多少个元素;
strs.first表示第一个值;
strs.last表示最后一个数;
注意:
在获得所有路径是,注意输出路径的地址,在显示图片的时候,应当去掉/
或者在显示图片的时候加上项目名称进行显示
GetAllPhoto.java
package com.babyhhcsy.ELServlet;
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 ;
}
}
</servlet-mapping>
<servlet>
<servlet-name>getPhoto</servlet-name>
<servlet-class>com.pk.mylogin.web.servlet.GetAllPhoto</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getPhoto</servlet-name>
<url-pattern>/getPhoto</url-pattern>
</servlet-mapping>
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%
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>
<c:import url="top.jsp"></c:import>
<%
Set<String> set = (Set<String>) request.getAttribute("photoList");
for (String str : set) {
str = str.substring(1);
%>
<img src="<%=str%>" width="100px" height="50px;">
<%
}
%>
<c:choose>
<c:when test="${empty photoList }">
<h2>没有图片</h2>
</c:when>
<c:otherwise>
<c:forEach var="str" items="${photoList }" varStatus="strs">
<c:if test="${strs.first }">
<a href = "http://www.baidu.com" target="_blank"><img src="/mylogin${str}" width="100px" height="100px"></a>
</c:if>
<%-- ${strs.first } ---- ${strs.last } ---- ${strs.count } ---- ${strs.index }--%>
<c:if test="${strs.index % 2 == 0}" >
<br />
</c:if>
<c:if test="${strs.count > 1 && !strs.last}">
<img src="/mylogin${str }" width="100px" height="100px">
</c:if>
<c:if test="${strs.last }">
<a href="http://www.google.cn.hk"><img src="/mylogin${str }" width="100px" height="100px"></a>
</c:if>
</c:forEach>
</c:otherwise>
</c:choose>
<c:if test="${ 2 == 2 }">
<h1>错误</h1>
</c:if>
</body>
</html>