【问题标题】:Image is not showing (one image is showing and another image is not showing ) [duplicate]图像未显示(一张图像显示,另一张图像未显示)[重复]
【发布时间】:2013-11-19 10:39:22
【问题描述】:

Servlet 程序

import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Redirect
 */
@WebServlet("/Redirect")
public class Redirect extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
    System.out.println("servlet is intialized");
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("image/jpeg");  
        request.getRequestDispatcher("/myhtml.jsp").forward(request, response);     

    }



}

JSP 文件

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="image/jpeg; charset=UTF-8">
<title>NewFile.jsp</title>
</head>
<body>
<a href="http://www.gmail.com"><img src="<%=request.getContextPath()%>/images/gmailimage.jpg" width="100" height="100"></a>
<a href="http://www.yahoomail.com"><img src="<%=request.getContextPath()%>/images/yahooimage.jpg" width="100" height="100"></a>
</body>
</htm

图像文件夹是 --> http://i.stack.imgur.com/558ON.png 。我的问题是 Gmail 图像显示,但 Yahoomail 图像没有显示。结果 --> http://i.stack.imgur.com/dH5AT.png 。为什么这样,有什么问题。建议我提出您的宝贵意见。

【问题讨论】:

  • 你应该检查你的图片名称。
  • 我提供的正确名称有任何疑问,请查看我提供的链接 i.stack.imgur.com/558ON.pngi.stack.imgur.com/dH5AT.png
  • 你自己尝试过吗?就像将上面的 yahoo 的 href 标签写到 gmail 的标签上。工作正常吗?
  • 如果我用 gmailimage 重命名这两个图像,它会显示完美的 gmail 符号,但是当放置 yahooimage 时,图像没有显示。为什么?
  • 这里只有 yahooimage 的问题。那是什么问题?

标签: java image jsp servlets jsp-tags


【解决方案1】:

您是否已经尝试检查图像 URL? 使用 Chrome 的开发人员工具 (F12),您可以选择一个元素并检查其属性。查看其 URL 并尝试将其复制并粘贴到地址栏中。

可能你看不到,然后你可以尝试手动调整网址。

【讨论】:

  • 你正在通过网络浏览器访问图像,例如 file:///home/mpatil/Desktop/Ram/rocking/Examples/WebContent/images/
  • 我听不懂。 request.getContextPath() 不返回本地文件系统路径,那么你不能有一个以“file:///”开头的路径作为&lt;img src="&lt;%=request.getContextPath()%&gt;/images/yahooimage.jpg" width="1..的结果@浏览器页面中src属性的值是什么?
  • @servletsram : 也避免使用 Scriptlets,它们是纯粹的邪恶。使用 EL+JSTL 或 OGNL+S2 代替。