循环浏览JSP中的图像列表并调用servlet以在表中显示每个图像,但是最终输出显示相同的图像 [英] Looping through a list of images in JSP and calling a servlet to display each one in a table, but the final output displays same image

查看:44
本文介绍了循环浏览JSP中的图像列表并调用servlet以在表中显示每个图像,但是最终输出显示相同的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个作业,其中我必须提供用户登录名,之后他将被定向到一个页面,他可以在该页面上载图像,并且他所有以前的和新的图像都将在下面的表格中显示.所以我的应用程序的结构是这样的...

I am working on an assignment in which I have to provide a user login after which he will be directed to a page where he can upload images and all of his previous + new images will be displayed below in a table. So the structure of my application is like this...

(为清楚起见,修剪了大部分代码)

(trimmed most of the code for clarity)

允许上传并显示图像的JSP文件

JSP file that allows upload and displays the image

<p>Please select an image file to upload(Max file size 1 MB)</p>

<%
    Integer userId = 0;
    userId = (Integer) session.getAttribute("userId");
%>
<%
    String errorMessage = (String) request.getAttribute("error");
    if (errorMessage != null) {
        out.print("<h4> " + errorMessage + "</h4>");
    }
%>
<form action="imageupload" enctype="multipart/form-data" method="post">
    <br /> <input type="file" name="uploader" id="uploader" /> <br /> <br />
    <input type="submit" />&nbsp;&nbsp;&nbsp; <input type="button"
        value="clear" onClick="clear()" />


</form>
<br />
<h4>Uploaded Images</h4>
<br />
<table width="80%">
    <tr>
        <th>S No.</th>
        <th>Name</th>
        <th>size</th>
        <th>Preview</th>
        <th>Actions</th>
    </tr>
    <%
        UserImagesDAOImplementation userImages = new UserImagesDAOImplementation();

        List<Image> images = (List<Image>) userImages.getUserImages(userId);

        for (Image image : images) {
    %>
    <tr>
        <td><%=image.getSn()%></td>
        <td><%=image.getImageName()%></td>
        <td><%=image.getSize()%></td>
        <td><% session.setAttribute("image",image.getImage() ); %><img src="toimage" height="100px" width="100px" /></td>
        <td><img src="img/edit.png" />&nbsp;&nbsp;<img src="img/url.png" /></td>
    </tr>
    <%
        }
    %>
</table>
<br />
Total space used: <%= (userImages.getSizeUsage(userId)/1024) %> KB / 10 MB

返回图像的"toimage" servlet

the "toimage" servlet that returns the image

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    HttpSession session = request.getSession();
    Byte[] image = (Byte[]) session.getAttribute("image");
    int len = image.length;
    byte[] imageData = new byte[len];

    for(int i=0; i < len; i++) {
        imageData[i] = image[i];
    }

    response.setContentType("image/jpg");
    response.getOutputStream().write(imageData);
    response.getOutputStream().flush();
    response.getOutputStream().close();

}

我的问题是,它在所有表行中显示列表中的最后一个图像,并且在上载其他图像后,它在每行中都显示该新图像.

My problem is that it displays the last image in the list in all the table rows and after uploading some other image it displays that new image in every row.

我对该站点的设计不是很自信,但这是我第一次使用JSP.我决定将图像列表放入JSP内,因为稍后我将不得不对其执行更多操作.我猜问题出在设置会话变量还是最后调用了src servlet.不管是什么,有人可以解释一下此设计中的典型事件流程.

I am not very confident about my design for the site but this is the first time I am working with JSP. I decided to get the image list inside JSP as I will have to perform some more operations on it later on. I guess the problem is either in setting the session variable or the src servlet being called in the end. Whatever it is, can someone please explain the typical flow of events in this design.

将打印语句放入"toimage" servlet中可证明该语句仅被调用一次.那么如何使JSP循环每次都调用图像src ??

Putting a print statement inside the "toimage" servlet proves that it is being called just once. So how can I make the JSP loop to call the image src every time ??

推荐答案

您使用单个会话属性存储每个图像.因此,在循环结束时,此会话属性包含最后一个.

You use a single session attribute to store every image. So, at the end of the loop, this session attribute contains the last one.

您完全不应该使用该会话.相反,您应该传递ID或名称或将图片标识为URL参数的任何内容:

You shouldn't use the session at all. Instead, you should pass the ID or name or whatever identifies the image as a URL parameter:

<img src="toimage?imageId=<%= image.getId() %>" height="100px" width="100px" />

并且servlet应该使用此参数来确定它必须覆盖的图像并将其发送到浏览器.

And the servlet should use this parameter to now which image it must oad and send to the browser.

另外,学习JSTL和EL.不得使用小脚本.

Also, learn the JSTL and the EL. Scriptlets sould not be used.

这篇关于循环浏览JSP中的图像列表并调用servlet以在表中显示每个图像,但是最终输出显示相同的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆