zoukankan      html  css  js  c++  java
  • 读取项目某个图片的所有路径并显示

    读取项目某个图片的所有路径并显示
    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>




  • 相关阅读:
    C/C++,彩色图像小游戏。
    js 调用百度地图,并且定位用户地址,显示省市区街,经纬度
    C/C++ 双精度double 数据相加出错缺陷解释
    辗转相除法求最大公约数,非goto
    如何用 js 获取table 或者其他块状标签的 宽和高
    如何用 ajax 连接mysql数据库,并且获取从中返回的数据。ajax获取从mysql返回的数据。responseXML分别输出不同数据的方法。
    C++ 连接数据库的入口和获取列数、数据
    Win10:如何修改双网卡的优先级?
    如何拉动内需,击中客户深层需求,4个经典案例分析!
    单点登录SSO简介
  • 原文地址:https://www.cnblogs.com/babyhhcsy/p/3019643.html
Copyright © 2011-2022 走看看