读取项目某个图片的所有路径并显示
2013-04-14 00:11
288 查看
读取项目某个图片的所有路径并显示1、利用ServletContext对象获得全局的属性值2、通过getResourcePath()返回值为Set<String>对象A3、利用req.setAttribute设置A4、跳转dispatcher进行传参
来自为知笔记(Wiz)
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>
来自为知笔记(Wiz)
相关文章推荐
- 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 ASP加javascript实现图片轮流显示(图片路径从数据库中读取)
- img标签src路径不变,读取不同图片时,图片不刷新甚至不显示
- android 根据SD卡中图片路径读取并显示SD中的图片——源代码
- 读取input:file的路径并显示本地图片的方法
- 解决JAVA EE项目中JSP无法显示绝对路径图片
- 读取input:file的路径并显示本地图片的方法
- android 根据SD卡中图片路径读取并显示SD中的图片——源代码
- 读取input:file的路径并显示本地图片的方法
- 读取input:file的路径并显示本地图片的方法
- .net网站上传图片换电脑不显示 当不用网站的IP地址访问图片,只用相对路径访问时,在发布网站的时候,将上传图片的目标文件夹,包含在项目中再发布即可。
- 用Qt5和OpenCV读取显示中文路径的图片
- android 根据SD卡中图片路径读取并显示SD中的图片——源代码
- jsp网页在浏览器中不显示图片_eclipse环境下配置tomcat中jsp项目的虚拟路径
- 读取input:file的路径并显示本地的图片
- Android中读取系统图库(包含相册)中的图片,显示图片与图片的路径
- Android读取assets目录下的所有图片并显示的方法
- 在JAVA WEB项目中,JSP中的图片无法显示,绝对路径和相对路径问题的解决办法。
- 读取文件中的所有图片的路径,保存到txt文件中。
- 【项目中】Android 小米红米手机无法读取照片图片路径的问题
- php自动读取文件夹下所有图片并分页显示