您的位置:首页 > 其它

读取项目某个图片的所有路径并显示

2013-04-14 00:11 288 查看
读取项目某个图片的所有路径并显示1、利用ServletContext对象获得全局的属性值2、通过getResourcePath()返回值为Set<String>对象A3、利用req.setAttribute设置A4、跳转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>


来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐