Search code examples
jspservletsjsp-tags

Image not showing in JSP?


I do have simple JSP web page, but it's not displaying an image

This is my JSP page where it will fetch the image from pics folder under web content

<%@ 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="text/html; charset=UTF-8">
    <title>Insert title here</title>
  </head>
  <body>

     <img width = "80" src="${pageContext.request.contextPath}/pics/">
  </body>
</html>

But the page looks like this

enter image description here


Solution

  • Your code should somewhat be like

    <img width = "80%" src="${pageContext.request.contextPath}/pics/imagename.jpg">
    

    File name is necessary and change 80 to 80% because your image might be too big and might just display a part of it

    Also make sure

    ${pageContext.request.contextPath}
    

    is not empty

    And to be absolutely sure

    <img width = "80%" src="check_with_original_directory/pics/imagename.jpg">