【问题标题】:How to show a JSP page using Servlet which has image in its Response?如何使用响应中有图像的 Servlet 显示 JSP 页面?
【发布时间】:2011-09-14 22:05:43
【问题描述】:

用于从数据库获取图像并将图像存储在响应中的 Servlet doGet() 代码

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

// Get userid from session

try {

    // Get photos from database in (image)

    // Init servlet response.
    response.reset();
    response.setBufferSize(DEFAULT_BUFFER_SIZE);
    response.setContentType(image.getContenttype());
    response.setHeader("Content-Length", String.valueOf(image.getLength()));
    response.setHeader("Content-Disposition", "inline; filename=\"" + image.getTitle()
    + "\"");

    // Prepare streams.
    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        // Open streams.
        input = new BufferedInputStream(image.getPhoto(), DEFAULT_BUFFER_SIZE);
        output = new BufferedOutputStream(response.getOutputStream(),
                 DEFAULT_BUFFER_SIZE);

        // Write file contents to response.
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        int length;
        while ((length = input.read(buffer)) > 0) {
            output.write(buffer, 0, length);
        }
    } finally {
        // Gently close streams.
        output.close();
        input.close();
    }

    //Redirect it to photo page
    RequestDispatcher rd = request.getRequestDispatcher
        ("/webplugin/jsp/profile/photos.jsp");
    rd.forward(request, response);

} catch (Exception e) {
    e.printStackTrace();
}

}

但是,当这个 servlet 显示 JSP 页面时,它显示的是 only image 而不是 JSP 页面。

JSP 代码:

... JSP code

<img src="Servlet url">

... JSP code cont...

我得到什么输出:

  1. 我在 JSP 中只得到 Image 而不是 image
  2. 当我使用 RequestDispatcher/sendRedirect() 时,我得到以下异常 java.lang.IllegalStateException: Cannot forward after response has been committed

问题:

  1. 如何在 JSP 中获取 Image 而不仅仅是在浏览器中获取 Image
  2. 如何避免上述异常?

编辑:我的 Web.xml 看起来像这样

<servlet>
    <servlet-name>Photo Module</servlet-name>
    <servlet-class>app.controllers.PhotoServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Photo Module</servlet-name>
    <url-pattern>/Photos</url-pattern>
</servlet-mapping>

【问题讨论】:

    标签: java image jsp servlets


    【解决方案1】:

    如何在 JSP 中获取 Image 而不仅仅是在浏览器中获取 Image

    在浏览器地址栏中输入包含&lt;img&gt; 元素的JSP 文件的URL。

    http://localhost:8080/contextname/webplugin/jsp/profile/photos.jsp


    如何避免上述异常?

    从 servlet 代码中删除以下行。

    //Redirect it to profile page
    RequestDispatcher rd = request.getRequestDispatcher
        ("/webplugin/jsp/profile/photos.jsp");
    rd.forward(request, response);
    

    servlet 应该只返回图像。而已。下载和显示图像的是网络浏览器本身,而不是网络服务器。

    另见:

    【讨论】:

    • 我已经删除了 RequestDispatcher 并且异常消失了,它工作正常。但是,应该在 JSP 中的图像在进行建议的更改后显示为原样。
    • 我很困惑。现在究竟是什么问题?您评论中的“但是”意味着它不能按您的预期工作,但是您还提到它工作正常并且图像按原样显示。
    • 另外,我的 web.xml 看起来和上面一样,我也使用了&lt;img src="Photos" &gt;,但它仍然只在浏览器中显示图像,而不是在 JSP 中显示图像。
    • 那么 URL 就是完全错误的。假设您的 JSP 确实在http://localhost:8080/contextname/webplugin/jsp/profile/photos.jsp 上运行,那么这个img src 将基本上指向http://localhost:8080/contextname/webplugin/jsp/profile/Photos,但servlet 实际上在http://localhost:8080/contextname/Photos 上进行侦听。至于如何解决这个问题,请重新阅读我在您之前的问题stackoverflow.com/questions/6315671/… 中发布的答案,是的,您需要在 URL 前面加上${pageContext.request.contextPath}
    • 好的。希望您现在也能很好地了解现在到底发生了什么。浏览器请求 JSP。服务器执行 JSP,JSP 依次生成并发送一堆 HTML 到响应。浏览器检索这组 HTML 代码(当您在浏览器中右键单击页面并执行 View Source 时可以看到)。浏览器解析 HTML 以显示它并遇到 &lt;img&gt; 并在其上发送新请求。最后浏览器显示图像。在 Net 选项卡中使用 HTTP 流量检查器(例如 Firebug)应该会显示所有这些请求(和响应)。
    【解决方案2】:

    1你在修改响应然后转发,没用。不要这样做。

    2 How to get image from servlet to jsp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多