【问题标题】:Servlet not being invoked in Tomcat未在 Tomcat 中调用 Servlet
【发布时间】:2012-11-24 08:27:50
【问题描述】:

我有一个简单的图像 servlet 来提供我存储在 Mongodb GridFS 中的图像。但是,我的 servlet 似乎没有被调用。

下面你可以看到我的web.xml的一部分,以及浏览器中的xhtml和html源码。 jsf 代码似乎工作正常,并且正在提取正确的 id。从我的日志来看,显示页面时似乎没有调用 servlet 的 doGet 方法。

任何关于为什么不这样做的想法将不胜感激。

这是我的 web.xml 片段:

<servlet>
    <servlet-name>imageServlet</servlet-name>
    <servlet-class>com.iLearn.view.servlet.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>imageServlet</servlet-name>
    <url-pattern>/image/*</url-pattern>
</servlet-mapping>

这是来自 xhtml 文件的片段:

    <h:panelGrid id="resourcePanel" columns="4">
    <a4j:repeat value="#{resourceUpload.resources}" var="res"
        rowKeyVar="rowKey">
        <h:panelGrid columns="1" style="width:100px; height:150 px;">
                <h:graphicImage
                    value="image?id=#{res.idAsString}"
                    style="width:100px; height:100px;"
                />
                <h:outputText value="#{res.name}" />
                <h:outputText value="#{res.mimeType}" />
                <h:outputText value="#{res.bytes}" />
        </h:panelGrid>
    </a4j:repeat>
</h:panelGrid>

这是我从浏览器获取的来源:

<table style="width:100px; height:150 px;">
<tbody>
<tr>
<td><img src="image?id=50ad879f93839043ffc4f156" style="width:100px; height:100px;" /></td>
</tr>
<tr>
<td>headshot.JPG</td>
</tr>
<tr>
<td>image/jpeg</td>
</tr>
<tr>
<td></td>
</tr>
</tbody>
</table>
<table style="width:100px; height:150 px;">
<tbody>
<tr>
<td><img src="image?id=50add3b09383f7b98d0f6e66" style="width:100px; height:100px;" /></td>
</tr>
<tr>
<td>workon.JPG</td>
</tr>
<tr>
<td>image/jpeg</td>
</tr>
<tr>
<td></td>
</tr>
</tbody>
</table>

这是我的 servlet 中的 doGet 方法。由于它没有命中第一个日志语句,我很确定它永远不会进入这个方法。为什么不呢??

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String idString = request.getParameter("id");
getLogger().debug("ImageServlet got request to serve up image id = " + idString);
    if((idString == null) || (idString.isEmpty())) {
        getLogger().debug("Image id was null, so image not displayed");
        return;
    }
    ObjectId id = new ObjectId(idString);
    Resource image = getResourceRecordDao().load(id);
    if(image == null) {
        getLogger().warn("Could not find image with id: " + id);
        return;
    }
    response.reset();
    response.setContentType(image.getMimeType());
    response.setHeader("Content-Length", String.valueOf(image.getSize()));
    response.setHeader("Content-Disposition", "inline; filename=\"" + image.getName() + "\"");
    image.render(response.getOutputStream());
   }

【问题讨论】:

  • 使用 firebug 或类似的东西来检查正在发出的实际 http 请求,我怀疑 web.xml 中的 url 模式是错误的。您正在寻找 /images/* - 与 "image?id=50add3b09383f7b98d0f6e66" 不匹配 您的 html 必须是 "images/image?id=50add3b09383f7b98d0f6e66"

标签: image mongodb jsf tomcat servlets


【解决方案1】:

&lt;h:graphicImage&gt; 中的相对路径是相对于当前请求 URL 的。因此,如果当前请求的页面本身位于http://examlpe.com/context/folder/page.xhtml 之类的文件夹中,则图像将从http://example.com/context/folder/image?id=123 下载,而您希望从上下文根目录中引用它,如http://example.com/context/image?id=123

在 URL 前加上 / 使其成为绝对路径,&lt;h:graphicImage&gt; 将呈现相对于上下文根的 URL。

<h:graphicImage value="/image?id=#{res.idAsString}" />

与具体问题无关,您应该在找不到图像时返回 404,而不是空响应。否则会让客户感到困惑。

if (image == null) {
    getLogger().warn("Could not find image with id: " + id);
    response.sendError(HttpServletResponse.SC_NOT_FOUND);
    return;
}

(在您检查参数本身的其他地方进行)

【讨论】:

  • 谢谢 - 这解决了它。关于 ima
  • 谢谢 - 这解决了它。关于 image == null 执行路径,我还没有决定如何处理它。如果图像为空,则数据库中可能存在数据完整性问题,因此将其作为 404 错误发送给用户没有多大意义。我想我会想抛出一个数据异常,但是我需要处理它,所以我仍然需要处理这条路径。感谢您指出这一点 - 有时您推迟决定,然后忘记,错误情况被埋在日志中。我还不如现在就抛出异常,以后再想办法处理。
猜你喜欢
  • 1970-01-01
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 2023-03-26
  • 2013-02-05
  • 1970-01-01
相关资源
最近更新 更多